ListView 中添加按钮,动态删除添加ItemView的操作
2010-11-29 12:47
381 查看
要实现添加按钮的操作,必须自定义Adapter,使用Button View的setTag()方法,将Button所属的位置设置到tag当中
要实现动态添加删除ItemView的操作,必须首先调整调整Adapter所绑定的数据源,然后调用Adapter的notifyDataSetChanged()方法
以下为实现的一个实例
要实现动态添加删除ItemView的操作,必须首先调整调整Adapter所绑定的数据源,然后调用Adapter的notifyDataSetChanged()方法
以下为实现的一个实例
package com.jason.joysmsyd; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.View.OnClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class SendMain extends ListActivity implements OnClickListener{ Button buttonMessage,buttonContact,buttonHistory; EditText textMessage; List<Map<String,String>> contacts = new ArrayList<Map<String,String>>(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.setContentView(R.layout.layout_send); buttonMessage = (Button) this.findViewById(R.id.ButtonMessage); buttonContact = (Button) this.findViewById(R.id.ButtonContact); buttonHistory = (Button) this.findViewById(R.id.ButtonHistory); textMessage = (EditText)this.findViewById(R.id.EditTextMessage); textMessage.setText(this.getIntent().getExtras().getString("message")); } public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.ButtonMessage: this.finish(); break; case R.id.ButtonContact: { Intent intent = new Intent(); intent.setAction("com.jason.action.contact"); this.startActivityForResult(intent, 0); } break; case R.id.ButtonHistory: { Intent intent = new Intent(); intent.setAction("com.jason.action.history"); this.startActivityForResult(intent, 1); } break; } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0 && resultCode == RESULT_OK) { this.getcontactFromString(data.getExtras().getString( UserSelectActivity.RETURN_LIST)); bindDataToList(); } } private void getcontactFromString(String data) { if (data == null || data.length() == 0) { return; } String[] arrayContact = data.split("#"); for (String singleContact : arrayContact) { if (singleContact != null && singleContact.length() > 0) { String[] props = singleContact.split(":"); if (props.length == 2) { Map<String,String> contact = new HashMap<String,String>(); contact.put("name", props[0]); contact.put("phone", props[1]); contacts.add(contact); } } } } private void bindDataToList(){ this.setListAdapter(new MyAdapter()); } public class MyAdapter extends BaseAdapter{ public int getCount() { // TODO Auto-generated method stub return contacts.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return contacts.get(position); } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater inflater = SendMain.this.getLayoutInflater(); final View view = inflater.inflate(R.layout.layout_user_item, null); final TextView textPhone = (TextView) view.findViewById(R.id.text1); final TextView textName = (TextView) view.findViewById(R.id.text2); Button button = (Button)view.findViewById(R.id.buttonDelete); textPhone.setText(contacts.get(position).get("phone")); textName.setText(contacts.get(position).get("name")); button.setTag( position); button.setOnClickListener(new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub int position = Integer.parseInt(v.getTag().toString()); contacts.remove(position); MyAdapter.this.notifyDataSetChanged(); // SendMain.this.getListView().refreshDrawableState(); }}); return view; } } }
相关文章推荐
- ListView 中添加按钮,动态删除添加ItemView的操作
- ListView 中添加按钮,动态删除添加ItemView的操作
- Android – ListView 中添加按钮,动态删除添加ItemView的操作
- 【android】ListView 中添加按钮,动态删除添加ItemView的操作
- Android – ListView 中添加按钮,动态删除添加ItemView的操作
- Android – ListView 中添加按钮,动态删除添加ItemView的操作
- js jquery分别实现动态的文件上传操作按钮的添加和删除
- js jquery分别实现动态的文件上传操作按钮的添加和删除
- 实现动态的文件上传操作按钮的添加和删除
- 揭秘在ListView等AdapterView上动态添加删除项的陷阱(和在listview下添加按钮的布局设计)
- JavaScript动态操作表格实例(添加,删除行,列及单元格)
- 添加删除、更新按钮的提示确认信息,以及DATAGRID的添加、插入、更新、删除操作。
- 点击地图div上的按钮实现对地图数据的操作:查询点、添加点、删除点
- 点击按钮实现表格动态添加或删除一行
- 添加删除、更新按钮的提示确认信息,以及DATAGRID的添加、插入、更新、删除操作。
- ListView 动态删除添加Item方法
- android ListView内数据的动态添加与删除
- Listview动态添加/删除数据
- File Input多次添加文件,动态删除文件,用来实现上传等操作
- 动态添加删除ExpandableListView的item的例子