我在努力适应
this tutorial
用于
TextView
和
Button
。我已经创建了
ArrayList
和
ListView
:
private void displayListView() {
//putting two Country objects into the ArrayList
//these countries have email addresses for some reason, just go with it
ArrayList<Country> countryList = new ArrayList<Country>();
country = new Country("email@email.com","DummyName1",false);
countryList.add(country);
country = new Country("email@email.com","DummyName2",false);
countryList.add(country);
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,
R.layout.pending_invite, countryList);
ListView listView = (ListView) findViewById(R.id.listViewPendingRequests);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
以下是我的主要XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listViewPendingRequests" />
</LinearLayout>
这是我的
pending_invite.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Pending Invitation 1"
android:id="@+id/textView7"
android:layout_weight="1" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/button5"
android:layout_weight="1"
android:onClick="onClickCancel"/>
</LinearLayout>
</LinearLayout>
我想我有麻烦了
MyCustomAdapter
考虑到我不再找支票之类的东西了。无论如何,这就是我目前所得到的:
private class MyCustomAdapter extends ArrayAdapter<Country> {
private ArrayList<Country> countryList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Country> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Country>();
this.countryList.addAll(countryList);
}
private class ViewHolder {
TextView code;
Button name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.pending_invite, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.textView7);
holder.name = (Button) convertView.findViewById(R.id.button5);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
Country country = countryList.get(position);
holder.name.setText(country.getName());
holder.name.setTag(country);
return convertView;
}
}
因此,这对于构造视图是有效的(令人惊讶),但我如何知道在onClick中按下了哪个?:
public void onClickCancel(View view){
Log.i(LOG_TAG, "OnClickCancel view: " + view.getId()); //getId returns the same thing for all clicks. How do I get which button clicked?
}
这个
view.getId
为所有按钮返回相同的ID。如何将onClick侦听器附加到自定义视图中的膨胀按钮?