2013-07-19 2 views
0

두 개의 버튼이있는 ListView 항목이 있습니다. 여러 개의 ListView 항목이 있으므로 많은 버튼이 있습니다. 각 버튼에 다른 값을 할당하려면 어떻게합니까? 내가 원하는 것은 모든 버튼이 같은 액티비티로 연결된다는 것입니다.하지만 새로운 액티비티로 이동하면 버튼이 할당 된 값을 보냅니다. 그런 다음 활동이 값을 처리 할 수 ​​있습니다. 여기 내 baseAdapterlistView에서 버튼에 다른 값을 할당하는 방법 - Android

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; 
      } 

      } 
+0

그리고 당신의'Intent'는 텍스트로 설정 한 것과 동일하게 버튼에 할당 된 값을 원하는? 인 텐트에 추가 값을 입력하고 시작된 활동에서 값을 읽을 수 있습니다. – ozbek

+0

하지만 어떻게해야합니까? –

+0

귀하의 의도를 게시하십시오 :) – ozbek

답변

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); 
      } 
     }); 
} 
+0

일단 TargetActivity가 생성되면 어떻게 매개 변수를 검색합니까? –

+0

또한이 시도하고 작동하지 않는, 사용자 이름을 param 원하는 및 그것을 할 때 오류가 발생합니다. –

+0

추가 intent.setFlags (Intent.FLAG_ACTIVITY_SINGLE_TOP); TargetActivity에서 onNewIntent (Intent intent) 메소드를 대체하여 최종 의도를 얻을 수 있습니다. – JaredLuo

관련 문제