0

次のようなカスタム アダプターがあります。

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;

}

}

name私の質問は次のとおりです。ユーザーが押したオブジェクト変数にアクセスするにはどうすればよいですか?

4

2 に答える 2

2

リストhttp://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.htmlでonItemClickListenerを使用します

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(); 

   } 
});
于 2012-06-28T11:36:51.817 に答える
0

holder.name.setOnClickListener....... を追加することで、TextView の名前のクリックのみに対して OnClick イベントを取得できます。次のコードを参照してください。

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;

}

}
于 2012-06-28T11:46:20.897 に答える