2011-09-21 93 views
0

我正在閱讀一些數據庫中的產品詳細信息,然後將它們添加到ListView中。如何將EditText添加到ListView中

然後,我想在每行上的一個數量EditText框,客戶可以在其中添加數量。 我該怎麼做?我做了一個簡單的頁面,但是當我輸入一個數字並向下滾動然後再次備份時,我丟失了數據,或者它甚至出現在另一行的另一個數字框中。

回答

1

好了,所以你需要做的第一件事是創建要在列表中的每一行有佈局的Row.xml文件..

<?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> 

接下來,您將需要擴展列表視圖並重寫獲取視圖以加載您的自定義行。

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 

} 

。這是你將需要開始,那麼你可以調用onItemListClick()來獲取當用戶單擊該項目的每個點擊的東西。

你可以得到一個完整的教程這裏...

Tutorial

編輯:

此外,如果你想保存在數量框中的數字,你需要有一個包。 如 saveState()方法

這將保存您的用戶數量,而應用程序仍然活着時,當帶回到視圖從數據包拉數字或int。

這應該是幫助下

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

+0

你也可以使用一個TextWatcher和用戶輸入保存每個量轉換成代碼數組或其他東西。然後在列表視圖中按位置加載每個項目。這可能是保持持久性的另一種方式。 –

+0

所以當輸入一個數字時,我可以直接查詢該行以獲取它們輸入的值? – shawrie

+0

正確。因此,如果該項目在第一行,並且用戶輸入的數量爲3。數組.get(position)將成爲1,因爲它是第一行。因此,您將獲得它所在的行的位置,並通過其行位置拉取數據。 –

0

您應該將狀態(用戶在EditText中輸入的內容)保存在提供給列表適配器的某種數組中。可能您在列表適配器的getView()方法中創建新的EditText。

0

嗨是我一直在玩弄

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 + ""; 
      } 
     } 
    }); 
+0

那麼你的代碼有什麼問題? –

+0

並刪除這個答案您發佈編輯;或更新:在你的問題。 =) –

+0

當我在數字框中輸入數字,然後向下滾動屏幕並備份數量似乎移動到另一個框 – shawrie