I have searched the web for a way to do this, but perhaps I’m not searching for the right thing. I have a ListView that serves as a live feed, essentially. In each feedEntry object of this ListView (ArrayList) the application displays words separated by a space as one String. Here’s an example of what one ListView item (one feedEntry) would display:
Sahara Africa Ohio Libya
I would like to have it so that the user can click on each word and a PopupWindow will display (which will, in turn have separate links in it). I understand how to set up this PopupWindow, but how would I attach an onClickListener for each of these words? I’m not sure which code would be helpful to you, but here is my main Feed activity (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);
}
}
Here is my feedEntry class (for each feedEntry item in the 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();
}
}
}
Any help is greatly appreciated. Again, I am very sorry for all of the code. I just wanted to give out as much as possible that may be relevant. At this point, I’m sure that I will need to include these onClickListeners in my Feed.java, since that’s the main activity, but I’m not sure how to put them on separate words within a String.
First, I am skeptical that your UX will be very good. Phones are tiny. Users will have great difficulty accurately tapping on one work versus another with lots of words per row.
Second, I am skeptical that a
ListViewis the right answer. AGridViewwould be simpler, if you want rows and columns of words.All that being said, you will have to extend
ArrayAdapter, overridegetView(), create individualTextViews per word, and set upOnClickListenersfor eachTextView.