简体   繁体   English

Android在ListView的同一行上获取Textview文本

[英]Android get Textview text on the same row of listview

I have this code inside my BaseAdapter Adapter: 我的BaseAdapter适配器内部有以下代码:

public View getView(final int position, View convertView, ViewGroup parent) {
   View vi=convertView;

   if(convertView==null) 
       vi = inflater.inflate(R.layout.facebook_friends_layout, null);

   TextView friendsName = (TextView)vi.findViewById(R.id.tvFacebookName); // Friends thumb
   friend_image=(ImageView)vi.findViewById(R.id.ivFacebookPicture);  // thumb image

   song= new HashMap<String, String>();
   song = data.get(position);
   friendsName.setText(song.get(MyFriends.KEY_FBNAME));
   //Log.i("Facebook Pangalan", ""+MyFriends.KEY_FBNAME);

   FriendName = (String) friendsName.getText().toString();

   FBID = song.get(MyFriends.KEY_FBID); 
   String fbAvatarUrl = "http://graph.facebook.com/"+ FBID +"/picture";
   //Log.i("FBID", FBID);
   BitmapManager.INSTANCE.loadBitmap(fbAvatarUrl, friend_image, 100,100);
   Button btn=(Button)vi.findViewById(R.id.btnAdd);
   if (FBID.equals(""))
   {
       btn.setVisibility(View.GONE);
   }
   else 
   {
       btn.setVisibility(View.VISIBLE); 
   }
   btn.setOnClickListener(new OnClickListener(){ 

       public void onClick(View arg0) {
           LOCK_BASEDATA = MyFriends.LOCK_BASEDATA;
           if (LOCK_BASEDATA.equals("0")) 
           {
               //FACEBOOK FRIENDS
               Log.i("", ""+FriendName);
               //AddfromFacebookFriends();
           }
           else if (LOCK_BASEDATA.equals("1"))
           {
               //MY REQUEST
               //((MyFriends)context).SetMyRequest();
               //Log.i("LangYa", "Langya");
           }
           else if (LOCK_BASEDATA.equals("2"))
           {
               //FRIEND REQUEST
               //((MyFriends)context).SetFriendRequest();
               //d2papasok
               AddFriendRequest();
           }

       }

    });

   return vi;
}

I am logging my friend's name whenever I click on the button. 每当我单击按钮时,我就会记录我朋友的名字。 Now my problem starts when I click the button; 现在,当我单击按钮时,问题就开始了。 I am not getting my desired string. 我没有得到想要的字符串。 It is getting the string from another row of my listview. 它是从列表视图的另一行获取字符串。

EDIT Thanks, actually ive just aaded this code 编辑谢谢,实际上我已经将这段代码添加了

public void onClick(View arg0) {
                LOCK_BASEDATA = MyFriends.LOCK_BASEDATA;
                if (LOCK_BASEDATA.equals("0")) 
                {
                    //Object x =btn.getTag();
                    //String sa = x.toString();
                    View parentView = (View) arg0.getParent();
                    String textviewtext = ((TextView) parentView.findViewById(R.id.hiddentv)).getText().toString();
                    //FACEBOOK FRIENDS

                    AddfromFacebookFriends(textviewtext);

                }
                else if (LOCK_BASEDATA.equals("1"))
                {

I am just getting my parents View to get my desired textview and now it works. 我只是在获取我的父母视图以获取所需的textview,现在可以使用了。 Thanks anyway 不管怎么说,还是要谢谢你

You are trying to get the FriendName using this code: 您正在尝试使用以下代码获取FriendName:

TextView friendsName = (TextView)vi.findViewById(R.id.tvFacebookName);
FriendName = (String) friendsName.getText().toString();

This is not The FriendName of the ListView item you clicked. 这不是您单击的ListView项的FriendName。 Its the FriendName of the newly created ListView item. 它是新创建的ListView项的FriendName。 Because in getView(), the ListView item you get is the newly created one. 因为在getView()中,您获得的ListView项是新创建的项。

One way to solve this is: 解决此问题的一种方法是:

Use tags like KunalK suggested. 建议使用类似KunalK的标签。 I am not sure if it works...i never tried it. 我不确定它是否有效...我从未尝试过。

Or the other way is: 或另一种方式是:

onButtonClicked get the Position of the ListView item you clicked, and get the respective Name from the ArrayList you are using. onButtonClicked获取您单击的ListView项的Position ,并从您正在使用的ArrayList中获取相应的Name

Here the Position is not the position parameter in the getView(). 这里的Position不是getView()中的position parameter That gives the position of the newly created ListView item again. 这再次给出了新创建的ListView项的位置。

Use this to get the actual position of the clicked item: 使用它来获取单击项的实际位置:

int clickedItemPosition = yourListView.getPositionForView((View) yourButton.getParent());

you should go with this by setting the "FriendName" in your button's setTag property. 您应该通过在按钮的setTag属性中设置“ FriendName”来实现。 and whenever you handle your button click event fetch your FriendName String by using getTag property of button. 并且每当您处理按钮单击事件时,都将使用button的getTag属性获取FriendName字符串。 for eg something like this: 例如:

....
btn.setTag(FriendName);
btn.setOnClickListener(new OnClickListener(){ 
   public void onClick(View arg0) {
       LOCK_BASEDATA = MyFriends.LOCK_BASEDATA;
       if (LOCK_BASEDATA.equals("0")) 
       {
           //FACEBOOK FRIENDS
           Log.i("", ""+btn.getTag().toString());
           //AddfromFacebookFriends();
       }
       else if (LOCK_BASEDATA.equals("1"))
       {
           //MY REQUEST
           //((MyFriends)context).SetMyRequest();
           //Log.i("LangYa", "Langya");
       }
       else if (LOCK_BASEDATA.equals("2"))
       {
           //FRIEND REQUEST
           //((MyFriends)context).SetFriendRequest();
           //d2papasok
           AddFriendRequest();
       }

   }

});
....

Getting the name from the list can be accompished by using an onListItemClick method as follows ( not in your adapter, but in the class with your list): 可以通过使用如下的onListItemClick方法来完成从列表中获取名称的操作( 不是在适配器中,而是在列表中的类中):

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    TextView friendsName = (TextView)v.findViewById(R.id.tvFacebookName);   
    FriendName = (String) friendsName.getText().toString();    
}

Since this code is the onListItemClick method, the View v parameter is the view of the row you clicked. 由于此代码是onListItemClick方法,因此View v参数是您单击的行的视图。 Once you have that, you search that particular row view for the TextView you want and then pull the string from that. 一旦有了它,就可以在特定的行视图中搜索所需的TextView,然后从中拉出字符串。

You had the right idea, you just put it in the wrong place. 您有正确的想法,只是将其放在错误的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM