`

带删除按钮的ListView

阅读更多
不用说了,上图先:

import java.util.ArrayList;
import com.ql.adapter.DeletableAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class Test_4_Activity extends Activity {
	private DeletableAdapter adapter;
	private ArrayList<String> text;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test4);

		ListView list_view = (ListView) findViewById(R.id.list_view);

		text = new ArrayList<String>();
		text.add("111");
		text.add("222");
		text.add("333");
		text.add("444");
		// 初始化数据结束
		adapter = new DeletableAdapter(this, text);
		list_view.setAdapter(adapter);
		// list_view.setSelector(R.drawable.list_select_color);

		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				text.add("10000");
				adapter.notifyDataSetChanged();
			}
		});

	}

}


import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ql.activity.R;

public class DeletableAdapter extends BaseAdapter{
	private Context context;
	private ArrayList<String> text;
	public DeletableAdapter(Context context,ArrayList<String> text){
		this.context = context;
		this.text=text;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return text.size();
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return text.get(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
		final int index=position;
		View view=convertView;
		if(view==null){
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view=inflater.inflate(R.layout.row_simple_list_item_2, null);
		}
		final TextView textView=(TextView)view.findViewById(R.id.simple_item_1);
		textView.setText(text.get(position));
		final ImageView imageView=(ImageView)view.findViewById(R.id.simple_item_2);
		imageView.setBackgroundResource(android.R.drawable.ic_delete);
		imageView.setTag(position);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				text.remove(index);
				notifyDataSetChanged();
				Toast.makeText(context, textView.getText().toString(), Toast.LENGTH_SHORT).show();
			}
		});
		return view;
	}
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
	    <ImageView android:id="@+id/simple_item_2"
	    android:layout_width="wrap_content"
	    android:layout_height="fill_parent" 
	    android:layout_alignParentRight="true"
	    android:focusable="false"
	    />
  		<TextView android:id="@+id/simple_item_1"
	    android:layout_width="fill_parent" 
	    android:layout_height="fill_parent" 
	    android:layout_alignParentLeft="true"
	    />
</RelativeLayout>


这里要实现点击ListView里面的一个控件而不是选中一行。
最好重写Adapter,而不要使用其自带的SimpleAdapter,否则该子控件的事件就不那么好处理了!
  • 大小: 10.5 KB
分享到:
评论
4 楼 qianguming 2013-12-20  
如何在删除的时候提醒呢
3 楼 280541736 2012-04-26  
这种问题也太普遍了吧,用hanlder实现主UI线程的更新,不能在其他线程中对主UI线程更新。
2 楼 gundumw100 2011-11-19  
bigboy 写道
兄台 你这样的做法测试过么 ?
至少这个
public void onClick(View v) { 
55.                // TODO Auto-generated method stub 
56.                text.remove(index); 
57.                notifyDataSetChanged(); 
58.                Toast.makeText(context, textView.getText().toString(), Toast.LENGTH_SHORT).show(); 
59.            } 

就会报错 !
Only the original thread that created a view hierarchy can touch its views.

一定哪里搞错了,我从项目里抠出来的代码,这个问题应该好解决,你自己试着解决下。
1 楼 bigboy 2011-11-19  
兄台 你这样的做法测试过么 ?
至少这个
public void onClick(View v) { 
55.                // TODO Auto-generated method stub 
56.                text.remove(index); 
57.                notifyDataSetChanged(); 
58.                Toast.makeText(context, textView.getText().toString(), Toast.LENGTH_SHORT).show(); 
59.            } 

就会报错 !
Only the original thread that created a view hierarchy can touch its views.

相关推荐

Global site tag (gtag.js) - Google Analytics