是否可以使用文本文件构建listview&;android中的复选框?

是否可以使用文本文件构建listview&;android中的复选框?,android,Android,所有这些数据都必须来自一个数据库,而且我没有固定数量的字段,所以我可以使用什么方法来构建图像中的listView 非常感谢。您可以在getView中创建自定义适配器并为自己的物品充气,如下所示 cell_view.xml <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:te

所有这些数据都必须来自一个数据库,而且我没有固定数量的字段,所以我可以使用什么方法来构建图像中的listView


非常感谢。

您可以在getView中创建自定义适配器并为自己的物品充气,如下所示

cell_view.xml

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sample text"/>

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.cell_view, parent, false);
    CheckBox checkBox = (CheckBox) rowView.findViewById(R.id.checkbox);
    checkBox.setText("MY TEXT GOES HERE");// Update Your text
    checkBox.setChecked(true);// Set checked/unchecked based on your functionality.
    return rowView;
  }

@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(R.layout.cell\u视图,父视图,false);
CheckBox=(CheckBox)rowView.findviewbyd(R.id.CheckBox);
checkBox.setText(“我的文本在这里”);//更新文本
checkBox.setChecked(true);//根据您的功能设置checked/unchecked。
返回行视图;
}

我编写了一个与上表非常相似的程序。结果在模拟器上显示如下:

要实现它,您需要创建一个自定义的
ListAdapter
。在适配器内部,可以重写
getView()
,为行项目创建布局。如果希望结果像在我的图像中一样显示,还需要两个XML“布局”文件:一个作为单个行的模板,另一个作为整个
列表视图的宿主。如果您不想要永久的标题,那么您可以只使用一行布局文件。该活动如下所示:

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Helps create fake ISBN and quantity numbers
    Random rand = new Random();

    //Arrays to store fake Book Names, ISBN numbers and Quantity Numbers
    final ArrayList<String> bookNames = new ArrayList<>();
    final ArrayList<String> code = new ArrayList<>();
    final ArrayList<String> quantity = new ArrayList<>();

    // You can remove this if you don't want the permanent headers
     setContentView(R.layout.activity_main);

    //This populates our arrays with the fake names and numbers, so there is something to
    //display in the list
    for (int i = 1; i < 30; i++){
        bookNames.add("Book No: " + i);
        code.add(String.valueOf((rand.nextInt(100) * i) + 1000));
        quantity.add(String.valueOf(rand.nextInt(5) + 1));
    }

    setListAdapter(new ListAdapter() {
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Value:"
        android:textStyle="bold"
        android:id="@+id/textView"
        android:textSize="18sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:id="@+id/editText"
        android:layout_gravity="center_horizontal"
        android:textSize="16sp" />

</LinearLayout>
convertView是否为null的区别在于确定列表是第一次加载,还是在用户滚动时填充。各种TextView对象由数组中位于行的指定
位置的任何项填充。以下是行布局的XML文件:

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

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox"
    android:checked="true" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/nameTV"
    android:layout_weight="1" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/codeTV"
    android:layout_weight="1" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/quantityTV"
    android:layout_weight="1" />

我刚刚在标题中创建了一个
不可见的
复选框
,以确保间距一致,但如果您愿意,可以删除此复选框,并在第一个
文本视图
项中添加一个
android:layout\u marginLeft
属性

编辑:

添加更改
数量
数字的功能非常简单-只需将
行.xml中的
元素更改为
。但是,问题是,每次从页面上滚动该行时,
列表视图将自动刷新原始值并删除编辑内容。这只是ListView节省内存策略的一部分

要保留它,我们需要将新值保存到数组中。我这样做的方法是使用一个弹出的
对话框
,我们可以在其中输入新值。通过实现
TextWatcher
或类似功能,也可以直接编辑
EditText
字段中的文本,但我发现这种方法更容易创建。因此,在我的示例应用程序中,当您单击
数量
字段时,它看起来像

为了实现这一点,我们需要首先在
onCreate
之外声明ArrayList变量。这是因为我将在稍后的单击事件中引用它们:

public class MainActivity extends ListActivity {

ArrayList<String> bookNames, code, quantity;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //This isn't necessary, but I create it to make fake ISBN and quantity numbers
    Random rand = new Random();

    //Arrays to store fake Book Names, ISBN numbers and Quantity Numbers
    bookNames = new ArrayList<>();
    code = new ArrayList<>();
    quantity  = new ArrayList<>();
最后,此方法添加在我们的
main活动
的末尾,并包含基于名为
update\u value.xml
的模板启动对话框的逻辑。当用户按下“接受”按钮时,它会更新行所在位置的
数量
阵列列表中的条目。这就是为什么我在这个方法中放置了一个
final int position
参数,因为它意味着我们可以从
getView()
访问
位置

对话框的
xml
文件非常简单,如下所示:

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Helps create fake ISBN and quantity numbers
    Random rand = new Random();

    //Arrays to store fake Book Names, ISBN numbers and Quantity Numbers
    final ArrayList<String> bookNames = new ArrayList<>();
    final ArrayList<String> code = new ArrayList<>();
    final ArrayList<String> quantity = new ArrayList<>();

    // You can remove this if you don't want the permanent headers
     setContentView(R.layout.activity_main);

    //This populates our arrays with the fake names and numbers, so there is something to
    //display in the list
    for (int i = 1; i < 30; i++){
        bookNames.add("Book No: " + i);
        code.add(String.valueOf((rand.nextInt(100) * i) + 1000));
        quantity.add(String.valueOf(rand.nextInt(5) + 1));
    }

    setListAdapter(new ListAdapter() {
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Value:"
        android:textStyle="bold"
        android:id="@+id/textView"
        android:textSize="18sp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:id="@+id/editText"
        android:layout_gravity="center_horizontal"
        android:textSize="16sp" />

</LinearLayout>
然后,在
getView()中添加以下内容:

Bundle bundle = new bundle();
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);

                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (!isChecked) {
                            bundle.putInt("checked" + position, position);
                        } else {
                            bundle.remove("checked" + position);
                        }
                    }
                });

                if (bundle.get("checked" + position) != null){
                    checkBox.setChecked(false);
                } else {
                    checkBox.setChecked(true);
                }

这在滚动列表时可以正常工作,但并不完美-例如,列表项在配置更改时仍会刷新。

检查此链接@Anisyup,谢谢,伙计,我像此列表一样构建,但我需要为每行生成一个文本字段以设置数量。。。这是我的一个问题^ ^你可以创建另一个textview来显示你的数量bro,并将这些数字设置为stringare,尝试在列表视图后面构建textview动态,然后获得textview id的值依赖性?你必须根据需要创建自定义布局并在listview上膨胀。哇,非常好,谢谢,但问题仍然是我需要一个文本字段而不是一个文本视图,我可以为所选项目设置数量值…嘿,我在上面的帖子中添加了一些编辑来回答你的后续问题。棘手的部分是确保ListView不会在滚动屏幕时简单地循环使用输入的值。我上面的回答给出了一个可能的解决方案。(Y)非常感谢您的帮助^ ^,它非常好的描述和解决方案亲爱的@PPartisan,请在我选中时出现问题,它也选中了列表中的其他元素,例如如果选中了pos 1,app选中了pos1和pos 18以及pos 36。。。这样地。。我如何修复它呢?我认为在列表中滚动时,它可以重新创建…(同样,当滚动复选框时,它被更改为另一个位置,如pos2,然后另一个滚动返回到位置1)…嗨,阿尼斯,是的,这是
ListView
的一个怪癖,是由它的内存节省策略买来的。在我的回答中,我给出了一个可能的解决方案——它会起作用,但我对它不是100%满意!我给你留下了一些建议,告诉你下一步可以在哪里改进它。
Bundle bundle = new bundle();
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);

                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (!isChecked) {
                            bundle.putInt("checked" + position, position);
                        } else {
                            bundle.remove("checked" + position);
                        }
                    }
                });

                if (bundle.get("checked" + position) != null){
                    checkBox.setChecked(false);
                } else {
                    checkBox.setChecked(true);
                }