简体   繁体   English

复选框在多选ListView上不可见

[英]Checkbox not visible on multiple choice ListView

I want my listview to show checkboxes. 我希望列表视图显示复选框。 Currently, it just acts as a normal listView, as in it renders only the items properly and the only issue is the missing checkbox. 当前,它只是充当普通的listView,因为它只能正确渲染项目,唯一的问题是缺少复选框。 I'm using a custom Adapter. 我正在使用自定义适配器。

I'm pasting the relevant code here, I can share more if needed, StackOverflow tells me there's too much code if I just paste the entire thing. 我在这里粘贴了相关的代码,如果需要,我可以共享更多,StackOverflow告诉我,如果我粘贴整个内容,则代码太多。

Ps. PS。 Beginner at android. android的初学者。

In my public class MainActivity extends ListActivity 在我的公共课堂上,MainActivity扩展了ListActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setListAdapter(new GroceryAdapter(new ArrayList<>(Arrays.asList(arr)), getApplicationContext()));
        ListView list = getListView();
        list.setItemChecked(2, true);
    }

In my class GroceryAdapter extends ArrayAdapter 在我的课程中,GroceryAdapter扩展了ArrayAdapter

private static class ViewHolder {
        TextView txtName;
        TextView txtCost;
    }

public GroceryAdapter(ArrayList<GroceryItem> data, Context x) {
        super(x, android.R.layout.simple_list_item_multiple_choice, data);
        this.data = data;
        this.x = x;
    }

public View getView(int pos, View convertView, ViewGroup parent) {
        GroceryItem item = getItem(pos);
        ViewHolder viewHolder;
        final View result;

        if(convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.row_item, parent, false);
            viewHolder.txtName = convertView.findViewById(R.id.name);
            viewHolder.txtCost = convertView.findViewById(R.id.cost);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.txtName.setText(item.name);
        viewHolder.txtCost.setText(String.format("Rs.%s", String.valueOf(item.cost)));
        result = convertView;
        return result;
    }

activity_main.xml activity_main.xml中

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/list"
    android:choiceMode="multipleChoice"
/>

row_item.xml row_item.xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/name"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/cost"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"/>

You're actually inflating the view you want shown for each item in this line: 您实际上是在扩大要在此行中每个项目显示的视图:

convertView = inflater.inflate(R.layout.row_item, parent, false);

Your row_item layout only has 2 TextViews so it'll inflate that layout for every item, and since there aren't any checkboxes in it, that's the reason you don't see them. 您的row_item布局仅具有2个TextViews因此它将为每个项目增加该布局,并且由于其中没有任何复选框,因此您没有看到它们。 Just change that layout to include a checkbox wherever you want it and it'll be there. 只需更改该布局即可在您需要的位置添加一个复选框,该复选框将在其中。

Either this or change the layout you're inflating, I think there should be some pre-made Android ones to use. 要么更改您要夸大的布局,要么我认为应该使用一些预制的Android。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM