Max Max - 1 month ago 14
Android Question

How to set onClickListener for custom ListView

I have created a custom ListView, where the layout is definited in two XML, one which contain the definition of the ListView in a layout

For example activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>


The other part of the XML is in another file, for example custom_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector">

<LinearLayout
android:id="@+id/photoList"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/takePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/camera"/>
</LinearLayout>
</RelativeLayout>


In my activity i have declared actvivity_main.xml as main layout:

public class MainActivity extends AppCompatActivity{
private List<CustomRow> imageList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cmp_details);

/* Initialization structures, variable, ecc */
ListView lv = (ListView) findViewById(R.id.imageList);
adapter = new CustomList(this, imageList);
lv.setAdapter(adapter);
}

/* Add element list in ListView */
private void addList(int idC){
CustomRow cr = new CustomRow();
cr.setId(idC);
cr.setTitle("xxx "+ (idC+1));
cr.setThumbnailUrl(R.drawable.no_image);
imageList.add(cr);
adapter.notifyDataSetChanged();
}

private void takePhoto(){
//Do something
}
}


Custom List.java

public class CustomList extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<CustomRow> ListItem;
private ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public CustomList(Activity activity, List<CustomRow> listPhotoItems) {
this.activity = activity;
this.ListItem = listPhotoItems;
}

@Override
public int getCount() {
return ListItem.size();
}

@Override
public Object getItem(int location) {
return ListItem.get(location);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.custom_list, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();

ImageView thumbNail = (ImageView) convertView.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);

CustomRow m = ListItem.get(position);
thumbNail.setImageResource(m.getThumbnailUrl());
title.setTag(m.getId());
title.setText(m.getTitle());
return convertView;
}
}


Now I have to implement the OnClickListener of takePhoto, how I can do this ?
How I can bind onClickListener from the activity ?

I tried this solution but not work...

Answer

you could use setonitemclicklistener

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 imageList.get(position); // here you will get the clicked item from 
    //your imagelist and you can check by getting a title  by using this

    String title= imageList.get(position).getTitle();
    if(title.equals("you title to match")){
    //do your action or you can get a particular position and click there
    }
           } 
        });