#android
#Android
Вопрос:
Учитывая listview
со многими полями данных в строке, как мы можем отделить только одно поле (скажем, id) от строки с onClickListener()
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object dataRow = listView.getItemAtPosition(position);
Log.e("hi, this is the full row of data, i just want 1 of the fields", dataRow);
}
Ответ №1:
Это может быть достигнуто с помощью пользовательского listview путем расширения BaseAdapter до класса.. В классе вы можете
Внутри oncreate :
listview.setAdapter(new newListview(this); //listview is the object of ListView in xml file
внешние onCreate и newListview являются внутренним классом для activity
class newListview extends BaseAdapter{
private Context context;
public PrayerList(Context context) {
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;// here return size of listview
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View view, ViewGroup viewgroup) {
ViewHolder holder = null;
LayoutInflater inflater = LayoutInflater.from(context);
if (view == null)
{
view = inflater.inflate(R.layout.mydatalistview, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) view.findViewById(R.id.title);
holder.txtViewDescription = (TextView) view.findViewById(R.id.description);
holder.arrowImage=(ImageView)view.findViewById(R.id.arrow_imageview);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
//here you can use onclick of particular item of listview
}
}
class ViewHolder{
TextView txtViewTitle;
TextView txtViewDescription;
ImageView arrowImage;
}