android 中的列表视图单元格插入

Listview Cellinsertion in android

您好,我有带有 textview 和 edittext.When 的自定义列表视图,我单击添加行按钮,在此处的列表视图中显示一行,输入文本,然后再次单击添加按钮,显示另一行,最后单击保存按钮,获取文本视图和来自列表视图的 edittext 值。

我已经试过了

 button = (Button) findViewById(R.id.button);
    Movi = new String[] { "one", "two", "three", "four", "five", "six",
            "seven", "eight", "nine" };
    dataList = (ListView) findViewById(R.id.list);
    adapter = new CustomAdapter(CustomListView.this, R.layout.list,
            imageArry);
    // add data in contact image adapter
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (itemCount < Movi.length) {
                imageArry.add(new Item(itemCount, Movi, text));
                dataList.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                itemCount++;
            }
        }
    });

下面我添加图片

无论何时向 imageArray 添加新项目,该插入都应反映在 CustomImageAdapter class 中。因此,在将项目添加到 imageArray 之后,将此 imageArray 分配给 CustomImageAdapter imageArray 属性.

public Class CustomImageAdapter {
    Context context;
    int layoutId;
    List<Item> imageArray;

      public CustomImageAdapter(Context context, int layoutId, List<Item> imageArray) {
          this.context = context;
          this.layoutId = layoutId;
          this.imageArray = imageArray;
      }
      public SetImageArray(List<Item> imageArray) {
          this.imageArray = imageArray;
      }

 }

您应该在调用 adapter.notifyDataSetChanged() 方法之前调用 SetImageArray。每次向已分配的适配器插入内容时,不必每次都设置适配器。

希望这对您有所帮助...

与其使用 Listview 并在每次点击时设置适配器,我建议您完全删除 Listview。以编程方式在每次单击时添加 TextView 和 EditText,如下所示。

button = (Button) findViewById(R.id.button);
// add data in contact image adapter
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        if (itemCount < Movi.length) {
            imageArry.add(new Item(itemCount, Movi, text));

            // Add TextView
            TextView textView=new TextView();
            textView.setTex(imageArray[itemCount]);
            // Add EditText
            EditText editText=new EditText();

            // Add both to root layout

            parentLayout.addView(textView);
            parentLayout.addView(editText);
            itemCount++;
        }
    }
});

其中 parentLayout 是您必须在 onCreate() 中分配的 activity 的根布局。例如,

LinearLayout parentLayout=(LinearLayout)findViewById(R.id.parent_layout);

希望对您有所帮助。