[英]How can I get the n-th element in a ListView
I have a listView with two items inside each row, a TextView and a Switch button. 我有一个listView,每行中有两个项目,一个TextView和一个Switch按钮。
This is how I created the List View: 这就是我创建列表视图的方式:
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);
I used the layout fragment_list_view.xml which has the listView and then in the SimpleAdapter I used a List> (aList) and the layout player_list.xml which has a TextView and a Switch. 我使用具有listView的layoutfragment_list_view.xml,然后在SimpleAdapter中使用List>(aList)和具有TextView和Switch的player_list.xml布局。 I don't know if it is the right way to do this but it works. 我不知道这是否是正确的方法,但是可以。
fragment_list_view.xml 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 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" />
As you can see the ListView works and I see on the screen what I want : the list of the players and the switches for each player: 如您所见,ListView正常运行,并且在屏幕上看到我想要的东西:播放器列表和每个播放器的开关:
Now, if I do a getItem() from the adapter I see the first element of the row (Player1, Player2,...) 现在,如果我从适配器执行getItem(),我会看到该行的第一个元素(Player1,Player2,...)
My question is: how can I get the Switch Id of each row (each player) to see if it's checked or not? 我的问题是:如何获取每行(每个玩家)的切换ID,以查看是否已选中? And if I did the ListView in the wrong way can you explain me what is the best way to do that? 而且,如果我以错误的方式处理了ListView,您能否解释一下这样做的最佳方法是什么? Thank you 谢谢
You may take a look of my answer to this, Spannable String only working for last Item in ListView . 您可能会看一下我对此的回答,即Spannable String仅适用于ListView中的最后一个Item 。
You can replace that ArrayAdapter with SimpleAdapter, like this: 您可以使用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);
Hope that help! 希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.