如何在listveiw中动态添加行?

时间:2015-10-18 07:47:16

标签: java android listview

我正在尝试通过提供外部数据动态地在列表视图中创建一行。

  • 列表视图中的每一行都有字符串数据开关。从外部编辑文本中获取的字符串数据。
  • 我有 EditText字段。我将通过此输入数据。
  • 我有一个按钮。按下时,应将EditText内容与Switch
  • 一起添加到listview的新行
  • 添加新行时,应注意保留以前添加的行。

我从网上获得了代码框架。我的操作符合我的要求。

以下代码动态添加行,但它正在清除以前添加的行。请帮助。谢谢

这是我的 MainActivity.java

public class MainActivity extends Activity {
Switch sw1;

private ListView listView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
    }

    public void getdata(View v) {
        EditText et;
        String dev_name;
        et = (EditText)findViewById(R.id.editText1) ;
        dev_name = et.getText().toString() ;
        create(dev_name, null);

    }

    public void create (String str, Switch sw1) {

    Items   Items_data[] = new Items[]
            {
              new Items( str,sw1)
            };

      ItemsAdapter adapter = new ItemsAdapter (this,R.layout.items_row, Items_data);

       listView1 = (ListView)findViewById(R.id.listView1);
       listView1.setAdapter(adapter);
   }

}

这是我的 ItemsAdapter.java

public class ItemsAdapter   extends ArrayAdapter<Items>{

    Context context;
    int layoutResourceId;   
    Items data[] = null;

    public ItemsAdapter(Context context, int layoutResourceId, Items[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


       View row = convertView;
        ItemsHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ItemsHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.listtext);
            holder.switch1= (Switch)row.findViewById(R.id.listswitch);


            row.setTag(holder);
        }
        else
        {
            holder = (ItemsHolder)row.getTag();
        }

        Items item = data[position];
        holder.txtTitle.setText(Items.tv);
        holder.switch1.setChecked(false);

        return row;
    }

    static class ItemsHolder
    {
       TextView txtTitle;
       Switch switch1;
    }

}

这是我的项目类

public class Items {

    public static String tv;
    public static Switch switc;

    public Items() {
        super();
    }

 public Items(String tv, Switch switc ) {
            super();
            this.tv = tv;
            this.switc = switc;
        }


}

2 个答案:

答案 0 :(得分:0)

更改适配器声明。可能会低于代码帮助你。

public class MainActivity extends Activity {
Switch sw1;

private ListView listView1;
 ItemsAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        adapter = new ItemsAdapter (this,R.layout.items_row, Items_data);
    }

    public void getdata(View v) {
        EditText et;
        String dev_name;
        et = (EditText)findViewById(R.id.editText1) ;
        dev_name = et.getText().toString() ;
        create(dev_name, null);

    }

    public void create (String str, Switch sw1) {

    Items   Items_data[] = new Items[]
            {
              new Items( str,sw1)
            };

       adapter = new ItemsAdapter (this,R.layout.items_row, Items_data);

       listView1 = (ListView)findViewById(R.id.listView1);
       listView1.setAdapter(adapter);
   }

}

点击此链接:Dynamically add elements to a listView Android

答案 1 :(得分:0)

您正在为一个数组分配一个使用create()中传递的参数创建的Items对象。你应该尝试这样的事情:

public class MainActivity extends Activity {
    private Items[] items = new Items[0]; //We're making this a field, so we can create a new array containing our new item.
    ...
    public void create (String str, Switch sw1) {
        int i = items.length+1; //plus one because we are adding one element at a time. If you'd like to add more at once, change the 1 accordingly.
        Items[] tmp = new Items[i];
        System.arrayCopy(items, 0, tmp, 0, items.length);
        tmp[i] = new Items(str, sw1);
        items = tmp; //use items like you would use Items_data
        ...
    }
}

这会将items的内容添加到tmp,然后在最后添加新项目。如果您希望新项目位于堆栈顶部,我们可以简单地说:

System.arrayCopy(items, 0, tmp, 1, items.length);
tmp[0] = new Items(str, sw1);