Getting Listview Row Data
I have a listview in my application.I want to set the value of textview in that particular row when I click one of the textview in that row itself.so,I tried like below likes.setOn
Solution 1:
I'm not sure how you are populating your list with data, however here is a method I use that works very well.
Data Models
publicclassPublication {
publicString string1;
publicString string2;
publicPublication() {
}
publicPublication(String string1, String string2) {
this.string1= string1;
this.string2= string2;
}
}
Create an array adapter
publicclassContactArrayAdapterextendsArrayAdapter<ContactModel> {
privatestaticfinalStringtag="ContactArrayAdapter";
privatestaticfinalStringASSETS_DIR="images/";
private Context context;
//private ImageView _emotionIcon;private TextView _name;
private TextView _email;
private CheckBox _checkBox;
private List<ContactModel> contactModelList = newArrayList<ContactModel>();
publicContactArrayAdapter(Context context, int textViewResourceId,
List<ContactModel> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.contactModelList = objects;
}
publicintgetCount() {
returnthis.contactModelList.size();
}
public ContactModel getItem(int index) {
returnthis.contactModelList.get(index);
}
public View getView(int position, View convertView, ViewGroup parent) {
Viewrow= convertView;
if (row == null) {
// ROW INFLATION
Log.d(tag, "Starting XML Row Inflation ... ");
LayoutInflaterinflater= (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 itemfinalContactModelcontactModel= getItem(position);
Resourcesres=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(newCompoundButton.OnClickListener() {
@OverridepublicvoidonClick(View view) {
if (contactModel.IsChecked) {
contactModel.IsChecked = false;
notifyDataSetChanged();
}
else {
contactModel.IsChecked = true;
notifyDataSetChanged();
}
}
});
return row;
}
}
Use the array adapter to fill your list
ContactArrayAdapter contactArrayAdapter;
//
List<ContactModel> contactModelList;
//Fill list with your method
contactModelList = getAllPhoneContacts();
//
contactArrayAdapter = newContactArrayAdapter(getApplicationContext(), R.layout.contact_list_entry, contactModelList);
//setListAdapter(contactArrayAdapter);
A sample method to fill data:
public List<ContactModel> getAllPhoneContacts() {
Log.d("START","Getting all Contacts");
List<ContactModel> arrContacts = newStack<ContactModel>();
Uriuri= ContactsContract.CommonDataKinds.Email.CONTENT_URI;
Cursorcursor= getContentResolver().query(uri, newString[] {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));
Stringname= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
intphoneContactID= cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
if (email != null)
{
ContactModelcontactModel=newContactModel();
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;
}
Accessing the data on click
finalListViewlv= getListView();
lv.setTextFilterEnabled(true);
//Click handler for listview
lv.setOnItemClickListener(newOnItemClickListener() {
publicvoidonItemClick(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();
}
});
Post a Comment for "Getting Listview Row Data"