2012-06-28 3 views
0

Я получил пользовательский адаптер, как это:Получение объекта из ListView с CustomAdapter андроида

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, где пользователь прессы?

+0

обновите свой вопрос/ –

+0

Посетите http://stackoverflow.com/questions/11243040/how-to-pass-the-value-of-row-in-listview-to-a-button/11243100#11243100 –

ответ

2

использование onItemClickListenerwith список 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(); 

   }  
}); 
+1

onItemSelectedListener или onItemClickListener? –

+0

спасибо большое Paresh, я написал название право, но код другой функции по ошибке –

0

Вы можете получить OnClick событие только для имени TextView нажмите, добавив holder.name.setOnClickListener ....... См followin код

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; 

} 

} 
+1

Здесь вы должны указать только требуемый код. Это хорошо выглядит. –

+0

Дорогой Пареш, я поместил весь код, потому что Тобиас Мо Торстенсен может знать, где добавить дополнительный код –

+0

Yup Paresh, но я подумал, что Tobias Moe Thorstensen новее (репутация-159), поэтому я сделал мой намек проще. Если Тобиас Мо Торстенсен не знает, что код, данный Dheeresh, должен быть помещен в Activity, который использует класс адаптера DeviceAdapter, то усилия Dheeresh могут быть менее полезными для Tobias Moe Thorstensen. –

Смежные вопросы