在 android 中向列表视图的每一行添加一个按钮?
Adding a button to each row of a list view in android?
我想向列表视图的每一行添加两个按钮。我想将它们添加到自定义适配器中。这两个按钮用于编辑和删除。以下是我的适配器代码。
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();
}
}
场景:
- 用户点击
add user
按钮一个对话框打开
- 用户输入必填字段并点击注册
- 列表视图中添加了一行。
屏幕截图:
对话框:
最终画面:
现在我想添加两个按钮 edit
和 delete
。允许
用户编辑和删除添加到列表视图的行。
对不起我的英语。
简单地说,您应该在添加 TextView 时在 "role_list.xml" 中添加按钮。然后您可以在适配器 class 的按钮上添加 onClickListner。
希望对你有所帮助
首先您需要重新编辑 R.layout.role_list
以添加您的按钮。我假设您知道如何将按钮添加到布局。之后,在您的 getView()
方法中,您需要定义按钮并为它们设置点击侦听器。
@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);
Button edit = (Button ) listViewItem.findViewById(R.id.edit_button);
Button delete = (Button ) listViewItem.findViewById(R.id.delete_button);
textViewName.setText(name[position]);
textViewusername.setText(username[position]);
textViewPass.setText(password[position]);
textViewRole.setText(role[position]);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yourList.remove(position);
notifyDataSetChanged();
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// May be you can show a popup here
}
});
return listViewItem;
}
希望它能满足您的需求
最简单的解决方案是将 String[] name
更改为 ArrayList<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;
}
希望对您有所帮助
我想向列表视图的每一行添加两个按钮。我想将它们添加到自定义适配器中。这两个按钮用于编辑和删除。以下是我的适配器代码。
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();
}
}
场景:
- 用户点击
add user
按钮一个对话框打开 - 用户输入必填字段并点击注册
- 列表视图中添加了一行。
屏幕截图:
最终画面:
现在我想添加两个按钮 edit
和 delete
。允许
用户编辑和删除添加到列表视图的行。
对不起我的英语。 简单地说,您应该在添加 TextView 时在 "role_list.xml" 中添加按钮。然后您可以在适配器 class 的按钮上添加 onClickListner。 希望对你有所帮助
首先您需要重新编辑 R.layout.role_list
以添加您的按钮。我假设您知道如何将按钮添加到布局。之后,在您的 getView()
方法中,您需要定义按钮并为它们设置点击侦听器。
@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);
Button edit = (Button ) listViewItem.findViewById(R.id.edit_button);
Button delete = (Button ) listViewItem.findViewById(R.id.delete_button);
textViewName.setText(name[position]);
textViewusername.setText(username[position]);
textViewPass.setText(password[position]);
textViewRole.setText(role[position]);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yourList.remove(position);
notifyDataSetChanged();
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// May be you can show a popup here
}
});
return listViewItem;
}
希望它能满足您的需求
最简单的解决方案是将 String[] name
更改为 ArrayList<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;
}
希望对您有所帮助