Reputation: 659
I'm pretty new to android and I've got a question. I'm playing around with a simple Twitter client tutorial which is on github. however I can't manage to get a onClickListener
working properly. How can I retrieve the position of any item on the list that is being clicked on?
I've tried playing around by combining tutorials with no luck yet.
Here is the code: https://github.com/cacois/TweetView/blob/progress_bar/src/com/example/TweetItemAdapter.java
package com.example;
import java.util.ArrayList;
import com.example.Example.Tweet;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class TweetItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
private Activity activity;
public ImageManager imageManager;
public TweetItemAdapter(Activity a, int textViewResourceId, ArrayList<Tweet> tweets) {
super(a, textViewResourceId, tweets);
this.tweets = tweets;
activity = a;
imageManager =
new ImageManager(activity.getApplicationContext());
}
public static class ViewHolder{
public TextView username;
public TextView message;
public ImageView image;
public ProgressBar progress; //ADDED
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
holder = new ViewHolder();
holder.username = (TextView) v.findViewById(R.id.username);
holder.message = (TextView) v.findViewById(R.id.message);
holder.image = (ImageView) v.findViewById(R.id.avatar);
holder.progress = (ProgressBar) v.findViewById(R.id.progress_bar); //ADDED
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final Tweet tweet = tweets.get(position);
if (tweet != null) {
holder.username.setText(tweet.username);
holder.message.setText(tweet.message);
holder.image.setTag(tweet.image_url);
imageManager.displayImage(tweet.image_url, activity, holder.image, holder.progress); //CHANGED
}
return v;
}
}
Upvotes: 1
Views: 5232
Reputation: 6892
In the getView method, use this:
v.addOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do some stuff with the view that has been clicked
}
});
When you click an item, the appropriate function will be called because you now specified what has to happen with each view when you click it.
Upvotes: 2
Reputation: 659
ok figured out what to do. here is a solution in case someone has the same problem:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.v("position", String.valueOf(position));
}
});
this goes in the main activity right after listView.setAdapter
Upvotes: 1
Reputation: 5586
You need to use the http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
and
http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
When an item is clicked the onItemClick
will be passed the position of the item click and you can use this and the ArrayAdapter
getItem(int position)
to access the item and perform the operation.
Attach it to your view, this can be done in the activity on the adapter but I would suggest using it in the activity like so:
final ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt,
long paramLong) {
// TODO whatever you want to do to this item
}
});
Upvotes: 1
Reputation: 2905
Implement OnItemClickListener
in your activity and add the unimplemented methods.
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
System.out.println(position);
}
Set onItemClickListener
to your list object like
list.setOnItemClickListener(this);
your class should implements following
public class YourClass extends Activity implements OnItemClickListener {
Thats it.
Upvotes: 0
Reputation: 234795
Rather than attaching an OnClickListener to the view, it's easier to just override onListItemClick
in your ListActivity. If you aren't using a ListActivity, use setOnItemClickListener
on the ListView itself to register an AdapterView.OnItemClickListener
object.
EDIT
Assuming that you are subclassing ListActivity to hold your list, do something like this:
public class MyListActivity extends ListActivity {
// . . .
protected void onListItemClick(ListView l, View v, int position, long id) {
// process click on item #position
Tweet item = (Tweet) l.getItemAtPosition(position);
}
}
Upvotes: 2