0

我的应用程序中有一个列表视图。当我单击该行本身中的一个文本视图时,我想在该特定行中设置文本视图的值。所以,我尝试如下

likes.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {

           TextView t=(TextView)v;
          TextView likesnumber1 = (TextView) findViewById(R.id.likesnumber);
            int i=  Integer.parseInt(likescount.get(position));

       if(like_or_ulike.get(position).equals("Like")){
            Log.e("inlike","like");
            like_or_ulike.set(position, "Unlike");
            t.setText(like_or_ulike.get(position));
            UrltoValue.getValuefromUrl("https://graph.facebook.com/"+objectid.get(position)+"/likes?access_token="+accesstoken+"&method="+"post");

           j=i+1;
           String s=Integer.toString(j);
        likescount.set(position, s);
         likesnumber1.setText(likescount.get(position));

        }
        else{
            Log.e("unlike","unlike");
            like_or_ulike.set(position, "Like");
            t.setText(like_or_ulike.get(position));
            UrltoValue.getValuefromUrl("https://graph.facebook.com/"+objectid.get(position)+"/likes?access_token="+accesstoken+"&method="+"DELETE");

               j=i-1;
             String s=Integer.toString(j);
            likescount.set(position, s);
             likesnumber1.setText(likescount.get(position));
        }
    }
});

the "likes" reference which I used is textview and I want to set the textview by getting the id of that particular row.


TextView likesnumber1 = (TextView) findViewById(R.id.likesnumber); 

当我使用它时,我得到了屏幕第一个可见行的 id。

如何在 textview 单击时获取该特定行的 textview 的 ID。谢谢

4

1 回答 1

0

我不确定您是如何用数据填充列表的,但是这是我使用的一种非常有效的方法。

数据模型

public class Publication {

public String string1;
public String string2;

public Publication()  {

}
public Publication(String string1, String string2) {
this.string1= string1;
this.string2= string2;
}


}

创建阵列适配器

public class ContactArrayAdapter extends ArrayAdapter<ContactModel> {
private static final String tag = "ContactArrayAdapter";
private static final String ASSETS_DIR = "images/";
private Context context;
    //private ImageView _emotionIcon;
private TextView _name;
private TextView _email;
private CheckBox _checkBox;

private List<ContactModel> contactModelList = new ArrayList<ContactModel>();
public ContactArrayAdapter(Context context, int textViewResourceId,
                           List<ContactModel> objects) {
    super(context, textViewResourceId, objects);
    this.context = context;
    this.contactModelList = objects;
}

public int getCount() {
    return this.contactModelList.size();
}
public ContactModel getItem(int index) {
    return this.contactModelList.get(index);
}
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row == null) {
        // ROW INFLATION
        Log.d(tag, "Starting XML Row Inflation ... ");
        LayoutInflater inflater = (LayoutInflater) this.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.contact_list_entry, parent, false);
        Log.d(tag, "Successfully completed XML Row Inflation!");
    }

    // Get item
   final ContactModel contactModel = getItem(position);
    Resources res = this.getContext().getResources();
        //Here are some samples so I don't forget...
        //
        //_titleCount = (TextView) row.findViewById(R.id.category_count);

        //  _category.setText(categories1.Category);
        //
        //if (categories1.Category.equals("Angry")) {
        //Drawable angry = res.getDrawable(R.drawable.angry);
        //_emotionIcon.setImageDrawable(angry);
        //}
    _checkBox = (CheckBox) row.findViewById(R.id.contact_chk);
    _email = (TextView) row.findViewById(R.id.contact_Email);
    _name = (TextView)row.findViewById(R.id.contact_Name);

     //Set the values
    _checkBox.setChecked(contactModel.IsChecked);
    _email.setText(contactModel.Email);
    _name.setText(contactModel.Name);

    _checkBox.setOnClickListener(new CompoundButton.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (contactModel.IsChecked) {
               contactModel.IsChecked = false;
               notifyDataSetChanged();
            }
            else {
                contactModel.IsChecked = true;
                notifyDataSetChanged();
            }

        }
    });

    return row;
}

}

使用数组适配器填充您的列表

   ContactArrayAdapter contactArrayAdapter;
   //
   List<ContactModel> contactModelList;
   //Fill list with your method
   contactModelList = getAllPhoneContacts();
   //
   contactArrayAdapter = new ContactArrayAdapter(getApplicationContext(),          R.layout.contact_list_entry, contactModelList);
   //
  setListAdapter(contactArrayAdapter);

填充数据的示例方法:

    public List<ContactModel> getAllPhoneContacts() {
    Log.d("START","Getting all Contacts");
    List<ContactModel> arrContacts = new Stack<ContactModel>();

    Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    Cursor cursor = getContentResolver().query(uri, new String[] {ContactsContract.CommonDataKinds.Email.DATA1
            ,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
            ,ContactsContract.CommonDataKinds.Phone._ID}, null , null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    cursor.moveToFirst();
    while (cursor.isAfterLast() == false)
    {
        String email= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        String name =  cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        int phoneContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));




        if (email != null)
        {
            ContactModel contactModel = new ContactModel();
            contactModel.Name = name;
            contactModel.Email = email;
            contactModel.IsChecked = false;
            arrContacts.add(contactModel);
        }

        cursor.moveToNext();
    }
    cursor.close();
    cursor = null;
    Log.d("END","Got all Contacts");
    return arrContacts;
}

点击访问数据

final ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        //Click handler for listview
    lv.setOnItemClickListener(new OnItemClickListener() {
       public void onItemClick(AdapterView parent, View view, int position, long id) {
            ContactModel contact= getItem(position);//This gets the data you want to change
            //
            some method here tochange set data
            contact.email = "new@email.com"
            //send notification
             contactArrayAdapter.notifyDataSetChanged();
        }
    });
于 2012-11-22T14:09:16.353 回答