如何在编辑文本中显示自定义列表视图记录

时间:2014-09-24 06:39:40

标签: android android-listview

我创建了一个带有两个文本视图和一个编辑文本的自定义列表视图,在自定义列表视图下有一个编辑文本(textPostalAddress),我想要做的是当用户在自定义编辑文本中输入值时列表视图当时我希望在列表视图下方的编辑文本中添加该记录,当用户再次输入值时,该值也会显示在编辑文本中的上一个值。

public class MainActivity extends Activity  {
ArrayList<Candy> myArrList;
EditText text;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     myArrList = new ArrayList<Candy>();
     ListView lisView1 = (ListView)findViewById(R.id.listView1);
     et=(EditText)findViewById(R.id.editText1);       
     myArrList.add(new Candy("Butterscotch", "10"));
     myArrList.add(new Candy("Birthday Cake", "100"));
     myArrList.add(new Candy("Black Crunch", "102"));
     myArrList.add(new Candy("Industrial Chocolate", "200"));
     myArrList.add(new Candy("Coffee Molasses Chip", "500"));      
     lisView1.setAdapter(new CountryAdapter(this));}
     public class CountryAdapter extends BaseAdapter
     {
     private Context context;
     public CountryAdapter(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.activity_mmnue, null);
            }
        // ColID
        TextView txtID = (TextView) convertView.findViewById(R.id.nm);
        txtID.setText(myArrList.get(position).getName() +".");
        // ColCode
        TextView txtCode = (TextView) convertView.findViewById(R.id.rat);
        txtCode.setText(myArrList.get(position).getRate());
        text = (EditText)convertView.findViewById(R.id.txtInput);
        text.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count){}
        public void beforeTextChanged(CharSequence s, int start, int count,int after){}
        public void afterTextChanged(Editable s)
        {
        String rk=myArrList.get(position).getName()+","+s+","+myArrList.get      (position).getRate();
                     et.setText(rk);
          Toast.makeText(getApplicationContext(), "hello"+s+""+myArrList.get
                      (position).getName()+""+myArrList.get(position).getRate(), 50).show();
                                   }
                             });
                     return convertView;}}

2 个答案:

答案 0 :(得分:0)

从edittext获取记录并将其添加到arraylist。然后调用lisView1.invalidate();更新列表视图中的记录

答案 1 :(得分:0)

EditText很难用作每个ListView行中的项目。我认为您应该使用Button作为项而不是EditText,然后点击此按钮打开包含AlertDialog的{​​{1}},在edittext press中输入值好的,并将其附加到EditText之外的Edittext,例如在您的适配器的listview方法中执行以下操作:

getView