Highlighted Item On Listview Gone When New Message Arrived Android Studio
I have a listview and it automatically filter all sim messages from mobile then I highlighted the items on the listview through clicking and it works, but the problem is when there
Solution 1:
Create a global array called isSelected like this
privateboolean[] isSelected;
Then assign your array with the size like this
isSelected=new boolean[arrayAdapter.getCount()]; // Do this after setting adapter
and .setOnItemClickListener on listview and when the click happens to make sure selected of that index is set to true like this
listView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
isSelected[position]=!isSelected[position];
}
});
And in receive_data() select those selected positions again
publicvoidreceive_data(final String smsMsg) {
arrayAdapter = newArrayAdapter(this,R.layout.list_item, list_items);
text_listview.setAdapter(arrayAdapter);
arrayAdapter.add(smsMsg);
arrayAdapter.notifyDataSetChanged();
boolean[] tempSelected=newboolean[arrayAdapter.getCount()];
for(int i=0;i<isSelected.length;i++)
{
tempSelected[i]=isSelected[i];
if(tempSelected[i])
{
text_listview.setItemChecked(i,true);
}
}
isSelected=tempSelected;
}
Post a Comment for "Highlighted Item On Listview Gone When New Message Arrived Android Studio"