在android中列表视图的每一行添加一个按钮?

时间:2016-04-22 07:26:40

标签: java android listview android-arrayadapter

我想在列表视图的每一行添加两个按钮。我想在自定义适配器中添加它们。这两个按钮用于编辑和删除。以下是我的适配器代码。

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. 用户点击add user按钮打开一个对话框
  2. 用户输入必填字段并点击注册
  3. 将一行添加到列表视图中。
  4. 截屏: enter image description here 对话框: enter image description here

    最终屏幕: enter image description here

    现在我想添加两个按钮editdelete。允许的 用户编辑和删除添加到列表视图的行。

3 个答案:

答案 0 :(得分:1)

首先,您需要重新编辑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;
}

希望它能满足您的需求

答案 1 :(得分:1)

最简单的解决方案是您应将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;
     }

希望这个帮助

答案 2 :(得分:0)

抱歉我的英语。 只需在添加TextView时在“role_list.xml”中添加按钮。然后你可以在你的适配器类的按钮上添加onClickListner。 我希望它会对你有所帮助。