Android listview with editText and checkbox for ea

2019-03-05 10:25发布

问题:

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

回答1:

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>

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;
}

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