Custom Adapter: Get Item Number Of Clicked Item In Inflated Listview
Solution 1:
Why not use onItemClickListener on ListView itself?
Your Adapter should contain a List of one Object type (there is no strict rule, but it helps managing the item much easier). For example
classMsgObject{
String msg;
String data
//TODO Getters/Setters goes here
}
Then your CustomAdapter will only contain
List<MsgObject> objectList;
Then your getView will looks similar to this
MsgObject m = (MsgObject)getObject(position);
TextView text=(TextView)vi.findViewById(R.id.text);
ImageView image=(ImageView)vi.findViewById(R.id.image);
text.setText(m.getMsg());
text.getLineCount();
imageLoader.DisplayImage(m.getData(), image);
//Tag idis predefined in xml
vi.setTag(R.id.listItemTag, m);
return vi;
Now your view will handle this as one object instead of one layout with multiple values.
Then we move all the click action to the Activity which ListView resides in.
listView.setOnItemClickListener(){
newAdapterView.OnItemClickListener(){
@overridepubliconItemClick(AdapterView<?> parent, View view, int position, long id){
MsgObjectm= (MsgObject)view.getTag(R.id.listItemTag);
Toast.makeText(context, "Pos[" + position
+ "] clicked, with msg: " + m.getMessage(), Toast.LENGTH_SHORT).show();
}
}
};
This is the way I have my ListView with Lazy load ImageView as well. Then you will be able to access the object tied to that view and also the position which is clicked.
If you are so wish to separate msg and data. You can use setTag(id, obj); for both object, etc.
setTag(R.id.listItemMsg, msg[position]);
setTag(R.id.listItemData, data[position]);
UPDATE: Example of my CustomAdapter
/**
* Adapter for displaying Place selection list.
* @author Poohdish Rattanavijai
*
*/publicclassPlaceAdapterextendsBaseAdapter {
privatestaticfinalStringTAG= PlaceAdapter.class.getSimpleName();
private List<PlaceVO> list; // <-- list of PlaceVOsprivate Context context;
privateint viewResourceId;
/**
*
* @param context Context
* @param viewResourceId Layout ID for each item
* @param list resource list to populate
*/publicPlaceAdapter(Context context, int viewResourceId, List<PlaceVO> list){
this.context = context;
this.viewResourceId = viewResourceId;
this.list = list;
}
/**
* Number of result in the list plus one (for +add at the last item)
*/@OverridepublicintgetCount() {
if(null != list){
return list.size();
}
return1;
}
@Overridepublic Object getItem(int arg0) {
if(null != list){
try {
return list.get(arg0);
} catch (IndexOutOfBoundsException e) {
returnnull;
}
}
returnnull;
}
@OverridepubliclonggetItemId(int position) {
// if(null != list){// try {// return list.get(position).getId();// } catch (IndexOutOfBoundsException e) {// return 0;// }// }return0;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
if(null == convertView){
/**
* View does not exist, populate.
*/LayoutInflaterinflater= LayoutInflater.from(this.context);
convertView = inflater.inflate(this.viewResourceId, parent, false);
}
ViewHolderholder= (ViewHolder)convertView.getTag(R.id.adpter_view);
if(null == holder){
Log.d(TAG, "holder not found, init.");
/**
* ViewHolder does not exists for this view; create and assign respective view.
*/
holder = newViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.details = (TextView) convertView.findViewById(R.id.details);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress);
}
PlaceVOv= (PlaceVO)getItem(position); // <-- GetItemif(null != v){
Log.d(TAG, "Place not null");
if(HelperUtil.IsNotNullOrEmpty(v.getName())){
Log.d(TAG, "Name: " + v.getName());
holder.title.setText(v.getName());
}
if(HelperUtil.IsNotNullOrEmpty(v.getVicinity())){
Log.d(TAG, "details: " + v.getVicinity());
holder.details.setText(v.getVicinity());
}
if(HelperUtil.IsNotNullOrEmpty(v.getIcon())){
holder.progress.setVisibility(View.VISIBLE);
holder.icon.setVisibility(View.GONE);
//TODO Initialize LazyLoad
}else{
holder.progress.setVisibility(View.VISIBLE);
holder.icon.setVisibility(View.GONE);
}
}
// Two tags, one for holder, another for the VO
convertView.setTag(R.id.adpter_view, holder);
convertView.setTag(R.id.adpter_object, v);
return convertView;
}
staticclassViewHolder{
TextView title;
TextView details;
ImageView icon;
ProgressBar progress;
}
}
Inside Activity I handle item click action with
OnItemClickListeneritemClick=newOnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
PlaceVOv= (PlaceVO)arg1.getTag(R.id.adpter_object); // <-- get object using tag.
switchToPlaceScreen(v);
}
};
listView.setOnItemClickListener(itemClick);
Hope this answer your question :)
Solution 2:
text.setText(msg[position]);
text.getLineCount();
imageLoader.DisplayImage(data[position], image);
As you move towards creating a Custom adapter, you want to reference your items using getItem(int position) as much as possible. Avoid referencing it using the actual arrays you pass in your Adapter constructor.
Try overriding the getItem(int position)
and getItemId(int position)
on your adapter, and use that instead of the arrays themselves inside your getView
.
Post a Comment for "Custom Adapter: Get Item Number Of Clicked Item In Inflated Listview"