带有CheckedTextView的Android listView,填充少量选中的行

时间:2016-09-28 20:26:47

标签: android listview checked checkedtextview

我是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);
                }
            }
        });

请建议,我不知道我是否遗漏了某些东西

3 个答案:

答案 0 :(得分:0)

您已经为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;
        }
    };

代码未经过测试。希望它有所帮助!

答案 1 :(得分:0)

尝试这个

 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);
             }
       }

答案 2 :(得分:0)

覆盖getView()并更新CheckedTextview,同时预填充listview set

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);
            }