Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何同时选择两个列表项?具体来说,只有两项不超过该项_Android_Android Layout_Listview_Listviewitem - Fatal编程技术网

Android 如何同时选择两个列表项?具体来说,只有两项不超过该项

Android 如何同时选择两个列表项?具体来说,只有两项不超过该项,android,android-layout,listview,listviewitem,Android,Android Layout,Listview,Listviewitem,我有一个列表视图,如下图所示。 现在,我想一次只从列表视图中选择任何两个项目,并将两个列表视图项目的值传递给下一个活动。我怎样才能做到 如果两个项目均未选中,是否对其设置验证 适配器类 我会在MultichoiceModelListener中使用动作模式 有一个回调onItemCheckedStateChanged(ActionMode模式,int位置,布尔值选中) 您可以轻松获取已选中列表项的值ListView.getCheckedItemPositions。因此,如果您已经选择了两个或甚

我有一个列表视图,如下图所示。

现在,我想一次只从列表视图中选择任何两个项目,并将两个列表视图项目的值传递给下一个活动。我怎样才能做到

如果两个项目均未选中,是否对其设置验证

适配器类


我会在
MultichoiceModelListener
中使用动作模式

有一个回调
onItemCheckedStateChanged(ActionMode模式,int位置,布尔值选中)


您可以轻松获取已选中列表项的值
ListView.getCheckedItemPositions
。因此,如果您已经选择了两个或甚至一次不费吹灰之力地传递所选的值(或ID),则可以阻止选择。

我将使用动作模式和
MultichoiceModelListener

有一个回调
onItemCheckedStateChanged(ActionMode模式,int位置,布尔值选中)


您可以轻松获取已选中列表项的值
ListView.getCheckedItemPositions
。因此,如果您已经选择了两个,或者甚至可以一次不费吹灰之力地传递所选项目的值(或ID),则可以阻止选择。

您只需在项目选择上放置一个计数器。

用一个变量来表示ex

int count = 0;
在将项目标记为选中/取消选中之前,请选中此变量:

if(item.isSelected())
{
   // you need to make is disable
   if(count>0)
   {
      count--;
      // // mark item as deselected
   }
}
else
{
   // make it selected
     if(count<2)
     {
        count++;
        // mark item as selected
     }
}
问题2的答案:如果要使用ImageView而不是文本颜色更改。执行与上面相同的操作,但将文本视图+颜色替换为图像视图+图像

编辑3:

您已经从Null对象调用了
getSelected()
方法。按如下方式修改代码:

public class LoadAdapter extends BaseAdapter {
private ArrayList<DataBase> mProductItems;
private LayoutInflater mLayoutInflater;
private Context mContext;
DBHelper mydb;
DataBase stringItem;
ArrayList<Boolean> selected = new ArrayList<>();
private int count=0;

public LoadAdapter(Context context, ArrayList<DataBase> arrayList){
    mContext = context;
    mProductItems = arrayList;
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < arrayList.size(); i++) {

        selected.add(false);

    }
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    Log.e("testtt", String.valueOf(mProductItems.size()));
    return mProductItems.size();

}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}



@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mLayoutInflater.inflate(R.layout.load_chart_item, parent, false);
        holder.txtv_name = (TextView) convertView.findViewById(R.id.text);
        holder.nameid = (TextView) convertView.findViewById(R.id.nameid);
        holder.btn_delete = (Button) convertView.findViewById(R.id.btn_delete);
        holder.btn_edit = (Button)convertView.findViewById(R.id.btn_edit);
        holder.location = (TextView)convertView.findViewById(R.id.loc);
        holder.btn_delete.setTag(position);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    mydb = new DBHelper(mContext);

    stringItem = mProductItems.get(position);

    if (stringItem != null) {
        if (holder.txtv_name != null) {

            holder.txtv_name.setText(stringItem.getName());
            holder.nameid.setText(stringItem.getId());
            holder.location.setText(stringItem.getLocation());
            Log.e("saved Location  values",stringItem.getLocation());
        }
    }

    if(selected.get(position))
    {
        //for selected row
        holder.txtv_name.setTextColor(color.red);
    }
    else
    {
        // for not selected row
        holder.txtv_name.setTextColor(color.black);
    }

    holder.txtv_name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(holder.txtv_name.getCurrentTextColor()== R.color.black)
            {
                //is not selected
                if(count<2)
                {
                    count++;
                    selected.set(position,true);

                    // mark item as selected
                }
            }
            else
            {
                //is selected
                if(count>0)
                {
                    count--;
                    selected.set(position,false);

                    // // mark item as deselected
                }
            }
            notifyDataSetChanged();
        }
    });


    holder.btn_delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            DataBase test = mProductItems.get(position);
            String id =  test.getId();

            mydb.deleteContact(Integer.valueOf(id));
            mProductItems.remove(mProductItems.get(position));
            LoadAdapter.this.notifyDataSetChanged();


            if (mProductItems.size() == 0){
                mProductItems.clear();
                LoadAdapter.this.notifyDataSetChanged();
            }



        }
    });




    Log.e("DataBase", String.valueOf(mydb.getAllCotacts()));

    holder.btn_edit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent=new Intent(mContext,UpdateData.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            SharedPreferences preff = PreferenceManager.getDefaultSharedPreferences(mContext);
            SharedPreferences.Editor edi = preff.edit();
            edi.putString("key",String.valueOf(position+1));
            edi.apply();

            mContext.startActivity(intent);
        }
    });

    return convertView;
}

public void refresh(ArrayList<DataBase> items)
{
    this.mProductItems = items;
    notifyDataSetChanged();
}


private static class ViewHolder {
    TextView txtv_name,nameid,location;
    Button btn_delete,btn_edit;
}}
 listView = (SwipeListView) findViewById(R.id.listview);
 loadAdapter = new LoadAdapter(getApplicationContext(),array_list);
    listView.setAdapter(loadAdapter);


    det.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String str = "";
        str =  loadAdapter.getSelected();
        Toast.makeText(RelationShipChartList.this, str, Toast.LENGTH_SHORT).show();

    }

您只需在物品选择上放置一个计数器即可。

用一个变量来表示ex

int count = 0;
在将项目标记为选中/取消选中之前,请选中此变量:

if(item.isSelected())
{
   // you need to make is disable
   if(count>0)
   {
      count--;
      // // mark item as deselected
   }
}
else
{
   // make it selected
     if(count<2)
     {
        count++;
        // mark item as selected
     }
}
问题2的答案:如果要使用ImageView而不是文本颜色更改。执行与上面相同的操作,但将文本视图+颜色替换为图像视图+图像

编辑3:

您已经从Null对象调用了
getSelected()
方法。按如下方式修改代码:

public class LoadAdapter extends BaseAdapter {
private ArrayList<DataBase> mProductItems;
private LayoutInflater mLayoutInflater;
private Context mContext;
DBHelper mydb;
DataBase stringItem;
ArrayList<Boolean> selected = new ArrayList<>();
private int count=0;

public LoadAdapter(Context context, ArrayList<DataBase> arrayList){
    mContext = context;
    mProductItems = arrayList;
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < arrayList.size(); i++) {

        selected.add(false);

    }
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    Log.e("testtt", String.valueOf(mProductItems.size()));
    return mProductItems.size();

}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}



@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mLayoutInflater.inflate(R.layout.load_chart_item, parent, false);
        holder.txtv_name = (TextView) convertView.findViewById(R.id.text);
        holder.nameid = (TextView) convertView.findViewById(R.id.nameid);
        holder.btn_delete = (Button) convertView.findViewById(R.id.btn_delete);
        holder.btn_edit = (Button)convertView.findViewById(R.id.btn_edit);
        holder.location = (TextView)convertView.findViewById(R.id.loc);
        holder.btn_delete.setTag(position);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    mydb = new DBHelper(mContext);

    stringItem = mProductItems.get(position);

    if (stringItem != null) {
        if (holder.txtv_name != null) {

            holder.txtv_name.setText(stringItem.getName());
            holder.nameid.setText(stringItem.getId());
            holder.location.setText(stringItem.getLocation());
            Log.e("saved Location  values",stringItem.getLocation());
        }
    }

    if(selected.get(position))
    {
        //for selected row
        holder.txtv_name.setTextColor(color.red);
    }
    else
    {
        // for not selected row
        holder.txtv_name.setTextColor(color.black);
    }

    holder.txtv_name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(holder.txtv_name.getCurrentTextColor()== R.color.black)
            {
                //is not selected
                if(count<2)
                {
                    count++;
                    selected.set(position,true);

                    // mark item as selected
                }
            }
            else
            {
                //is selected
                if(count>0)
                {
                    count--;
                    selected.set(position,false);

                    // // mark item as deselected
                }
            }
            notifyDataSetChanged();
        }
    });


    holder.btn_delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            DataBase test = mProductItems.get(position);
            String id =  test.getId();

            mydb.deleteContact(Integer.valueOf(id));
            mProductItems.remove(mProductItems.get(position));
            LoadAdapter.this.notifyDataSetChanged();


            if (mProductItems.size() == 0){
                mProductItems.clear();
                LoadAdapter.this.notifyDataSetChanged();
            }



        }
    });




    Log.e("DataBase", String.valueOf(mydb.getAllCotacts()));

    holder.btn_edit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent=new Intent(mContext,UpdateData.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            SharedPreferences preff = PreferenceManager.getDefaultSharedPreferences(mContext);
            SharedPreferences.Editor edi = preff.edit();
            edi.putString("key",String.valueOf(position+1));
            edi.apply();

            mContext.startActivity(intent);
        }
    });

    return convertView;
}

public void refresh(ArrayList<DataBase> items)
{
    this.mProductItems = items;
    notifyDataSetChanged();
}


private static class ViewHolder {
    TextView txtv_name,nameid,location;
    Button btn_delete,btn_edit;
}}
 listView = (SwipeListView) findViewById(R.id.listview);
 loadAdapter = new LoadAdapter(getApplicationContext(),array_list);
    listView.setAdapter(loadAdapter);


    det.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String str = "";
        str =  loadAdapter.getSelected();
        Toast.makeText(RelationShipChartList.this, str, Toast.LENGTH_SHORT).show();

    }


使用
复选框
列表视图
将更容易做到这一点。如果我使用自己的图标而不是复选框怎么办?如果您使用您的图标,请确保它将是一个选择器。我建议您最好选中
复选框
。实际上我只需要设置我的图标?我该怎么做?制作自定义列表项,如
图像视图
图标和
文本视图
文本。图标使其可绘制。使用
列表视图选中此使用
复选框
,这样做会更容易。如果我使用自己的图标而不是复选框怎么办?如果您使用图标,请确保它是一个选择器。我建议您最好选中
复选框
。实际上我只需要设置我的图标?我该怎么做?制作自定义列表项,如
图像视图
图标和
文本视图
文本。图标使其可绘制。检查一下,你能告诉我这是什么吗?我不知道你的密码。因此,我刚刚给出了一个示例作为项目。项目可以是您的复选框,文本查看您正在使用的任何内容。如果你想让我解释一下你将要把我的建议放在哪里,请分享你的Adpeter代码。我想把这个设置为完整的listview行吗?如何设置你正在设置背景色??单击列表中的项目时,您的屏幕截图与github上的项目链接似乎不匹配。通过编辑问题共享您的适配器代码。您能告诉我这里的项目是什么吗?我不知道您的代码。因此,我刚刚给出了一个示例作为项目。项目可以是您的复选框,文本查看您正在使用的任何内容。如果你想让我解释一下你将要把我的建议放在哪里,请分享你的Adpeter代码。我想把这个设置为完整的listview行吗?如何设置你正在设置背景色??单击列表中的项目时,您的屏幕截图与github上的项目链接似乎不匹配。通过编辑问题共享适配器代码。