android 多选ListView & Textview

2024-05-29

我想创建如下所示的列表视图。

复选框 |文本视图 |文本视图 |文本视图 |文本视图 |文本视图 |文本视图 |

所有数据都来自数据库并设置在文本视图中。

我尝试过,但遇到了一些问题

  1. 当我选择复选框和滚动列表时,它会自动取消选中。

  2. 如果我选择第一个复选框,则会自动选中第五个复选框。

  3. 全部选中并取消全部选中

请帮助我,我尝试了很多代码,但没有成功。

提前致谢。


它可能会帮助你。

package com.Sample_MultipleSelection;

import java.util.ArrayList;

import android.R.integer;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class Sample_MultipleSelectionActivity extends ListActivity {

    private MyListAdapter adapter;
    ArrayList<String> item_id = new ArrayList<String>();
    ArrayList<String> item_name = new ArrayList<String>();
    ArrayList<String> item_balance = new ArrayList<String>();
    ArrayList<String> item_email = new ArrayList<String>();

    ArrayList<String> items = new ArrayList<String>();

    private CheckBox chk_main;
    boolean flag = false;
    boolean[] selection;
    ArrayList<String> selection_val;
    private Button btn_select;
    private CheckBox chk_select;
    ViewHolder holder12;
    boolean select_all;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        item_id.add("1");
        item_name.add("China");
        item_balance.add("4000");
        item_email.add("[email protected] /cdn-cgi/l/email-protection");

        item_id.add("2");
        item_name.add("abc");
        item_balance.add("4000");
        item_email.add("[email protected] /cdn-cgi/l/email-protection");

        item_id.add("3");
        item_name.add("xyz");
        item_balance.add("4000");
        item_email.add("[email protected] /cdn-cgi/l/email-protection");

        item_id.add("4");
        item_name.add("xyza");
        item_balance.add("40070");
        item_email.add("[email protected] /cdn-cgi/l/email-protection");

        item_id.add("5");
        item_name.add("xyzc");
        item_balance.add("1000");
        item_email.add("[email protected] /cdn-cgi/l/email-protection");

        final ViewHolder holder = new ViewHolder();
        selection = new boolean[item_id.size()];
        selection_val = new ArrayList<String>();

        adapter = new MyListAdapter(this);
        setListAdapter(adapter);

        holder12 = new ViewHolder();


        btn_select = (Button) findViewById(R.id.button1);
        btn_select.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                int len = selection.length;
                int cnt = 0;
                String selectIds = "";
                for (int i = 0; i < len; i++) {
                    if (selection[i]) {
                        cnt++;
                    }
                }
                for (int i = 0; i < selection_val.size(); i++) {
                    selectIds = selectIds + " | " + selection_val.get(i);
                }
                if (cnt == 0) {
                    Toast.makeText(getApplicationContext(), "NO Selection", 1)
                            .show();
                } else {
                    Toast.makeText(
                            getApplicationContext(),
                            "Your are Selected   " + cnt + " ids. " + " "
                                    + selectIds, 1).show();
                }
            }
        });
    }

    public class MyListAdapter extends BaseAdapter {
        Context con;
        private LayoutInflater layoutinf;
        ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
        ArrayList<String> items_ = new ArrayList<String>();

        public MyListAdapter(
                Sample_MultipleSelectionActivity sample_MultipleSelectionActivity) {
            con = sample_MultipleSelectionActivity;
        }

        public int getCount() {
            return item_id.size();
        }

        public Object getItem(int arg0) {
            return item_id.size();
        }

        public long getItemId(int arg0) {
            return item_id.get(arg0).hashCode();
        }

        public View getView(final int arg0, View arg1, ViewGroup arg2) {

            View v = arg1;
            ViewHolder holder = null;
            if (v == null) {
                layoutinf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = layoutinf.inflate(R.layout.row, null);
                holder = new ViewHolder();
                holder.chk = (CheckBox) v.findViewById(R.id.checkBox1);
                holder.tv_name = (TextView) v.findViewById(R.id.textView1);
                holder.tv_bal = (TextView) v.findViewById(R.id.textView2);
                holder.tv_email = (TextView) v.findViewById(R.id.textView3);

                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }

            holder.chk.setId(arg0);
            holder.tv_name.setId(arg0);

            holder.chk.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                    try {
                        CheckBox cb = (CheckBox) v;
                        int id = cb.getId();
                        String val = cb.getText().toString();
                        if (selection[id]) {
                            cb.setChecked(false);
                            selection[id] = false;
                            selection_val.remove("" + val);
                        } else {
                            cb.setChecked(true);
                            selection[id] = true;
                            selection_val.add("" + val);
                        }
                        adapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        Log.e("error", "" + e.toString());
                    }
                }
            });
            holder.chk.setChecked(selection[arg0]);

            if(selection[arg0] == true)
            {
                holder.tv_name.setBackgroundColor(Color.GRAY);
                holder.tv_bal.setBackgroundColor(Color.GRAY);
                holder.tv_email.setBackgroundColor(Color.GRAY);
            }
            else
            {
                holder.tv_name.setBackgroundColor(Color.TRANSPARENT);
                holder.tv_bal.setBackgroundColor(Color.TRANSPARENT);
                holder.tv_email.setBackgroundColor(Color.TRANSPARENT);
            }

            holder.chk.setText(item_id.get(arg0));
            holder.tv_name.setText("" + item_name.get(arg0));
            holder.tv_bal.setText(item_balance.get(arg0));
            holder.tv_email.setText(item_email.get(arg0));

            return v;
        }
    }

    public class ViewHolder {
        private CheckBox chk;
        private TextView tv_name;
        private TextView tv_bal;
        private TextView tv_email;

    }
}

main.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/chk_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"

            android:text="" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#66cc33" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Balance"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#66cc33" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Email-Id"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#66cc33" />
    </LinearLayout>

    <View
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="2dp"
        android:background="#fff" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="314dp"
        android:layout_weight="1" >
    </ListView>

    <Button
        android:id="@+id/button1"
        android:focusable="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="select" />

    </LinearLayout>

row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:orientation="horizontal">


                         <CheckBox
                            android:id="@+id/checkBox1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"                            
                            android:textColor="#000" />



                        <TextView
                            android:id="@+id/textView1"
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"                         
                             android:padding="3dp"
                            android:textAppearance="?android:attr/textAppearanceMedium" />

                        <TextView
                            android:padding="3dp"
                            android:id="@+id/textView2"
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"                         
                            android:textAppearance="?android:attr/textAppearanceMedium" />

                        <TextView
                            android:padding="3dp"
                            android:id="@+id/textView3"
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"                         
                            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

如果正确的话就改正它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android 多选ListView & Textview 的相关文章

随机推荐

  • Android Studio - 程序类型已存在:org.hamcrest.CoreMatchers

    我不知道为什么会出现这个错误 Program type already present org hamcrest CoreMatchers Message kind ERROR text Program type already prese
  • 在ggplot2中创建部分虚线

    我正在 R 中创建一个图 并且需要创建一条线 其中某些值是投影 投影用虚线表示 这是代码 df data frame date c rep 2008 2013 by 1 value c 303 407 538 696 881 1094 gg
  • JWT(Json Web 令牌)与自定义令牌

    我仔细查看了问题 但没有找到任何可以解决我的疑问的内容 我找到了有关 JWT 的大量信息 但在比较 JWT 相对于针对 REST 服务生成自定义令牌来对身份验证请求提供的优势时 发现的信息并不多 与生成自定义生成令牌相比 使用 JWT Js
  • 在Python中通过套接字发送文件

    我正在尝试用 python 编写一个实现套接字的程序 每个客户端发送一个 PDF 文件 服务器接收该文件并将标题更改为 file number pdf 例如 file 1 pdf 出现的问题是只有客户端才能成功发送文件 当第二个客户端尝试发
  • SVG feColorMatrix 在 safari 中不起作用

    我有一个相当简单的设置 我想通过使用 svg 过滤器来改变 svg 图像的颜色
  • 如何在arm64主机上运行amd64 docker镜像

    警告 请求的映像平台 linux amd64 与检测到的主机平台 linux arm64 v8 不匹配 并且未请求特定平台 2021 07 28 22 25 06 349222 F tensorflow core platform cpu
  • 对象返回时是否保证被移动?

    我知道 当将对象按值传递给函数时 如果存在移动构造函数 则始终会调用移动构造函数 假设没有复制省略 按值返回对象怎么样 例如 假设我们有一堂课Foo它有一个移动构造函数 我们有一个返回一个的函数Foo object Foo g Foo f
  • 如何安装 scipy 杂项包

    我已经安装 实际上是重新安装 scipy 10 x86 64 whl 19 8MB 19 8MB downloaded Installing collected packages scipy Successfully installed s
  • URL::forceSchema 在登录前不起作用

    我在用着URL forceSchema https 在我的 Laravel 5 3 应用程序上强制使用 SSL 然而 它只有在登录后才有效 即使登录页面没有被 https 覆盖 我在用着可信代理 https github com fidel
  • 验证项目是否在开始日期和结束日期内

    我有一个java程序 它将检查每个项目的开始日期和结束日期 每个项目必须有自己特定的开始日期和结束日期范围 如果新的开始日期和结束日期的范围落在旧的开始日期和结束日期内 系统将提示错误消息 例如 Company ABC Item Numbe
  • typescript 中的重载签名和实现签名

    我正在阅读 Typescript 手册 我很难理解为什么以下代码片段有错误消息 function fn x string void function fn vo Expected to be able to call with zero a
  • 级联删除时触发调用

    我在 MySQL 中有表 A 它有一些对其他表 B C D 的级联删除的引用 当从 A 中删除某些内容时 我需要使用触发器 当我直接从 A 删除记录时 此触发器起作用 但它不适用于级联删除 是否存在任何版本的 MySQL 可以让我的触发器与
  • 通过 PDO 将双精度数插入 MySQL 时精度损失

    我遇到了这种非常烦人的行为 我想知道我是否做错了什么 或者这是否是故意的 如果是的话 为什么 每当我在 php 5 3 中有一个 double 类型的变量 并且想将其插入到数据库 MYSQL 5 0 的 double 类型字段中时 该值总是
  • 从函数返回随机值是副作用吗?

    我当时正在编写一些 F 代码 并且正在编写一个从一组字符串中返回随机字符串的函数 假设我有这样的事情 open System let a a b c d let rstring arr string let r new Random arr
  • 如何使用 C# 在 MS Excel 单元格中添加数字验证

    我的目标是限制用户在 MS Excel 单元格中仅输入 1 到 100 范围内的值 我正在以编程方式生成 Excel 文件 但是当我添加上述验证时 抛出异常Exception from HRESULT 0x800A03EC 我写的代码如下
  • 从轨道控制器返回

    这是一个初学者 Rails 问题 我这样做之后 format xml head ok 如何从控制器端点返回而不显示视图 如果我此时放弃函数的末尾 我会得到我所期望的结果 但如果我调用 返回 我最终会进入视图 或者在我的情况下会出现缺少的视图
  • R:在 Shiny 中,如何修复应用于“反应性”类对象的“xtable”没有适用的方法

    我收到此错误 Error in UseMethod xtable no applicable method for xtable applied to an object of class reactive UI R library shi
  • 如何从注释处理器中的嵌套注释读取 Class[] 值

    我正在尝试使用Java注释处理工具生成一些代码 我有嵌套注释 其中父注释值是子注释的数组 子注释值是类的数组 注释 public interface ParentAnnotation ChildAnnotation value public
  • 避免 UAC 但使用 Windows 服务启动提升的进程

    我有一个非交互式服务作为 Windows 计算机上的特权系统用户运行 我需要它来启动给定的可执行文件作为提升的进程 我已设法使用 WTSGetActiveConsoleSessionId 以 SYSTEM 身份启动一个子进程 找到一个系统进
  • android 多选ListView & Textview

    我想创建如下所示的列表视图 复选框 文本视图 文本视图 文本视图 文本视图 文本视图 文本视图 所有数据都来自数据库并设置在文本视图中 我尝试过 但遇到了一些问题 当我选择复选框和滚动列表时 它会自动取消选中 如果我选择第一个复选框 则会自