[英]How can I get the n-th element in a ListView
我有一個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的layoutfragment_list_view.xml,然后在SimpleAdapter中使用List>(aList)和具有TextView和Switch的player_list.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
<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正常運行,並且在屏幕上看到我想要的東西:播放器列表和每個播放器的開關:
截圖-> 圖片
現在,如果我從適配器執行getItem(),我會看到該行的第一個元素(Player1,Player2,...)
我的問題是:如何獲取每行(每個玩家)的切換ID,以查看是否已選中? 而且,如果我以錯誤的方式處理了ListView,您能否解釋一下這樣做的最佳方法是什么? 謝謝
您可能會看一下我對此的回答,即Spannable String僅適用於ListView中的最后一個Item 。
您可以使用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);
希望對您有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.