简体   繁体   中英

Get position of item in listView with onItemClick on particular object

I have a ListView with several entries that I get from an external database. Every entry has an image button and other objects. I want to know the position of the list entry, where the user clicked the image button.

Is there a possibility to look at witch object particulary the user clicked with

this.getALlEntrysListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            try {
                JSONObject entryClicked = jsonArray.getJSONObject(position);

? This code is in my MainActivity class.

I set the particular contents of each entry in an Adapter class.

What is another possibility to get the position of jsonArray only if the user cicks the image button?

This is my Adapter class:

public class GetAllEntrysListViewAdapter extends BaseAdapter {

private JSONArray dataArray;
private Activity activity;
private static LayoutInflater inflater = null;

public GetAllEntrysListViewAdapter(JSONArray jsonArray, Activity a) {
    this.dataArray = jsonArray;
    this.activity = a;

    inflater = (LayoutInflater)this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return this.dataArray.length();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    //set up convert view if it is not null
    ListCell cell;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);

        cell = new ListCell();
        cell.likes = (TextView) convertView.findViewById(R.id.listViewLikes);
        cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
        cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);
        cell.likeButton = (ImageButton) convertView.findViewById(R.id.likeButton);

        convertView.setTag(cell);

    }
    else {
        cell = (ListCell)convertView.getTag();
    }

    //change data of cell
    try {
        JSONObject jsonObject = this.dataArray.getJSONObject(position);
        cell.likes.setText("Likes: "+ jsonObject.getString("likes"));
        cell.note.setText(jsonObject.getString("note"));

        String img = jsonObject.getString("image");
        if (img.equals("img")) {
            cell.img.setImageResource(R.drawable.logo);
        }
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
    return convertView;

}

private class ListCell {
    private TextView likes;
    private TextView note;
    private ImageView img;
    public ImageButton likeButton;

}
imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RelativeLayout parent = (RelativeLayout)v.getParent();
                int position = listView.getPositionForView(parent);
                // position in listview
            }
        });

What is another possibility to get the position of jsonArray only if the user cicks the image button?

you have to call setOnClickListener and the ImageButton . Particularly important is to tag the ImageButton with the position

cell.likeButton.setTag(position);
cell.likeButton.setOnClickListener(...);

and when onClick is fired, retrieve the tag , which represent the position, and through it the entry in your dataset

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM