0

我有一个 ListView 项目,其中有 2 个按钮。有多个 ListView 项目,所以有很多按钮。如何为每个按钮分配不同的值?我想要发生的是每个按钮都会导致相同的活动,但是当它进入新活动时,它会发送一个按钮被分配的值。然后活动可以处理该值。我希望分配给按钮的值与文本设置的值相同,这是我的 baseAdapter

class CreateCommentLists extends BaseAdapter{
            Context ctx_invitation;
            String[] listComments;
            String[] listNumbers;
            String[] listUsernames;


            public CreateCommentLists(String[] comments, String[] usernames, String[] numbers, DashboardActivity context)
            {
                super();
                ctx_invitation = context;
                listComments = comments;
                listNumbers = usernames;
                listUsernames = numbers;
            }

            @Override
            public int getCount() {
                if(null == listComments)
                {
                return 0;
                }   

                // TODO Auto-generated method stub
                return listComments.length;
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return listComments[position];
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                View v = null;
                try
                {
                    String inflater = Context.LAYOUT_INFLATER_SERVICE;
                    LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
                    v = li.inflate(R.layout.list_item, null);

                    TextView commentView = (TextView)v.findViewById(R.id.listComment);
                    TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
                    TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);
                    Button usernameButton = (Button)v.findViewById(R.id.listUsernameButton);
                   Button numberButton = (Button)v.findViewById(R.id.listNumberButton);

                    commentView.setText(listComments[position]);
                    NumbersView.setText(listNumbers[position]);
                    usernamesView.setText(listUsernames[position]);
                    usernameButton.setText("Go to " + listUsernames[position]);
                    numberButton.setText("Go to " + listNumbers[position]);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
                return v;
            }

          }
4

1 回答 1

1
usernameButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
                intent.putExtra("param", listUsernames[position]);
                startActivity(intent);
            }
        });
}
于 2013-07-19T01:53:17.940 回答