0

I've got a custom Adapter like this:

public class DeviceAdapter extends BaseAdapter {

private static ArrayList<Device> availableDevices;
Context c;
private LayoutInflater mInflater;

/**
 * Constructor for this class
 * @param context Which context triggered this class
 * @param devices Object of each devices
 */
public DeviceAdapter(Context context, ArrayList<Device> devices) {
    this.availableDevices = devices; 
    this.c = context; 
    mInflater = LayoutInflater.from(c);
}


public int getCount() {
    return availableDevices.size();
}


public Object getItem(int position) {
    return availableDevices.get(position);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.device_list, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.address = (TextView) convertView.findViewById(R.id.address);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.name.setText("Device name: "+availableDevices.get(position).getName());
    holder.address.setText("Mac-address: "+ availableDevices.get(position).getAddress());


    return convertView;
}


static class ViewHolder {
    TextView name;
    TextView address;

}

}

My question is: How can I access an objects variable name where the user presses?

Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143
  • Visit http://stackoverflow.com/questions/11243040/how-to-pass-the-value-of-row-in-listview-to-a-button/11243100#11243100 – RPB Jun 28 '12 at 11:39

2 Answers2

2

use onItemClickListenerwith list http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

ist.setOnItemClickListener(new AdapterView.onItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
      Object listItem = list.getItemAtPosition(position);

     or
     TextView tv =    (TextView) view.findViewById(R.id.name);
         name = tv.getText(); 

   } 
});
Khan
  • 7,585
  • 3
  • 27
  • 44
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

You can get OnClick event for only name TextView click, by adding holder.name.setOnClickListener....... See followin code

public class DeviceAdapter extends BaseAdapter {

private static ArrayList<Device> availableDevices;
Context c;
private LayoutInflater mInflater;

/**
 * Constructor for this class
 * @param context Which context triggered this class
 * @param devices Object of each devices
 */
public DeviceAdapter(Context context, ArrayList<Device> devices) {
    this.availableDevices = devices; 
    this.c = context; 
    mInflater = LayoutInflater.from(c);
}


public int getCount() {
    return availableDevices.size();
}


public Object getItem(int position) {
    return availableDevices.get(position);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.device_list, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.address = (TextView) convertView.findViewById(R.id.address);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.name.setText("Device name: "+availableDevices.get(position).getName());
    holder.address.setText("Mac-address: "+ availableDevices.get(position).getAddress());

    //   PUT FOLLOWIN   ------------------
    holder.name.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    put your dezired code here which runs OnClick of TextView NAME
                }
            });
    //         -----------------

    return convertView;
}


static class ViewHolder {
    TextView name;
    TextView address;

}

}
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
  • 1
    You should include only the required code here. It looks good as well. – Paresh Mayani Jun 28 '12 at 11:47
  • Dear Paresh, I have put all code, because Tobias Moe Thorstensen can know where to add the extra code – Chintan Raghwani Jun 28 '12 at 12:11
  • Yup Paresh, but I thought Tobias Moe Thorstensen is newer(reputation-159), so I made my hint easier. If Tobias Moe Thorstensen doesn't know that the code given by Dheeresh must be putted in the Activity which is using DeviceAdapter adapter class, then Dheeresh's effort may be lesser useful to Tobias Moe Thorstensen. – Chintan Raghwani Jun 28 '12 at 12:21