gpt4 book ai didi

java - 在android中的 ListView 的每一行添加一个按钮?

转载 作者:行者123 更新时间:2023-11-30 02:57:44 25 4
gpt4 key购买 nike

我想向 ListView 的每一行添加两个按钮。我想将它们添加到自定义适配器中。这两个按钮分别用于编辑和删除。以下是我的适配器代码。

public class RoleList extends ArrayAdapter<String>
{
private String[] name;
private String[] username;
private String[] password;
private String[] role;
private Activity context;

public RoleList(Activity context, String[] name, String[] username, String[] password, String[] role)
{
super(context, R.layout.role_list,name);
this.context = context;
this.name = name;
this.username =username;
this.password = password;
this.role = role;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.role_list, null, true);
TextView textViewName = (TextView) listViewItem.findViewById(R.id.tv_empname);
TextView textViewusername = (TextView) listViewItem.findViewById(R.id.tv_empusername);
TextView textViewPass = (TextView) listViewItem.findViewById(R.id.tv_emppassword);
TextView textViewRole = (TextView) listViewItem.findViewById(R.id.tv_emprole);


textViewName.setText(name[position]);
textViewusername.setText(username[position]);
textViewPass.setText(password[position]);
textViewRole.setText(role[position]);



return listViewItem;
}
}

列表的处理程序:-

  public void userRolehandler()
{
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run()
{
RoleList roleList = new RoleList(UserRegistration.this,employee_name,emp_username,emp_password,employee_role);

userList.setAdapter(roleList);
roleList.notifyDataSetChanged();


}
});
}

//用户角色数组

 public void userRoleArray() throws JSONException {
name = editTextName.getText().toString();
username = editTextUsername.getText().toString();
password = editTextPassword.getText().toString();
emp_role = textRole.getText().toString();

JSONObject jsonobj = new JSONObject();
jsonobj.put("name",name);
jsonobj.put("username",username);
jsonobj.put("password",password);
jsonobj.put("emp_role",emp_role);

rolejson.put(counter,jsonobj);
counter++;
}

//travRoleArray

  public void travRoleArray() throws JSONException {
response = "{\"result\":" + rolejson.toString() + "}";
Log.d("RESPONSE",response);
JSONObject jsonOb = new JSONObject(response);
JSONArray result = jsonOb.getJSONArray(JSON_ARRAY);
try
{
for (int i=0; i< result.length();i++)
{
JSONObject jObj = result.getJSONObject(i);
employee_name[i] = jObj.getString(KEY_NAME);
emp_username[i] = jObj.getString(KEY_UNAME);
emp_password[i] = jObj.getString(KEY_PASS);
employee_role[i] = jObj.getString(KEY_ROLE);
}

}catch (ArrayIndexOutOfBoundsException e)
{
Toast.makeText(UserRegistration.this, "Contact customer care for assigning more roles", Toast.LENGTH_LONG).show();
}

}

场景:

  1. 用户单击添加用户按钮,将打开一个对话框
  2. 用户输入必填字段并点击注册
  3. 一行被添加到 ListView 中。

屏幕截图: enter image description here对话框: enter image description here

最终屏幕: enter image description here

现在我想添加两个按钮编辑删除。为了允许用户可以编辑和删除添加到 ListView 中的行。

最佳答案

最简单的解决方案是您应该更改 String[] nameArrayList<String> name

public class RoleList extends ArrayAdapter<String>{
//private String[] name;
private ArrayList<String> name;

// it show how many items are in the data set represented by this Adapter.
@Override
public int getCount(){
return name.size();
}
}

当前您使用String[] ,并且其大小是固定的

String[] toppings = new String[10];
System.out.println("Hello World"+toppings.length); // will print 10

因此你的ListView始终显示 10 个项目(甚至有些是空的)

但是如果你使用ArrayList<String> ,当您添加或删除项目时,列表的大小会自动更改,然后您的 ListView总行数 = 列表大小

如果您仍想使用String[]
您可以创建一个新的 int变量如 realTotalFriends (从0开始)并且每次添加新用户时(只需将其增加1)
然后里面getCount()return realTotalFriends

     int realTotalFriends;
@Override
public int getCount(){
return realTotalFriends;
}

希望这有帮助

关于java - 在android中的 ListView 的每一行添加一个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36787562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com