Skip to content Skip to sidebar Skip to footer

How To Add An Edittext To A Listview

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

Solution 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"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"
><ImageViewandroid:id="@+id/icon"android:padding="2dip"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ok"
/><TextViewandroid: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.

publicclassDemoextendsListActivity {

@OverridepublicvoidonCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(newAdapter());}

//Here extends a ArrayAdapter to create your custom viewclassAdapterextendsArrayAdapter<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/

Solution 2:

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;

publicclassOrderProductSearchextendsListActivity {

/** Called when the activity is first created. */@OverridepublicvoidonCreate(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 storedfinalListViewlistContent= (ListView)findViewById(R.id.orderproductlistview); 

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

    //Create instance of the databasefinalDbAdapterdb=newDbAdapter(this); 

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

    //Routine to call all product sub groups from the databasefinalCursorcursor= db.getAllSubGroupProduct();

    //Manages the cursor
    startManagingCursor(cursor);  

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

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

    //Create simple cursor adapterSimpleCursorAdaptercursorAdapter=newSimpleCursorAdapter(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 systemintcheck= cursor.getCount();

    AlertDialog.Builderps=newAlertDialog.Builder(OrderProductSearch.this);
    finalButtonborder= (Button) findViewById(R.id.orderqty);


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


    border.setOnClickListener(newView.OnClickListener() {
        publicvoidonClick(View arg0) {
            try {
                Stringclicked="";
                clicked = "neil shaw";

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

Post a Comment for "How To Add An Edittext To A Listview"