Question

I'm using LazyAdaptor to create a listview with some data I'm parsing from a web server. Each row has a ImageView that is acting like a button and what I'm trying to do when the ImageView is clicked it will retrieve a value in the row named "reference". I'm trying to just Toast it as a quick way to make sure I'm grabbing the correct reference ID. I added a OnClick method and was able to retrieve the "position" but not sure how to grab the reference ID. Any help would be appreciated.

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    RowHolder holder = null;
    if (convertView == null) {
   vi = inflater.inflate(R.layout.group_view, null);

    holder = new RowHolder();
    holder.reference = (TextView) vi.findViewById(R.id.reference);
    holder.name = (TextView) vi.findViewById(R.id.name);
    holder.vicinity = (TextView) vi.findViewById(R.id.vicinity);
    holder.checkin = (ImageView) vi.findViewById(R.id.check_in);
    vi.setTag(holder);
} else {
    holder = (RowHolder) vi.getTag();
}
    HashMap<String, String> placeList = new HashMap<String, String>();
    placeList = data.get(position);

    holder.reference.setText(placeList.get(MainActivity.KEY_REFERENCE));
    holder.name.setText(placeList.get(MainActivity.KEY_NAME));
    holder.vicinity.setText(placeList.get(MainActivity.KEY_VICINITY));


    holder.checkin.setTag(position);
    holder.checkin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                Toast.makeText(activity, v.getTag().toString(), Toast.LENGTH_SHORT).show();
            }
        });

    return vi;
}
Was it helpful?

Solution 4

Thanks for the input guys. I actually was able to do the following to return all the values in a alertdialog.

 final HashMap<String, String> finalPlaceList = placeList;
    holder.checkin.setTag(finalPlaceList);
    holder.checkin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            alert.showAlertDialog(activity, "Row Details", v.getTag().toString() ,false);
            }
        });

OTHER TIPS

Try this,

  holder.checkin.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            Toast.makeText(activity, placeList.get(MainActivity.KEY_REFERENCE), Toast.LENGTH_SHORT).show();
        }
    });

and

final HashMap<String, String> placeList = new HashMap<String, String>();

put final keyword.

Instead get string from your View using tab you should use this...

holder.checkin.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            Toast.makeText(activity, holder.reference.getText().toString(), Toast.LENGTH_SHORT).show();
        }
    });

Try Using onItemClick listener

listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {

// your code here. . . 
  }

            });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top