I need to make my list item clickable instead of individual textview or Image view. My Activity code calling adapter is as below:
@Override
public void onRequestTaskCompleted(String response) {
// TODO Auto-generated method stub
locList = new ArrayList<LocationData>();
locList = LocationData.getDataFromJSONArray(response);
list = (ListView) findViewById(R.id.locationlist_listview);
adapter = new LocationListAdapter(this, locList);
list.setAdapter(adapter);
}
Adapter class is as below:
public class LocationListAdapter extends BaseAdapter {
Context context;
ArrayList<LocationData> data;
LayoutInflater inflater;
ViewHolder holder;
private LayoutInflater mLayoutInflater;
public LocationListAdapter(Context context, ArrayList<LocationData> arrayList
) {
this.context = context;
data = arrayList;
// get the layout inflater
mLayoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
private class ViewHolder {
TextView locName,locAddress;
ImageView locPic;
}
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(
R.layout.locationlist_listitem, null);
holder.locName = (TextView) convertView
.findViewById(R.id.loclistitem_name);
holder.locAddress = (TextView) convertView
.findViewById(R.id.loclistitem_address);
holder.locPic = (ImageView) convertView
.findViewById(R.id.loclistitem_pic);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.locName.setText(data.get(position).getLocationName());
holder.locName.setId(data.get(position).getLocationId());
holder.locAddress.setText(data.get(position).getAddress1().concat(data.get(position).getAddress2()));
holder.locPic.setBackgroundResource(R.drawable.songs);
return convertView;
}
If is set onclick on any textview its working fine but I need whole list item to be clickable.
I have tried list.setOnItemClickListener
before call to my adapter from activity but that doesn't work. Below is what I tried
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
LocationData lc = new LocationData();
lc = locList.get(position);
int locId = lc.getLocationId();
AppSession sInstance = AppSession.getInstance();
sInstance.setLocId(locId);
}
});
ListItem XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://splashurl.com/m22ydvb
android:id="@+id/itemdisplaylist_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/loclist_item_minumum_height"
android:orientation="horizontal"
android:weightSum="1" >
<ImageView
android:id="@+id/loclistitem_pic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.2"
android:contentDescription="@null" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:orientation="vertical"
android:weightSum="1" >
<TextView
android:id="@+id/loclistitem_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:textColor="@color/loclistitem_text"
android:textIsSelectable="true" >
</TextView>
<TextView
android:id="@+id/loclistitem_address"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:textIsSelectable="true" >
</TextView>
</LinearLayout>
Changed GetView method:
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(
R.layout.locationlist_listitem, null);
holder.locName = (TextView) convertView
.findViewById(R.id.loclistitem_name);
holder.locAddress = (TextView) convertView
.findViewById(R.id.loclistitem_address);
holder.locPic = (ImageView) convertView
.findViewById(R.id.loclistitem_pic);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.locName.setTag(position);
holder.locName.setText(data.get(position).getLocationName());
holder.locName.setId(data.get(position).getLocationId());
holder.locAddress.setText(data.get(position).getAddress1().concat(data.get(position).getAddress2()));
holder.locPic.setBackgroundResource(R.drawable.songs);
convertView.setOnClickListener(this);
return convertView;
}
@Override
public void onClick(View v) {
int position = (Integer)v.findViewById(R.id.loclistitem_name).getTag();
Log.d("list item clicked on :" , Integer.toString(position));
}
Also I tried implementing callback from adapter to activity but that didn't work too. May be I wasn't doing it right way.
I have read lot many similar posts and tried numerous things but seems like missing something to implement it the right way.
Please advise.