我是android的新手。最近开始学习。
我动态创建了一个LinearLayout。我在LinearLayout中有以下字段: 1.客户名称 2.编辑按钮 3.删除按钮。
方向是水平的。
现在我想在单击删除按钮时删除单个记录。在这种情况下如何获取行ID,稍后我可以传递到数据库以删除记录。这是代码。
user_list_view_item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_main"
>
<TextView android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="50dp"
android:textColor="#FFFFFF"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<Button
android:id="@+id/userListEditButton"
style="@style/SpecialText"
android:layout_width="50dp"
android:layout_height="50dp"
android:typeface="monospace"
android:text="@string/save"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<Button
android:id="@+id/userListDeleteButton"
style="@style/SpecialText"
android:layout_width="50dp"
android:layout_height="50dp"
android:typeface="monospace"
android:text="@string/reset"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
UserAdapter.java
package com.rad.mobileshop.activities;
import java.sql.SQLException;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.rad.mobileshop.common.entities.User;
import com.rad.mobileshop.db.DBAdapter;
public class UserAdapter extends ArrayAdapter<User> {
Context context;
int layoutResourceId;
User data[] = null;
private DBAdapter mdb;
public UserAdapter(Context context, int layoutResourceId, User[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
mdb = new DBAdapter(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
holder.editButton = (Button) row
.findViewById(R.id.userListEditButton);
holder.deleteButton = (Button) row
.findViewById(R.id.userListDeleteButton);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
User user = data[position];
holder.txtTitle.setText(user.getName());
holder.editButton.setText("Edit");
holder.deleteButton.setText("Delete");
holder.editButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
System.out.println("hello Edit");
}
});
holder.deleteButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
System.out.println("hello delete ");
}
});
return row;
}
private void deleteUserDetails(int userId) {
try {
mdb.open();
mdb.deleteUserDetails(userId);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
static class UserHolder {
TextView txtTitle;
Button editButton;
Button deleteButton;
}
}
User.java
package com.rad.mobileshop.common.entities;
public class User {
private int id;
private String name;
private String type;
public User() {
super();
}
public User(int id, String name, String type) {
super();
this.id = id;
this.name = name;
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return this.name ;
}
}
答案 0 :(得分:1)
首先您需要将User[]
数组更改为ArrayList,因为您无法动态删除基本数组的元素。 (至少不那么容易,在这种情况下,ArrayAdapter已经设计为可以与ArrayLists一起使用。)
下一步让我们在deleteButton
getView()
中保存位置,以便于访问:
holder.deleteButton.setTag(position);
上次将此添加到您的删除OnClickListener:
public void onClick(View view) {
User user = getItem((Integer) view.getTag()));
remove(user);
deleteUserDetails(user.getId());
}
我没有涉及一些小调整,例如更改UserAdapter的构造函数和data[position]
中的getView()
,但它们很容易修复。
可选建议:
getView()
data
的副本,它已存储在ArrayAdapter中,只需使用getItem(position)
访问它。getView()
块来简化if(row == null)
。所有这一切,如果您正在使用数据库,我不确定为什么要扩展ArrayAdapter。我建议切换到自定义CursorAdapter,但这不是必需的。