我有一个listView,每行内有两个项目,一个TextView和一个Switch按钮。
这就是我创建列表视图的方式:
rootView = inflater.inflate(R.layout.fragment_list_view, container,false);
ListView list = (ListView) rootView.findViewById(R.id.listViewFragment);
rootView.findViewById(R.id.buttonSubmitPlayersTrainingAttendances) ;
String[] from = { "namePlayer", "switch" };
int[] to = { R.id.textViewPlayerName, R.id.switchAttendance };
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int a=0; a < totalPlayers ;a++)
{
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("namePlayer", playersSurnames.get(a) + " " + playersNames.get(a) );
aList.add(hm);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.player_list, from, to);
list.setAdapter(adapter);
我使用了包含listView的布局fragment_list_view.xml,然后在SimpleAdapter中使用了List&gt; (aList)和布局player_list.xml,它有一个TextView和一个Switch。我不知道这是否是正确的方法,但它有效。
fragment_list_view.xml
<ListView
android:id="@+id/listViewFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
player_list.xml
<TextView
android:id="@+id/textViewPlayerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<Switch
android:id="@+id/switchAttendance"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
正如你所看到的,ListView可以工作,我在屏幕上看到了我想要的东西:每个玩家的玩家列表和开关:
截图 - &gt; Image
现在,如果我从适配器执行getItem(),我会看到该行的第一个元素(Player1,Player2,...)
我的问题是:我怎样才能获得每一行(每个玩家)的Switch ID以查看它是否被检查过?如果我以错误的方式做了ListView,你能解释一下,最好的方法是什么? 谢谢
答案 0 :(得分:0)
您可以查看我对此的回答,Spannable String only working for last Item in ListView。
您可以使用SimpleAdapter替换ArrayAdapter,如下所示:
String[] from = {ITEM_ID, ITEM_NAME, ITEM_CHECKED};
int[] to = {R.id.tvId, R.id.tvDescription, R.id.cb};
mSimpleAdapter = new SimpleAdapter(this, mDataList, R.layout.custom_list_items, from, to){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Prepare views ready for data
convertView = super.getView(position, convertView, parent);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.cb);
TextView tvDescription = (TextView) convertView.findViewById(R.id.tvDescription);
cb.setOnClickListener(new OnClickListener(){
@SuppressWarnings("unchecked")
@Override
public void onClick(View view) {
CheckBox buttonView = (CheckBox) view;
// Get view, position and DataList(position)
int pos = (int) buttonView.getTag();
HashMap<String, Object> selectedItem = new HashMap<String, Object>();
selectedItem = (HashMap<String, Object>) getItem(pos);
// Update DataList
mDataList.remove(selectedItem);
selectedItem.remove(ITEM_CHECKED);
if(buttonView.isChecked()){
selectedItem.put(ITEM_CHECKED, true);
mDataList.add(pos, selectedItem);
}else{
selectedItem.put(ITEM_CHECKED, false);
mDataList.add(pos, selectedItem);
}
// Update all UI views by
Toast.makeText(getApplicationContext(), ""+pos+" Changed", Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
});
// Get data from DataList
@SuppressWarnings("unchecked")
HashMap<String, Object> currentItem = (HashMap<String, Object>) getItem(position);
toggleLineThrough(tvDescription, (String)currentItem.get(ITEM_NAME), (boolean)currentItem.get(ITEM_CHECKED));
// Save position to CheckBox, so position can be retrieved when CheckBox is clicked
cb.setTag(position);
return convertView;
}
};
mListView.setAdapter(mSimpleAdapter);
希望有所帮助!