您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在Android中,要实现ListView的项编辑功能,你可以采用以下步骤:
list_item_edit.xml
的布局文件:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="Enter text"/>
<Button
android:id="@+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
EditableListAdapter
的类:public class EditableListAdapter extends BaseAdapter {
private List<String> data;
private LayoutInflater inflater;
public EditableListAdapter(Context context, List<String> data) {
this.data = data;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_edit, parent, false);
holder = new ViewHolder();
holder.editText = convertView.findViewById(R.id.edit_text);
holder.buttonSave = convertView.findViewById(R.id.button_save);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.editText.setText(data.get(position));
holder.buttonSave.setOnClickListener(v -> {
String newText = holder.editText.getText().toString();
data.set(position, newText);
notifyDataSetChanged();
});
return convertView;
}
static class ViewHolder {
EditText editText;
Button buttonSave;
}
}
MainActivity
中:public class MainActivity extends AppCompatActivity {
private ListView listView;
private EditableListAdapter adapter;
private List<String> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
// 初始化数据
data = new ArrayList<>();
data.add("Item 1");
data.add("Item 2");
data.add("Item 3");
// 设置适配器
adapter = new EditableListAdapter(this, data);
listView.setAdapter(adapter);
}
}
activity_main.xml
中:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
现在,当你运行应用程序时,你应该可以看到一个包含可编辑项的ListView。用户可以在EditText中输入新文本,然后点击“Save”按钮来保存更改。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。