ListView item中按钮点击实现删除
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView item中按钮点击实现删除相关的知识,希望对你有一定的参考价值。
ListView item中按钮点击实现删除
在处理ListView 中item的按钮点击事件首先需要在item的根布局中添加
android:descendantFocusability="blocksDescendants"属性,否则item的点击事件会把其中image button的点击事件屏蔽掉
item的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:textColor="#333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="10dp"
android:textSize="18sp"
android:id="@+id/tv_title"
android:text="我的日记"/>
<TextView
android:layout_below="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/tv_content"
android:textColor="#333333"
android:text="内容"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:id="@+id/bt_delect"/>
</RelativeLayout>
</LinearLayout>
主页面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.notetext.ui.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/bt_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:onClick="add"
android:text="添加" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我的日记"
android:textSize="18sp" />
<TextView
android:id="@+id/bt_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:onClick="set"
android:text="设置" />
</RelativeLayout>
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
代码:
package com.example.notetext.ui;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.notetext.modul.Content;
import com.example.notetext.R;
import java.io.Serializable;
import java.util.ArrayList;
public class MainActivity extends Activity {
private ListView list;
private ArrayList<Content> arraylist = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.lv_list);
initData();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
Content context = arraylist.get(position);
Toast.makeText(getApplicationContext(), "条目点击了", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), LookActivity.class);
intent.putExtra("content", (Serializable) context);
startActivity(intent);
}
});
}
private MyAdapter adapter;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getApplicationContext(), "zhixing", Toast.LENGTH_SHORT).show();
if (data != null) {
switch (requestCode) {
case 1:
Toast.makeText(getApplicationContext(), "zhixing", Toast.LENGTH_SHORT).show();
Content content = (Content) data.getExtras().getSerializable("content");
arraylist.add(content);
MyAdapter adapter = new MyAdapter(arraylist);
list.setAdapter(adapter);
break;
}
}
}
private void initData() {
MyAdapter adapter = new MyAdapter(arraylist);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public void add(View view) {
Intent intent = new Intent(this, ChangeActivity.class);
startActivityForResult(intent, 1);
}
public void set(View view) {
Intent intent = new Intent (this,SettingActivity.class);
startActivity(intent);
}
public class MyAdapter extends BaseAdapter {
private ArrayList<Content> arrayList;
public MyAdapter(ArrayList<Content> arrayList) {
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
Holder holder;
if (convertView == null) {
convertView = View.inflate(getApplicationContext(), R.layout.item_note, null);
holder = new Holder(convertView);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
final Content content = arrayList.get(position);
holder.tv_title.setText(content.getTitle());
holder.tv_content.setText(content.getContent());
holder.bt_delect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//将选中的条目在arrayList中移除
arrayList.remove(position);
//刷新界面
initData();
Log.e("删除条目","33333333333333");
}
});
return convertView;
}
class Holder {
private TextView tv_title;
private TextView tv_content;
private Button bt_delect;
public Holder(View convertView) {
tv_title =(TextView)convertView.findViewById(R.id.tv_title);
tv_content =(TextView)convertView.findViewById(R.id.tv_content);
bt_delect = (Button) convertView.findViewById(R.id.bt_delect);
}
}
}
}
以上是关于ListView item中按钮点击实现删除的主要内容,如果未能解决你的问题,请参考以下文章
listview中的item是自定义的含有textview和edittext 还有button 点击listview外的按钮增加一个item问题
关于adapter里getview的按钮点击,使控件隐藏显示的问题