【发布时间】:2014-09-19 03:12:33
【问题描述】:
我有一个自定义列表视图,这是它的 layout.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/playerToken"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playerName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playerMoney"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
这里是一个自定义适配器,它接受一个玩家对象数组,其中存储了玩家姓名、代表他的代币和他的资金余额等信息。适配器获取该信息并填充我的自定义列表,如上面的布局。
public class MyAdapter extends ArrayAdapter<Player> {
public MyAdapter(Context context, Player[] values) {
super(context, R.layout.activity_banking_layout, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.activity_banking_layout, parent, false);
Player player = getItem(position);
TextView playerNameText = (TextView) theView.findViewById(R.id.playerName);
TextView playerMoneyText = (TextView) theView.findViewById(R.id.playerMoney);
ImageView playerToken = (ImageView) theView.findViewById(R.id.playerToken);
playerNameText.setText(player.getName());
playerMoneyText.setText(Integer.toString(player.getMoney()));
int rId = theView.getResources().getIdentifier(player.getToken(), "drawable",
getContext().getPackageName());
playerToken.setImageResource(rId);
return theView;
}
}
这只是显示我们正在调整的 listView 的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bankListView" />
</LinearLayout>
所以基本上,我的列表是在显示列表的活动的 onCreate 方法中创建和适配器的。之后,我的列表项可以打开一个上下文菜单,并根据选择的内容来操作播放器对象。我希望我的列表能够反映这些更改,所以我想知道如何访问自定义列表的特定部分并对其进行编辑。例如,我的自定义列表有一个玩家图标,右侧是玩家姓名,名称下方是金额。假设我想更改特定玩家的金额并将该更改反映在列表中,我如何在该 ListView 中的特定位置访问该特定 TextView?
【问题讨论】:
标签: android android-listview android-arrayadapter