Ad

Add An Onclicklistener To Every Row From A Listview (android)

- 1 answer

I've fetched and parsed a JSON data from an URL and displayed it in ListView but I can't seem to add an onclicklistener on every row I just want to be able to do an action when I click on the row, with the data that the row contains to get all informations of this row in an other activity ? when i launch code, the app stopped immediatly !!! thanks

public class DemandeAdapter extends BaseAdapter {

private Context context;
private ArrayList<DemandeModel> demandeModelArrayList;


public DemandeAdapter(Context context, ArrayList<DemandeModel> demandeModelArrayList) {

    this.context = context;
    this.demandeModelArrayList = demandeModelArrayList;
}

@Override
public int getViewTypeCount() {
    return getCount();
}
@Override
public int getItemViewType(int position) {

    return position;
}

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

@Override
public Object getItem(int position) {
    return demandeModelArrayList.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.demande, null, true);

        holder.iv = (ImageView) convertView.findViewById(R.id.iv);
        holder.id = (TextView) convertView.findViewById(R.id.id);
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.desc = (TextView) convertView.findViewById(R.id.desc);
        holder.date = (TextView) convertView.findViewById(R.id.date);
        holder.listView = (ListView) convertView.findViewById(R.id.lv);

        convertView.setTag(holder);
    }else {
        // the getTag returns the viewHolder object set as a tag to the view
        holder = (ViewHolder)convertView.getTag();
    }

    Picasso.get().load(demandeModelArrayList.get(position).getImgURL()).into(holder.iv);
    holder.id.setText("ID : "+demandeModelArrayList.get(position).getId());
    holder.date.setText("Date : "+demandeModelArrayList.get(position).getDate());
    holder.name.setText("Name : "+demandeModelArrayList.get(position).getNom());
    holder.desc.setText("Description : "+demandeModelArrayList.get(position).getDescription());
    holder.listView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, demandeDetails.class);
            intent.putExtra("ID",demandeModelArrayList.get(position).getId());
            intent.putExtra("Date",demandeModelArrayList.get(position).getDate());
            intent.putExtra("Name",demandeModelArrayList.get(position).getNom());
            intent.putExtra("Description",demandeModelArrayList.get(position).getDescription());
            intent.putExtra("Image",demandeModelArrayList.get(position).getImgURL());
            context.startActivity(intent);
        }
    });

    return convertView;
}

private class ViewHolder {

    protected TextView id,name,desc,date;
    protected ImageView iv;
    private ListView listView;

}

}

i do that min my adapter class not in my activity !!! And this is the other activity where i want to display data

public class demandeDetails extends AppCompatActivity implements Serializable {

protected TextView id,name,desc,date;
protected ImageView img;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demande_details);
    id = (TextView)findViewById(R.id.demande_Id);
    name = (TextView)findViewById(R.id.demande_Name);
    desc = (TextView)findViewById(R.id.demande_Desc);
    date = (TextView)findViewById(R.id.demande_Date);
    img = (ImageView)findViewById(R.id.picture_demande);

    Intent intent = getIntent();
    int imagee = intent.getExtras().getInt("Image");
    String idd = intent.getExtras().getString("ID");
    String namee = intent.getExtras().getString("Name");
    String descc = intent.getExtras().getString("Description");
    String datee = intent.getExtras().getString("Date");

    img.setImageResource(imagee);
    id.setText(idd);
    name.setText(namee);
    desc.setText(descc);
    date.setText(datee);

}

}
Ad

Answer

You can add onItemClickListener just like below

listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

        Toast.makeText(this,"You selected : " + position,Toast.LENGTH_SHORT).show();                
    }
});
Ad
source: stackoverflow.com
Ad