【问题标题】:How to add an EditText to a ListView如何将 EditText 添加到 ListView
【发布时间】:2011-09-21 18:37:29
【问题描述】:

我希望从数据库中读取一些产品详细信息,然后将它们添加到 ListView。

然后我希望在每一行都有一个数量的 EditText 框,客户可以在其中添加数量。 我怎样才能做到这一点?我做了一个简单的页面,但是当我输入一个数量并向下滚动然后再次备份时,我丢失了数据,或者它甚至出现在另一行的另一个数量框中。

【问题讨论】:

    标签: android listview android-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>
    

    接下来,您需要扩展 listview 并覆盖 get view 以加载到您的自定义行中。

    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/

    【讨论】:

    • 您也可以使用 TextWatcher 并将用户输入的每个数量保存到数组或其他东西中。然后在列表视图中按位置加载每个项目。这可能是保持其持久性的另一种方式。
    • 所以当输入数量时,我可以查询该行以获取他们输入的值?
    • 正确..所以如果项目在第一行并且用户输入 3 作为数量。数组 .get(position) 将是 1,因为它是第一行。所以你会得到它所在的行的位置,并通过它的行位置提取数据。
    • 非常感谢。我将从数据库中读取产品详细信息,最好先读入数组还是使用游标适配器?
    • 我会先使用光标适配器,因为您正在从数据库中读取数据。
    【解决方案2】:

    您应该将状态(用户在 EditText 中输入的内容)保存在某种数组中,提供给您的列表适配器。可能您在列表适配器的 getView() 方法中创建了新的 EditText。

    【讨论】:

      【解决方案3】:

      你好,下面是我一直在玩的代码

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

      【讨论】:

      • 并删除此答案,您发布编辑;或更新:在您的问题中。 =)
      • 当我在数量框中输入一个数字然后向下滚动屏幕并备份数量似乎移动到另一个框
      • 当滚动回视图时,设置数量的代码是什么样的?这是一个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 2020-04-18
      • 1970-01-01
      • 2012-04-15
      相关资源
      最近更新 更多