我有一个带有两个按钮的自定义ListView,当我点击任意一行上的任一按钮时,我想在Listview上获取文本标签,现在只需弹出一个吐司.到目前为止,我还没有收到我的数组中的最后一个项目.

这是一个屏幕截图,让您更好地了解我的意思

这是我的自定义ListView的适配器子类

static final String[] Names =

new String[] { "John", "Mike", "Maria", "Miguel"};

class MyArrayAdapter extends ArrayAdapter {

private final Context context;

int which;

public MyArrayAdapter(Context context, String[] pValues) {

super(context, R.layout.main, pValues);

this.context = context;

values = pValues;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

rowView = inflater.inflate(R.layout.main, parent, false);

TextView textView = (TextView) rowView.findViewById(R.id.label);

ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);

Button call = (Button) rowView.findViewById(R.id.button1);

Button chat = (Button) rowView.findViewById(R.id.button2);

textView.setText(values[position]);

// Change icon based on name

s = values[position];

which = position;

call.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

String name = values[which];

Toast.makeText(CustomListView.this, name, Toast.LENGTH_SHORT).show();

}

});

return rowView;

}

}

编辑:

String name = textView.getText().toString();

RelativeLayout ll = (RelativeLayout)v.getParent();

textView = (TextView)ll.findViewById(R.id.label);

Toast.makeText(CustomListView.this, name,

Toast.LENGTH_SHORT).show();

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐