A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 dongyangzhang 于 2013-4-26 20:42 编辑

请问怎么把图中1,2条目一个一个删除了?请大牛举一个简单的例子,学习一下,谢谢啦。

QQ截图20130418135012.png (38.65 KB, 下载次数: 117)

QQ截图20130418135012.png

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

6 个回复

倒序浏览
实现ListFragment的 onListItemClick(ListView l, View v, int position, long id)方法,ListView中的一项被点击时会调用此方法,可在此方法中实现删除操作。
具体删除方法:根据参数position或id,删除相应List中的内容(该List就是你的ListView显示内容的装载类),然后用新的List生成Adapter,再调用listView.setAdapter();
回复 使用道具 举报
董波 发表于 2013-4-26 12:21
实现ListFragment的 onListItemClick(ListView l, View v, int position, long id)方法,ListView中的一项 ...

用新的List生成Adapter然后再调用listView.setAdapter();的作用是什么呢?这段有点不懂?请问有具体代码实现吗?谢谢你啊:)
回复 使用道具 举报
dongyangzhang 发表于 2013-4-26 18:12
用新的List生成Adapter然后再调用listView.setAdapter();的作用是什么呢?这段有点不懂?请问有具体代码 ...

就是把往ListView中填充内容的步骤在重复一遍,指示这时的List是删除过一项元素之后的。
你既然生成了上面图示的ListView,应该会填充内容的步骤吧。
至于代码实现没法给你写,你看一下你得生成Adapter的代码,里面不是有很多参数吗,我不知道你得每个参数使用的是什么值,不好意思

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

回复 使用道具 举报
董波 发表于 2013-4-26 19:28
就是把往ListView中填充内容的步骤在重复一遍,指示这时的List是删除过一项元素之后的。
你既然生成了上 ...

有点明白了谢谢你
回复 使用道具 举报
int position = Integer.parseInt(arr[1]);  //确定item所在位置
mData.remove(position);  //移除该item
MyAdapter.this.notifyDataSetChanged();  //刷新listview

看一下网上找的一个例子,讲得比较详细
http://www.cnblogs.com/qzxia/archive/2010/11/29/1890959.html
回复 使用道具 举报
李慧声 发表于 2013-4-26 22:37
int position = Integer.parseInt(arr[1]);  //确定item所在位置
mData.remove(position);  //移除该item
M ...

谢谢你!!找对代码了,学习了

  1. package com.jason.joysmsyd;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;

  6. import android.app.ListActivity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.view.Window;
  13. import android.view.View.OnClickListener;
  14. import android.widget.BaseAdapter;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.TextView;

  18. public class SendMain extends ListActivity implements OnClickListener{

  19.     Button buttonMessage,buttonContact,buttonHistory;
  20.     EditText textMessage;
  21.      
  22.     List<Map<String,String>> contacts = new ArrayList<Map<String,String>>();
  23.      
  24.      
  25.      
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         // TODO Auto-generated method stub
  29.         super.onCreate(savedInstanceState);
  30.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  31.          
  32.         this.setContentView(R.layout.layout_send);
  33.          
  34.         buttonMessage = (Button) this.findViewById(R.id.ButtonMessage);
  35.         buttonContact = (Button) this.findViewById(R.id.ButtonContact);
  36.         buttonHistory = (Button) this.findViewById(R.id.ButtonHistory);
  37.          
  38.         textMessage = (EditText)this.findViewById(R.id.EditTextMessage);
  39.         textMessage.setText(this.getIntent().getExtras().getString("message"));
  40.      
  41.     }

  42.     public void onClick(View v) {
  43.         // TODO Auto-generated method stub
  44.         switch(v.getId()){
  45.         case R.id.ButtonMessage:
  46.             this.finish();
  47.             break;
  48.         case R.id.ButtonContact:
  49.         {
  50.             Intent intent = new Intent();
  51.             intent.setAction("com.jason.action.contact");
  52.             this.startActivityForResult(intent, 0);
  53.         }
  54.             break;
  55.         case R.id.ButtonHistory:
  56.         {
  57.             Intent intent = new Intent();
  58.             intent.setAction("com.jason.action.history");
  59.             this.startActivityForResult(intent, 1);
  60.         }
  61.             break;
  62.         }
  63.          
  64.     }

  65.      
  66.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  67.         // TODO Auto-generated method stub
  68.         super.onActivityResult(requestCode, resultCode, data);
  69.          if (requestCode == 0 && resultCode == RESULT_OK) {
  70.             this.getcontactFromString(data.getExtras().getString(
  71.                     UserSelectActivity.RETURN_LIST));
  72.             bindDataToList();
  73.         }
  74.     }

  75.     private void getcontactFromString(String data) {
  76.         if (data == null || data.length() == 0) {
  77.             return;
  78.         }

  79.          
  80.         String[] arrayContact = data.split("#");
  81.         for (String singleContact : arrayContact) {
  82.             if (singleContact != null && singleContact.length() > 0) {
  83.                 String[] props = singleContact.split(":");
  84.                 if (props.length == 2) {
  85.                     Map<String,String> contact = new HashMap<String,String>();
  86.                     contact.put("name", props[0]);
  87.                     contact.put("phone", props[1]);
  88.                     contacts.add(contact);

  89.                 }
  90.             }

  91.         }
  92.          
  93.     }
  94.      
  95.     private void bindDataToList(){
  96.         this.setListAdapter(new MyAdapter());
  97.     }
  98.      
  99.     public class MyAdapter extends BaseAdapter{

  100.         public int getCount() {
  101.             // TODO Auto-generated method stub
  102.             return contacts.size();
  103.         }

  104.         public Object getItem(int position) {
  105.             // TODO Auto-generated method stub
  106.             return contacts.get(position);
  107.         }

  108.         public long getItemId(int position) {
  109.             // TODO Auto-generated method stub
  110.             return position;
  111.         }


  112.         public View getView(int position, View convertView, ViewGroup parent) {
  113.             // TODO Auto-generated method stub
  114.             LayoutInflater inflater = SendMain.this.getLayoutInflater();
  115.              final View view = inflater.inflate(R.layout.layout_user_item, null);
  116.              final TextView textPhone = (TextView) view.findViewById(R.id.text1);
  117.              final TextView textName = (TextView) view.findViewById(R.id.text2);
  118.              Button button = (Button)view.findViewById(R.id.buttonDelete);
  119.             
  120.              textPhone.setText(contacts.get(position).get("phone"));
  121.              textName.setText(contacts.get(position).get("name"));
  122.               
  123.              button.setTag( position);
  124.               
  125.              button.setOnClickListener(new OnClickListener(){

  126.                 public void onClick(View v) {
  127.                     // TODO Auto-generated method stub
  128.                     int position = Integer.parseInt(v.getTag().toString());
  129.                     contacts.remove(position);
  130.                     MyAdapter.this.notifyDataSetChanged();
  131.                      
  132. //                  SendMain.this.getListView().refreshDrawableState();
  133.                 }});
  134.               
  135.               
  136.               
  137.             
  138.             return view;
  139.         }
  140.          
  141.     }
  142. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马