Android listview带有每个元素的editText和复选框 [英] Android listview with editText and checkbox for each element

查看:84
本文介绍了Android listview带有每个元素的editText和复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个项目列表,我想用这些项目中的每一个建立一个表单.此表单包含两个复选框和一个editText.例如,我想知道仓库中是否存在每个项目及其数量.我正在考虑使用列表视图来解决我的问题,其中列表视图的每个元素将由一个项目的名称,两个复选框和一个editText组成.
问题是我知道要显示元素列表的列表视图的唯一用途,我不怎么解决它的问题(我是android的初学者).有人可以帮我吗?
还有另一种方法可以解决我的问题吗?
谢谢

let's say that i have a list of items and i want to build a form with each of these items. This form consist of two checkboxes and an editText. For example i want to know if each item is present in a warehouse and its quantity. I'm thinking for solving my problem to use a listview where each element of my listview will consist of the name of an item, two checkboxes and an editText.
The problem is that the only use of listview i know to present list of elements, i don't how to solve my problem with it (i'm a beginner in android). Can someone help me ?
Is there another way to solve my problem ?
Thank you

推荐答案

尝试实现自定义ListView适配器!这比您想像的要容易!

Try to implements a cusom ListView adapter! This is easier than you might think!

首先,您需要创建将代表列表中每个项目的布局:

First you need to create layout which would will represent each item in your list:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test TEST" />

<LinearLayout android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignBottom="@id/itemTextView"
    android:layout_alignParentRight="true">
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/doneCheckBox" />
</LinearLayout>

然后在代码内实现cusom适配器:

Then implement cusom adapter inside your code:

public CusomAdapter(Context mainContex, YourItems<SomeItem> someItems) {
    this.mainContex = mainContex;
    this.someItems = someItems;
}

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

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

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

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


    View item = convertView;
    if (item == null) {
        item = LayoutInflater.from(mainContex).inflate(R.layout.shoplist_item, null); // your listView layout here!
    }

     //fill listView item with your data here!
    //initiate your check box
    CheckBox doneCheckBox = (CheckBox)item.findViewById(R.id.doneCheckBox);

    //add a checkbox listener
    doneCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if(isChecked){
            doneCheckBox.ischecked=true;
        }
        else{
            doneCheckBox.ischecked=false;
        }
    }
});

    return item;
}

别忘了在Activity布局内添加ListView元素!

don't forget to add ListView element inside your Activity layout!

这篇关于Android listview带有每个元素的editText和复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆