0
所以我有一個列表視圖,當你點擊一行時,它會打開一個新的活動。在新的活動中有一個複選框。如果選中複選框,然後返回到listview活動,它應該在最初單擊的列表視圖項目旁邊設置複選標記。在基於CheckBox的ListView的某些位置設置ImageView
現在發生的事情是,當我檢查複選框並返回到列表視圖時,無論從哪個行檢查複選框,每行都有一個複選標記。
我的繼承人與ListView和上點擊收聽mainactivity啓動第二個複選框活動
//fill list view with xml array of routes
final CharSequence[] routeListViewItems = getResources().getTextArray(R.array.routeList);
//custom adapter for list view
ListAdapter routeAdapter = new CustomAdapter(this, routeListViewItems);
final ListView routeListView = (ListView) findViewById(R.id.routeListView);
routeListView.setAdapter(routeAdapter);
routeListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int listViewItemPosition = position;
CharSequence route = routeListViewItems[position];
int imageId = (int) image.getResourceId(position, -1);
if (route.equals(routeListViewItems[position]))
{
Intent intent = new Intent(view.getContext(), RouteDetails.class);
intent.putExtra("route", routeDetail[position]);
intent.putExtra("imageResourceId", imageId);
intent.putExtra("routeName", routeListViewItems[position]);
intent.putExtra("listViewItemPosition", listViewItemPosition);
startActivity(intent);
}
}
}
);
}
然後繼承人從第二活動傳回給ListView活動什麼IM
@Override ///////for back button///////
public void onBackPressed() {
super.onBackPressed();
////////sets checkmark next to listview item//////
if (routeCheckBox.isChecked())
{
Intent check = new Intent(RouteDetails.this,MainActivity.class);
check.putExtra("checkImageResource", R.drawable.checkmark);
check.putExtra("listViewItemPosition", listViewItemPosition);
startActivity(check);
}
}
和繼承人返回到我的listview活動中,我從我的複選框活動中接收信息,並設置對號
編輯爲包含完全適應呃類 編輯,包括我發現解決我的問題的代碼!
class CustomAdapter extends ArrayAdapter<CharSequence> {
public CustomAdapter(Context context, CharSequence[] routes) {
super(context, R.layout.custom_row ,routes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater routeInflater = LayoutInflater.from(getContext());
View customView = convertView;
if(customView == null){customView = routeInflater.inflate(R.layout.custom_row, parent, false);}
CharSequence singleRoute = getItem(position);
TextView routeText = (TextView) customView.findViewById(R.id.routeText);
routeText.setText(singleRoute);
/////////////set check mark/////////////
ImageView checkImageView = (ImageView) customView.findViewById(R.id.checkImageView);
int checkImageResourceId = ((Activity) getContext()).getIntent().getIntExtra("checkImageResource",0);
int listViewItemPosition = ((Activity) getContext()).getIntent().getIntExtra("listViewItemPosition",0);
/////my solution was just setting where listviewitemposition == position in my getview method//////
if (listViewItemPosition == position)
{
checkImageView.setImageResource(checkImageResourceId);}
//////////////////////////////////////////////////
return customView;
}
感謝您的任何幫助!
創建一個實體(SYA路線)與兩個屬性(說的名字,並檢查:布爾)和根據此實體的自定義適配器。在適配器的getView方法中實現邏輯,根據「checked」值顯示和隱藏複選框。 –
無論如何,你可以用代碼發佈一個例子嗎?我對編程真的很陌生,並且在視覺上會更容易。 – zsh5032
請在活動中粘貼更多內容「我從我的複選框活動中接收信息並設置複選標記」 – Hahn