我有一个android ListActivity,通过布局充气器创建多个按钮(来自数据库源):
private class QuestAdaptor extends ArrayAdapter<Quests> {
private ArrayList<Quests> quests;
public QuestAdaptor(Context context,
int textViewResourceId,
ArrayList<Quests> items) {
super(context, textViewResourceId, items);
this.quests = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.quests, null);
}
Quests o = quests.get(position);
TextView qt = (TextView) v.findViewById(R.id.quest_title);
TextView qpr = (TextView) v.findViewById(R.id.quest_price);
TextView qp = (TextView) v.findViewById(R.id.quest_points);
TextView btn = (TextView) v.findViewById(R.id.btn_select_quest);
qt.setText(o.title);
qpr.setText(o.dollar_price);
qp.setText(o.pts);
btn.setText(o.quest_id);
return v;
}
我还有一个在xml布局中定义的“onclick”方法:
<Button
android:id="@+id/btn_select_quest"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/quest_points"
android:onClick="selectQuest"
android:text="" />
public void selectQuest (View v){
Button btn_select_quest = (Button)findViewById(R.id.btn_select_quest);
String buttonText = btn_select_quest.getText().toString();
showToastMessage(buttonText);
}
我的问题是上面的代码在点击任何按钮时总是返回“1”(按钮标题为“1”,“2”,“3”等)。我想我正在寻找像this.getText()。toString?
这样的东西答案 0 :(得分:0)
不使用SelectQuest中的findviewbyid,而是使用传递给函数的v
String buttonText = v.getText()。toString();