Я искал в Интернете способ сделать это, но, возможно, я не ищу нужную вещь.У меня есть ListView, который по сути служит живой трансляцией.В каждом объекте feedEntry этого ListView (ArrayList) приложение отображает слова, разделенные пробелом, как одну строку.Вот пример того, что будет отображаться один элемент ListView (один feedEntry):
Sahara Africa Ohio Libya
Я хотел бы иметь его, чтобы пользователь мог щелкнуть по каждому слову и отобразить PopupWindow (которое, в свою очередь, будет, в свою очередь,есть отдельные ссылки в нем).Я понимаю, как настроить это PopupWindow, но как бы я прикрепил onClickListener для каждого из этих слов?Я не уверен, какой код будет вам полезен, но вот моя основная активность в Feed (Feed.java):
public class Feed extends Activity {
ListView liveFeed;
static ArrayAdapter<feedEntry> aa;
static ArrayList<feedEntry> entries = new ArrayList<feedEntry>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feed);
liveFeed = (ListView)this.findViewById(R.id.liveFeed);
int layoutID = android.R.layout.simple_list_item_1;
aa = new ArrayAdapter<feedEntry>(this, layoutID, entries);
liveFeed.setAdapter(aa);
final Handler handler = new Handler();
Timer time = new Timer();
TimerTask refresh = new TimerTask(){
public void run(){
handler.post(new Runnable(){
public void run(){
feedEntry.refreshFeed();
}
});
}
};
time.scheduleAtFixedRate(refresh, 0, 5000);
}
}
Вот мой класс feedEntry (для каждого элемента feedEntry в ListView):
public class feedEntry {
private static int line=1;//use later
private String keywords;
private JSONArray data;
public feedEntry(JSONArray obj){
data = obj;
keywords="";
try{
for (int i=0;i<data.length();i++){
JSONArray tmp = (JSONArray)data.get(i);
keywords += tmp.get(0)+" ";
}
} catch(JSONException e){
e.printStackTrace();
}
}
public String toString(){
return keywords;
}
public static void addNewEntry(feedEntry entry){
Feed.entries.add(0, entry);
Feed.aa.notifyDataSetChanged();//notify the array adapter that data has changed
}
//connects to and parses the feed
static public void refreshFeed(){
try{
String url = "http://192.17.254.11:8080/getdata?nextline="+line;
line++;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response;
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String input = null;
try {
while ((input = reader.readLine()) != null) {
sb.append(input + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String enter = sb.toString();
if (!enter.equals("None")){
JSONArray jArray = new JSONArray(enter);
feedEntry add = new feedEntry(jArray);
addNewEntry(add);
}
in.close();
} catch(MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e){
e.printStackTrace();
}
}
}
Любая помощь с благодарностью.Опять же, мне очень жаль весь код.Я просто хотел выдать как можно больше, что может иметь отношение к делу.На данный момент я уверен, что мне нужно будет включить эти onClickListeners в мой Feed.java, так как это основное действие, но я не уверен, как поместить их в отдельные слова в строке.