如何在列表视图中选择多个值?

时间:2016-11-24 10:11:49

标签: android listview baseadapter

说明:             我有一个对话框。其中,我有listview,其中所有数据都被设置到列表视图中并显示在对话框中。我可以从列表视图中选择多个值,然后单击“应用”按钮,我获得了所选的值数据。

问题

问题是当我再次打开对话框时,我的代码中没有选择我之前的选择。

我想要什么?

当我再次打开对话框时,我在列表视图项目中仍然选择了我之前的选择。我怎么能这样做?

item_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#333044">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/ll_btn_actions">
        <ListView
            android:id="@+id/item_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_btn_actions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/cancel"
            android:background="@android:color/transparent"
            android:textColor="@color/white"
            android:textSize="@dimen/meet_details_fs"
            android:gravity="center"
            android:textAllCaps="false"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/btn_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/apply"
            android:background="@android:color/transparent"
            android:textColor="@color/white"
            android:textSize="@dimen/meet_details_fs"
            android:gravity="center"
            android:textAllCaps="false"
            android:layout_weight="1"/>
    </LinearLayout>
</RelativeLayout>

当我按下按钮时,对话框将打开,此处是打开对话框的代码。

public void setitemDialog(final Activity activity, final List<Brand> compactStoreItemList) {
        itemDialog = new Dialog(activity);
        itemDialog.setContentView(R.layout.item_dialog);
        itemDialog.setCancelable(true);
        itemDialog.setTitle("Select Brands");
        itemDialog.setCanceledOnTouchOutside(true);

        Button btnSubmit = (Button) itemDialog.findViewById(R.id.btn_submit);
        Button btnCancel=(Button)itemDialog.findViewById(R.id.btn_cancel);

        final ListView itemList = (ListView) itemDialog.findViewById(R.id.item_list);
        final BrandAdapter adapter = new BrandAdapter(activity, compactStoreItemList, prefManager);
        itemList.setAdapter(adapter);

        itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selectItem(position);
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemDialog.isShowing()) {
                    itemDialog.dismiss();
                }
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                brandList = new ArrayList<>();
                String brandNames = "";
                for (Brand a : adapter.getBox()) {
                    if (a.isChecked) {
                        Log.e(TAG, "BRAND IDS=>" + a.getId());
                        brandList.add(a);
//                        tempArr.add(a);
                        brandNames += a.getBrandName() + ",";
                    }
                }
//                for(Brand b:tempArr){
//
//                    brandNames += b.getBrandName() + ",";
//                }
                spiBrand.setText(brandNames);
                if (itemDialog.isShowing()) {
                    itemDialog.dismiss();
                }
            }
        });
        adapter.notifyDataSetChanged();
        itemDialog.show();
    }

下面是对话框调用的适配器,并将值设置为listview。

public class BrandAdapter extends BaseAdapter {

    public static final String TAG=BrandAdapter.class.getSimpleName();

    public Activity activity;
    private List<Brand> brandList;
    private LayoutInflater inflater;
    PreferenceSetting prefManager;

    public BrandAdapter(Activity activity, List<Brand> brandList, PreferenceSetting preferenceSetting){
        this.activity=activity;
        this.brandList=brandList;
        prefManager=PreferenceSetting.getInstance(activity);
        inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return brandList.size();
    }

    @Override
    public Object getItem(int position) {
        return brandList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void selectItem(int position){
        brandList.get(position).isChecked = !brandList.get(position).isChecked;
        notifyDataSetChanged();
    }
    public ArrayList<Brand> getBox() {
        ArrayList<Brand> box = new ArrayList<>();
        for (Brand p : brandList) {
            if (p.isChecked)
                box.add(p);
        }
        return box;
    }
    private class Holder{
        TextView brandName;
        CheckBox checkBox;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        Holder holder;
        if(convertView==null){
            holder=new Holder();
            convertView=inflater.inflate(R.layout.ac_problems_list,parent,false);

            holder.brandName=(TextView)convertView.findViewById(R.id.txt_cooling);
            holder.checkBox=(CheckBox)convertView.findViewById(R.id.cb_cooling);

            convertView.setTag(holder);
        }
        else{
            holder=(Holder)convertView.getTag();
        }

        final Brand brand=brandList.get(position);
        holder.brandName.setText(brand.getBrandName());
//        for(Brand brand1:MeetingActivity.tempArr){
//            if(brand.getId()==brand1.getId()){
//                holder.checkBox.setChecked(brand1.isChecked);
//                Log.e(TAG, "IS CHECKED=>" + brand.isChecked);
//            }
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectItem(position);
            }
        });
        return convertView;
    }
}

Brand.java

public class Brand {

    public static final String BRAND_ID="id";
    public static final String BRAND_NAME="brand_name";
    public static final String USER_ID="user_id";
    public static final String DELETE_STATUS="delete_status";
    public static final String DISABLE_STATUS="disable_status";
    public static final String CREATED_ON="created_on";

    private int id;
    private String brandName;
    private String userId;
    private String deleteStatus;
    private String disableStatus;
    private String createdOn;
    public boolean isChecked = false;

    public Brand(){}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getDeleteStatus() {
        return deleteStatus;
    }

    public void setDeleteStatus(String deleteStatus) {
        this.deleteStatus = deleteStatus;
    }

    public String getDisableStatus() {
        return disableStatus;
    }

    public void setDisableStatus(String disableStatus) {
        this.disableStatus = disableStatus;
    }

    public String getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(String createdOn) {
        this.createdOn = createdOn;
    }
}

我怎么能这样做?请帮帮我。

2 个答案:

答案 0 :(得分:0)

在你的getView方法中,你应该检查你的品牌对象上的isChecked并在那里实现逻辑。 (根据其isChecked值显示正确的设计状态)

我已经做了类似的事情,在这里:

@Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        Contract currentContract = mDataset.get(position);
    if (app.isContractSelected()) {
                if (currentContract.getContractNr() == app.getSelectedContract().getContractNr()) {
                    contractViewHolder.selector.setVisibility(View.VISIBLE);
                    contractViewHolder.listItem.setBackgroundColor(ContextCompat.getColor(mContext, R.color.veryLightGrey));
                } else {
                    contractViewHolder.selector.setVisibility(View.INVISIBLE);
                    TypedValue outValue = new TypedValue();
                    mContext.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
                    contractViewHolder.listItem.setBackgroundResource(outValue.resourceId);
                }
            }}

答案 1 :(得分:0)

您可以在对话框关闭时(自然地)在SharedPreferences中保存复选框的状态,然后再次重新启动对话框时,您需要从SharedPreferences中读取这些值,然后在初始化对话框。您可以在共享首选项中放置布尔值,然后在创建对话框时再次读取它们。