I have a list view in my android list activity which get data through a json file. In the list view I have to show the basic details of the item. When we click on a item I need
public class MainActivity extends AppCompatActivity {
private ListView listview;
private ArrayList<DataModel> arrayList;
private DataModel datamodel;
private AdapterForList adapterForList;
public ArrayList<DataModel> getArrayList(){
arrayList = new ArrayList<DataModel>();
datamodel = new DataModel();
for(int i=0; i<8; i++){
datamodel.setTextData("count"+i);
}
return arrayList;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
adapterForList = new AdapterForList(this,getArrayList());
listview.setAdapter(adapterForList);
}
private class AdapterForList extends BaseAdapter{
ArrayList<DataModel> DataList;
private ViewHolder holder;
private LayoutInflater inflater;
private Context context;
private AdapterForList(Context context,ArrayList<DataModel> arrayList){
this.DataList = arrayList;
this.context = context;
Log.d("hereeee","--->"+DataList.size());
}
@Override
public int getCount() {
return DataList.size();
}
@Override
public Object getItem(int position) {
return DataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
holder = new ViewHolder();
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.raw_view, null, true);
holder.textview = (TextView) convertView.findViewById(R.id.textView);
holder.lltextview = (LinearLayout) convertView.findViewById(R.id.lltextview);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.textview.setText(DataList.get(position).getTextData());
holder.lltextview.setTag(R.integer.btnview, convertView);
holder.lltextview.setTag(R.integer.btnpos, position);
holder.lltextview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View tempview = (View) v.getTag(R.integer.btnview);
Integer pos = (Integer) v.getTag(R.integer.btnpos);
TextView tv = (TextView) tempview.findViewById(R.id.textView);
tv.setText("changeee");
}
});
return convertView;
}
private class ViewHolder{
public TextView textview;
public LinearLayout lltextview;
}
}
}
In your listener implementation you have reference to the View
that was clicked, and in case of itemClick
(or ExpandableList: groupClick
/ childClick
) you also have the position of the underlying data inside your list.
So just use the position to retrieve the clicked data from the data structure you used to populate the list (adapter).
Update
If you have a ListActivity
, in which the dataList
member holds the values you are displaying in the list, you just override it's onListItemClick
method:
/**
* The list of custom data you display in this activity
*/
private ArrayList<MyData> dataList;
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final MyData selectedValue = dataList.get(position);
//TODO: deal with this selectedValue
}
API Docs for onListItemClick
This method will be called when an item in the list is selected. Subclasses should override.
Subclasses can call getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters:
l
The ListView where the click
happenedv
The view that was clicked within
the ListViewposition
The position of the view
in the listid
The row id of the item that was
clickedA complete example you can find on Android-er here and here.