Android 带有 CheckedTextView 的 listView,填充少量选中的行
Android listView with CheckedTextView, populate with few checked rows
我是 android 的新手,我试图在屏幕上显示带有 CheckedTextView 的列表视图,其中已经检查了很少的元素,但无法成功,我的列表视图始终未选中
这是我的代码,
final ListView listView = (ListView) findViewById(R.id.BusRoutesList);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routes_list_item, R.id.stop_checkedtextbox, routesSubList);
listView.setAdapter(arrayAdapter);
for (int i = 0; i < routesSubList.size(); i++) {
if (selectedRoutesList.contains(routesSubList.get(i))) {
//listView.setItemChecked(i,true); No Luck
View v = listView.getAdapter().getView(i, null, null);
CheckedTextView checkedTextView = (CheckedTextView) v.findViewById(R.id.stop_checkedtextbox);
checkedTextView.setChecked(true);//I get correct row to be checked but not shown on screen
}
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(R.id.stop_checkedtextbox);
boolean checked = checkedTextView.isChecked();
// String str =
if (checked) {
checkedTextView.setChecked(false);
selectedRoutesList.remove(listView.getItemAtPosition(position).toString());
} else {
selectedRoutesList.add(listView.getItemAtPosition(position).toString());
checkedTextView.setChecked(true);
}
}
});
求指教,不知道是不是漏了什么
您已经为 listView 提供了自定义布局,因此它不知道在选中某个项目时它应该如何显示。您可以尝试覆盖 getView()。
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routes_list_item, R.id.stop_checkedtextbox, routesSubList){
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.routes_list_item);
}
CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.stop_checkedtextbox);
if(listView.isItemChecked(position)){
checkedTextView.setChecked(true);
}else{
checkedTextView.setChecked(false);
}
return convertView;
}
};
代码未经测试。希望对您有所帮助!
试试这个
final ListView listView = (ListView) findViewById(R.id.BusRoutesList);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routes_list_item,R.id.stop_checkedtextbox, routesSubList);
listView.setAdapter(arrayAdapter);
View v = listView.getAdapter().getView(i, null, null);
CheckedTextView checkedTextView = (CheckedTextView)v.findViewById(R.id.stop_checkedtextbox);
for (int i = 0; i < routesSubList.size(); i++) {
if (selectedRoutesList.contains(routesSubList.get(i))){
checkedTextView.setChecked(true);
}
else {
checkedTextView.setChecked(false);
}
}
覆盖 getView() 并更新 CheckedTextview,同时预填充列表视图集
listView.setItemChecked(i, true);
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.routes_list_item, parent, false);
}
if (convertView != null) {
CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.stop_checkedtextbox);
if (listView.isItemChecked(position)) {
checkedTextView.setChecked(true);
} else {
checkedTextView.setChecked(false);
}
我是 android 的新手,我试图在屏幕上显示带有 CheckedTextView 的列表视图,其中已经检查了很少的元素,但无法成功,我的列表视图始终未选中 这是我的代码,
final ListView listView = (ListView) findViewById(R.id.BusRoutesList);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routes_list_item, R.id.stop_checkedtextbox, routesSubList);
listView.setAdapter(arrayAdapter);
for (int i = 0; i < routesSubList.size(); i++) {
if (selectedRoutesList.contains(routesSubList.get(i))) {
//listView.setItemChecked(i,true); No Luck
View v = listView.getAdapter().getView(i, null, null);
CheckedTextView checkedTextView = (CheckedTextView) v.findViewById(R.id.stop_checkedtextbox);
checkedTextView.setChecked(true);//I get correct row to be checked but not shown on screen
}
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(R.id.stop_checkedtextbox);
boolean checked = checkedTextView.isChecked();
// String str =
if (checked) {
checkedTextView.setChecked(false);
selectedRoutesList.remove(listView.getItemAtPosition(position).toString());
} else {
selectedRoutesList.add(listView.getItemAtPosition(position).toString());
checkedTextView.setChecked(true);
}
}
});
求指教,不知道是不是漏了什么
您已经为 listView 提供了自定义布局,因此它不知道在选中某个项目时它应该如何显示。您可以尝试覆盖 getView()。
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routes_list_item, R.id.stop_checkedtextbox, routesSubList){
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.routes_list_item);
}
CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.stop_checkedtextbox);
if(listView.isItemChecked(position)){
checkedTextView.setChecked(true);
}else{
checkedTextView.setChecked(false);
}
return convertView;
}
};
代码未经测试。希望对您有所帮助!
试试这个
final ListView listView = (ListView) findViewById(R.id.BusRoutesList);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.routes_list_item,R.id.stop_checkedtextbox, routesSubList);
listView.setAdapter(arrayAdapter);
View v = listView.getAdapter().getView(i, null, null);
CheckedTextView checkedTextView = (CheckedTextView)v.findViewById(R.id.stop_checkedtextbox);
for (int i = 0; i < routesSubList.size(); i++) {
if (selectedRoutesList.contains(routesSubList.get(i))){
checkedTextView.setChecked(true);
}
else {
checkedTextView.setChecked(false);
}
}
覆盖 getView() 并更新 CheckedTextview,同时预填充列表视图集
listView.setItemChecked(i, true);
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.routes_list_item, parent, false);
}
if (convertView != null) {
CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.stop_checkedtextbox);
if (listView.isItemChecked(position)) {
checkedTextView.setChecked(true);
} else {
checkedTextView.setChecked(false);
}