如何在Custom ListView中的arraylist中添加EditText值

时间:2014-09-10 18:25:23

标签: android android-listview android-edittext

我在我的第一个自定义ListView中创建了两个TextView和一个EditText,我想在Arraylist中添加/存储EditText值因为我想在第二个Custom 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;

    }

1 个答案:

答案 0 :(得分:0)

首先,每次创建一个新的HashMap<String, String>,都会浪费大量空间。不确定为什么你会ArrayList HashMaps。拥有一个或另一个就足够了。

您可以创建一个名为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"));

现在,您可以使用名为CandygetID()的{​​{1}}类的getter方法。

变化:

getCode()

要:

// 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);