【问题标题】:How to add EditText value in arraylist in Custom ListView如何在自定义 ListView 的 arraylist 中添加 EditText 值
【发布时间】:2014-11-04 12:27:46
【问题描述】:

我在我的第一个自定义 ListView 中创建了两个 TextView 和一个 EditText,我想在 Arraylist 中添加/存储 EditText 值,因为我想在第二个自定义 ListView 中一个一个地显示 EditText 值,所以如何做到这一点请给出提示或代码来解决这个问题..

public class Mmenu extends Activity {
ArrayList<Candy> myArrList;
ArrayList<String> editTextValues;

protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myArrList = new ArrayList<Candy>();
    editTextValues = new ArrayList<String>();
    final ListView lisView1 = (ListView)findViewById(R.id.listView1);
    final ListView lisView2 = (ListView)findViewById(R.id.listView2);

    myArrList.add(new Candy("Butterscotch", "Rs 10"));
    myArrList.add(new Candy("Birthday Cake", "Rs 100"));
    myArrList.add(new Candy("Black Crunch", "Rs 102"));
    myArrList.add(new Candy("Industrial Chocolate", "Rs 200"));
    myArrList.add(new Candy("Coffee Molasses Chip", "Rs 500"));      


    lisView1.setAdapter(new CountryAdapter(this));


    Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
    btnGetItem.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {                
            int count = lisView1.getAdapter().getCount();


            for(int i=0;i<count;i++)
            {
            LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i);

            EditText text = (EditText)findViewById(R.id.txtInput);
            String value = text.getText().toString();
            editTextValues.add(value);
            Toast.makeText(getApplicationContext(),"10"+value,   Toast.LENGTH_LONG).show();
            }


            lisView2.setAdapter(new CountryAdapter2(getApplicationContext()));
            }
            });
             }


    public class CountryAdapter extends BaseAdapter
    {
    private Context context;

    public CountryAdapter(Context c)
    {
    //super( c, R.layout.activity_column, R.id.rowTextView, );
    // TODO Auto-generated method stub
    context = c;
    }
    public int getCount() {
    // TODO Auto-generated method stub
    return myArrList.size();
    }
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
    }
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
    convertView = inflater.inflate(R.layout.views, null);
     }
    // ColID
    TextView txtID = (TextView) convertView.findViewById(R.id.nm);
    txtID.setText(myArrList.get(position).getID() +".");
    // ColCode
    TextView txtCode = (TextView) convertView.findViewById(R.id.rat);
    txtCode.setText(myArrList.get(position).getCode());
    return convertView;

    }
     }
    public class CountryAdapter2 extends BaseAdapter
    {
    private Context context;
    public CountryAdapter2(Context c)
    {
        context = c;
    }
    public int getCount() {
    // TODO Auto-generated method stub
        return myArrList.size();
    }
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
    }
     public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null)
    {convertView = inflater.inflate(R.layout.newviews, null);
    }

    // ColID
    TextView txtID = (TextView) convertView.findViewById(R.id.nm);
    txtID.setText(myArrList.get(position).getID() +".");
    // ColCode
    TextView txtCode = (TextView) convertView.findViewById(R.id.rat);
    txtCode.setText(myArrList.get(position).getCode());
    return convertView;

    }

【问题讨论】:

    标签: android android-listview android-edittext


    【解决方案1】:

    首先,您每次都创建一个新的HashMap&lt;String, String&gt; 会浪费大量空间。不知道为什么你会有ArrayListHashMaps。拥有其中一个就足够了。

    您可以像这样创建一个名为 Candy 的类:

    class Candy {
        private String id;
        private String code;
        Candy(String id, String code) {
            this.id = id;
            this.code = code;
        }
        public String getID() { return this.id; }
        public String getCode() { return this.code; }
    }
    

    然后你可以定义你的ArrayList并在onCreate()的开头实例化它:

    // Don't use starting capital letters for methods, classes have first capital letter
    // I renamed MyArrList to myArrList
    ArrayList<Candy> myArrList;
    // ...
    void onCreate() {
    // ...
        myArrList = new ArrayList<Candy>();
    // ...
    }
    

    然后像这样填充它:

    myArrList.add(new Candy("Butterscotch", "Rs 10"));
    myArrList.add(new Candy("Birthday Cake", "Rs 100"));
    myArrList.add(new Candy("Black Crunch", "Rs 102"));
    myArrList.add(new Candy("Industrial Chocolate", "Rs 200"));
    myArrList.add(new Candy("Coffee Molasses Chip", "Rs 500"));
    

    现在您可以使用名为getID()getCode()Candy 类的getter 方法。

    变化:

    // ColID
    TextView txtID = (TextView) convertView.findViewById(R.id.nm);
    txtID.setText(MyArrList.get(position).get("ID") +".");
        // ColCode
    TextView txtCode = (TextView) convertView.findViewById(R.id.rat);
    txtCode.setText(MyArrList.get(position).get("Code"));
    

    收件人:

    // ColID
    TextView txtID = (TextView) convertView.findViewById(R.id.nm);
    txtID.setText(myArrList.get(position).getID() +".");
    // ColCode
    TextView txtCode = (TextView) convertView.findViewById(R.id.rat);
    txtCode.setText(myArrList.get(position).getCode());
    

    现在回答您的问题,我假设您要将EditText 字段中的文本存储到ArrayList 中。

    您可以创建另一个类ArrayList 对象来保存String 对象并在onCreate() 中实例化它。

    ArrayList<String> editTextValues;
    // ...
    void onCreate() {
    // ...
        editTextValues = new ArrayList<String>();
    // ...
    }
    

    现在您只需将 EditText 中的文本添加到此 ArrayList

    EditText text = (EditText)findViewById(R.id.YOUR_ID);
    String value = text.getText().toString();
    editTextValues.add(value);
    

    然后在需要的地方使用editTextValues

    【讨论】:

    • 那么 EditText 怎么办
    • 当我点击 getInputItem 时,我需要更多帮助
    • 如果没有其他信息,我无法为您提供帮助。您需要具体描述出了什么问题,是否显示了任何错误,如果错误是什么意思,以及哪些代码导致了错误
    • 我更新我的代码检查它,我需要将第一个自定义 ListView 的所有 EditText 值设置为第二个自定义 ListView EditText ,当我按下按钮时点击它只显示第一个 EditText 值
    • 很抱歉,您的措辞令人困惑。我相信你应该为第二个ListView 做的是:'EditText text2 = (EditText)findViewById(R.id.txtInput2);字符串值 = editTextValues.get(0); text2.setText(value, TextView.BufferType.EDITABLE);'
    猜你喜欢
    • 2016-05-06
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多