Listview動態新增/刪除資料
阿新 • • 發佈:2019-02-18
這裡是針對list的資料新增/刪除方式,其他的資料方式相同。
佈局很簡單:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加" /> <Button android:id="@+id/btn_remove" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="減少" /> </LinearLayout> <ListView android:id="@+id/lv_show" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
每行的佈局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="42dp" android:layout_marginTop="30dp" android:src="@drawable/custom_loc" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView1" android:layout_centerHorizontal="true" android:text="TextView" /> </RelativeLayout>
activity:
public class AdapterAddData extends Activity implements OnClickListener { private Button addBtn; private Button removeBtn; private ListView show; private List list; private MyShowAdapter myadapter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.acy_adddate); init(); list = new ArrayList(); for (int i = 0; i < 3; i++) { list.add("123"); } myadapter = new MyShowAdapter(this, list); show.setAdapter(myadapter); } // 初始化 public void init() { // 元件 addBtn = (Button) findViewById(R.id.btn_add); removeBtn = (Button) findViewById(R.id.btn_remove); show = (ListView) findViewById(R.id.lv_show); // 新增監聽器 addBtn.setOnClickListener(this); removeBtn.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == addBtn) { list.add("123"); myadapter.notifyDataSetChanged(); } else if (v == removeBtn) { list.remove(0); myadapter.notifyDataSetChanged(); } } }
介面卡:
public class MyShowAdapter extends BaseAdapter {
private Context context;
private List list;
public MyShowAdapter(Context context, List list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
convertView=LayoutInflater.from(context).inflate(R.layout.adater_line, null);
}
return convertView;
}
}
效果:
這個執行的時候,開始加入了3條資料。
新增一個數據:
刪除2條資料:
動態改變行數:
public class TestMoreList extends Activity implements OnClickListener {
private int i = 10;
private MyAdpter adp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acy_morelist);
ListView lv = (ListView) findViewById(R.id.lv_txt);
adp = new MyAdpter(i);
lv.setAdapter(adp);
findViewById(R.id.btn_more).setOnClickListener(this);
}
class MyAdpter extends BaseAdapter {
private int i = 0;
public MyAdpter(int i) {
this.i = i;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return i;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView txt = null;
if (null == convertView) {
convertView = getLayoutInflater().inflate(R.layout.line_txt,
null);
}
txt = (TextView) convertView.findViewById(R.id.tv_txt);
txt.setText(new Date().toString());
return convertView;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_more:
adp.i += 10;
adp.notifyDataSetChanged();
break;
default:
break;
}
}
}
注意:
1.資料的動態改變主要是這句:
myadapter.notifyDataSetChanged();
如果修改了,沒通知介面卡資料更改,那就不能看到更改的效果,雖然list改變了。