ListView 使用
package com.example.ex_2; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.Intent; import android.net.Uri; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class Remark extends AppCompatActivity { public static String TAG = "mydebug"; private ListView mListView; private List<BeanRemark> datas = new ArrayList<BeanRemark>(); private RemarkAdopter adopter; private View view; ViewHoder viewHoder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_re_mark); initDatas(); //获取listview mListView = (ListView) findViewById(R.id.lv1); //设置适配器 adopter = new RemarkAdopter(this, datas); mListView.setAdapter(adopter); this.registerForContextMenu(mListView); } private void initDatas() { BeanRemark data; data = new BeanRemark(1, "通知1", "2019.12.16", "要去学习"); datas.add(data); data = new BeanRemark(1, "通知2", "2019.10.16", "要去学习"); datas.add(data); data = new BeanRemark(1, "通知3", "2019.11.16", "要去学习"); datas.add(data); data = new BeanRemark(1, "通知4", "2019.10.16", "要去学习"); datas.add(data); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("选择操作"); menu.add(1, 1, 0, "增加"); menu.add(1, 2, 1, "编辑"); menu.add(1, 3, 2, "删除"); view = this.getLayoutInflater().inflate(R.layout.dialog_edit, null); } public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); int id = menuInfo.position; BeanRemark data = (BeanRemark) datas.get(id); Log.d(TAG, "postion: " + id); AlertDialog.Builder builder = new AlertDialog.Builder(this); // setContentView(R.layout.dialog_edit); viewHoder = new ViewHoder(); switch (item.getItemId()) { case 1: viewHoder.title = (EditText) view.findViewById(R.id.etx1); viewHoder.time = (EditText) view.findViewById(R.id.etx2); viewHoder.content = (EditText) view.findViewById(R.id.etx3); builder.setView(view); builder.setTitle("添加"); builder.setPositiveButton("确定", new addClick()); builder.setNegativeButton("取消", new exitClick()); builder.create(); builder.show(); break; case 2: viewHoder.title = (EditText) view.findViewById(R.id.etx1); viewHoder.time = (EditText) view.findViewById(R.id.etx2); viewHoder.content = (EditText) view.findViewById(R.id.etx3); builder.setView(view); builder.setTitle("编辑").setView(view); Log.d(TAG, "onContextItemSelected:"+data.toString() + "id = " + id); viewHoder.title.setText(data.getTitle()); viewHoder.time.setText(data.getTime()); viewHoder.content.setText(data.getContent()); builder.setPositiveButton("确定", new editClick(id)); builder.setNegativeButton("取消", new exitClick()); builder.create(); builder.show(); break; case 3: builder.setTitle("删除"); builder.setPositiveButton("确定", new delClick(id)); builder.setNegativeButton("取消", new exitClick()); builder.create(); builder.show(); break; } return super.onContextItemSelected(item); } //添加元素 class addClick implements DialogInterface.OnClickListener { BeanRemark data; @Override public void onClick(DialogInterface dialog, int which) { data = new BeanRemark(1, viewHoder.title.getText().toString(), viewHoder.time.getText().toString(), viewHoder.content.getText().toString()); Log.d(TAG, "onClick: " + data.toString()); datas.add(data); adopter.notifyDataSetChanged(); } } //编辑元素 class editClick implements DialogInterface.OnClickListener { BeanRemark data; int id; public editClick(int id) { this.id = id; } @Override public void onClick(DialogInterface dialog, int which) { data = new BeanRemark(1, viewHoder.title.getText().toString(), viewHoder.time.getText().toString(), viewHoder.content.getText().toString()); datas.set(id, data); adopter.notifyDataSetChanged(); } } class exitClick implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { adopter.notifyDataSetChanged(); } } class delClick implements DialogInterface.OnClickListener { int id; public delClick(int id) { this.id = id; } @Override public void onClick(DialogInterface dialog, int which) { datas.remove(id); adopter.notifyDataSetChanged(); } } private class ViewHoder { EditText title,time,content; } }
通过对话框实现长按编辑
package com.example.ex_2; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import java.util.List; public class phoneAdapter extends BaseAdapter { private Context context; private List<BeanPhone> datas; public phoneAdapter(Context context, List<BeanPhone> datas) { this.context = context; this.datas = datas; } //返回子项数量 @Override public int getCount() { return datas.size(); } //返回子项 @Override public Object getItem(int postion) { return datas.get(postion); } //返回下标 @Override public long getItemId(int postion) { return postion; } //返还视图 @Override public View getView(int postion, View convertView, ViewGroup parent) //子项下标,当前视图,父项视图 { BeanPhone item = (BeanPhone) getItem(postion); View view; ViewHoder viewHoder; if(convertView == null) { view = LayoutInflater.from(context).inflate(R.layout.list_tiem, null); viewHoder = new ViewHoder(); viewHoder.img = (ImageView) view.findViewById(R.id.img); viewHoder.name = (TextView) view.findViewById(R.id.tv1); viewHoder.num = (TextView) view.findViewById(R.id.tv2); viewHoder.cname = (TextView) view.findViewById(R.id.tv3); view.setTag(viewHoder); } else { view = convertView; viewHoder = (ViewHoder) view.getTag(); } viewHoder.name.setText(item.getName()); viewHoder.img.setImageResource(item.getImgId()); viewHoder.num.setText(item.getNum()); viewHoder.cname.setText(item.getCname()); return view; } class ViewHoder { ImageView img; TextView name,num,cname; } }
适配器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="50dp"
android:layout_height="20dp"
android:textSize="15dp"
android:text="title"
/>
<EditText
android:id="@+id/etx1"
android:layout_width="200dp"
android:layout_height="30dp"
android:paddingTop="0dp"
android:textSize="15dp"
/>
<TextView
android:layout_width="50dp"
android:layout_height="20dp"
android:textSize="15dp"
android:text="notice time"
/>
<EditText
android:id="@+id/etx2"
android:paddingTop="0dp"
android:layout_width="200dp"
android:layout_height="30dp"
android:textSize="15dp"
/>
<TextView
android:layout_width="50dp"
android:layout_height="20dp"
android:textSize="15dp"
android:text="content"
/>
<EditText
android:id="@+id/etx3"
android:layout_width="200dp"
android:layout_height="30dp"
android:paddingTop="0dp"
android:textSize="15dp" />
</LinearLayout>
对话框布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:width="0dp" android:orientation="horizontal" android:gravity="right"> <TextView android:id="@+id/title" android:width="0dp" android:layout_weight="1.0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20sp" android:text="通讯录标题"/> <TextView android:id="@+id/noticeTime" android:textSize="10sp" android:width="0dp" android:layout_weight="3.0" android:layout_width="match_parent" android:layout_height="match_parent" android:text="time"/> </LinearLayout> <TextView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="20dp" android:text="通讯录内容hhhhhhhhhhhhhhhhhhhh"/> </LinearLayout>
子项

浙公网安备 33010602011771号