How to add an EditText to a ListView

2019-08-29 23:10发布

问题:

I'm looking to read some product details in from a database and then add them to a ListView.

I then want on each line a qty EditText box where customer can add a qty in. How can I do this? I did a simple page but when I enter a qty and the scroll down and then back up again I loose the data or it even appears in another qty box on another row.

回答1:

Okay so the first thing you will need to do is create a Row.xml file for the layout that you want each row in the list to have..

<?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="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/icon"
android:padding="2dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ok"
/>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
/>
//Add a edittext here..
/LinearLayout>

Next you will need to extends listview and override get view to load in your custom row.

public class Demo extends ListActivity {

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new Adapter());}

//Here extends a ArrayAdapter to create your custom view
class Adapter extends ArrayAdapter<String> {
Adapter() {
super(DynamicDemo.this, R.layout.row, R.id.label, items);
}
public View getView(int position, View convertView,
ViewGroup parent) {
//Here load in your views such as the edittext

}

Thats what you will need to get started you can then call onItemListClick() to get each click when the user clicks the item.

You can get a full tutorial here...

Tutorial

EDIT:

Also if you want to save the number in the quantity box you will need to have a Bundle. Such as saveState() method

This will save your users quantity number while the app is still alive, and when brought back into view pull the number or int from the bundle.

This should be of help

http://www.edumobile.org/android/android-beginner-tutorials/state-persistence/



回答2:

You should save state(content entered by user in EditText) in some sort of array, supplied to your list adapter. Probably you create new EditText in getView() method of your list adapter.



回答3:

Hi below is the code i have been playing around with

package sanderson.swords.mobilesales;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class OrderProductSearch extends ListActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    try{
    setContentView(R.layout.orderproducts);
    }
    catch (Exception e) {
        //
        String shaw="";
        shaw = e.getMessage();
    }

    //Create view of the list where content will be stored
    final ListView listContent = (ListView)findViewById(R.id.orderproductlistview); 

    //Set for fast scrolling 
    listContent.setFastScrollEnabled(true);

    //Create instance of the database
    final DbAdapter db = new DbAdapter(this); 

    //Open the Database and read from it
    db.openToRead();

    //Routine to call all product sub groups from the database
    final Cursor cursor = db.getAllSubGroupProduct();

    //Manages the cursor
    startManagingCursor(cursor);  

    //The columns we want to bound
    String[] from = new String[]{DbAdapter.KEY_PRNAME, 
            DbAdapter.KEY_PRSIZE, DbAdapter.KEY_PKQTY};

    //This is the id of the view that the list will be map to
   int[] to = new int[]{R.id.productlinerow, R.id.productlinerow2, R.id.productlinerow3};

    //Create simple cursor adapter
    SimpleCursorAdapter cursorAdapter = 
        new SimpleCursorAdapter(this, R.layout.productlinerow, cursor, from, to);

    //Set the cursor to the list content view
    listContent.setAdapter(cursorAdapter);

    //Close the database
    db.close();     

    //check if any orders are on the system
    int check = cursor.getCount();

    AlertDialog.Builder ps = new AlertDialog.Builder(OrderProductSearch.this);
    final Button border = (Button) findViewById(R.id.orderqty);


    //notify the user if there are no orders on the system
    if (check == 0)
    {
    ps.setTitle("No Products Found");
    ps.setMessage("There are no products in this group");
    ps.setPositiveButton("Ok", new DialogInterface.OnClickListener(){     
    public void onClick(DialogInterface dialog, int which) 
    {         
        OrderProductSearch.this.finish();
        startActivity(new Intent("sanderson.swords.mobilesales.PRODUCTENQUIRY"));
    } 
    }); 
        ps.show();
    }


    border.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            try {
                String clicked = "";
                clicked = "neil shaw";

            } catch (Exception e) {
                String error = e.getMessage();
                error = error + "";
            }
        }
    });