Change ListView background - strange behaviour
Let me show you the code that I use for every ListView
and properly controlling the click event for changing the background and doing anything further
public class Offices extends Activity {
private ListView listView;
/* selectedListItem will contain the number of items to be selected.
* Your list item OnOlickListener will simply change this variable
* to the position of the clicked item. The Adapter will do the rest
* because you need to refresh the ListView.
*/
private int selectedListItem = -1;
private Handler mHandler = new Handler();
private Vector<String> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.officeslayout);
data = new Vector<String>();
// Add data as per your requirement
data.add("one");
data.add("two");
data.add("three");
data.add("four");
data.add("Five");
data.add("Six");
data.add("Seven");
data.add("Eight");
data.add("Nine");
data.add("Ten");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
listView.setAdapter(new EfficientAdapter(getApplicationContext()));
}
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.officeslistitemlayout, null);
holder = new ViewHolder();
holder.backgroundView = (ImageView) convertView
.findViewById(R.id.OfficesBackground);
holder.officesTitle = (TextView) convertView
.findViewById(R.id.OfficesName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position == selectedListItem) {
holder.backgroundView.setBackgroundResource(R.drawable.and_gray_bg_listing_selected);
} else {
holder.backgroundView.setBackgroundResource(R.drawable.and_gray_bg_listing);
}
holder.officesTitle.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView officesTitle;
ImageView backgroundView;
}
}
officeslistitemlayout.xml file will be like following add drawable and design it according to you put the following code in RelativeLayout
<ImageView android:id="@+id/OfficesBackground" android:layout_width="fill_parent"
android:layout_height="45dip"
android:layout_alignParentTop="true"
android:background="@drawable/and_gray_bg_listing"
android:scaleType="fitXY"
></ImageView>
<TextView android:id="@+id/OfficesName" android:layout_width="wrap_content"
android:text="Offices Name"
android:textColor="#000000" android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_marginLeft="5dip"
></TextView>
Hope it will help :)
ExpandableListView item background color weird behavior
i consider ConciergeOptionsPairs as your child data so add one boolean variable isSelect in ConciergeOptionsPairs class then in your onChildClick
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
ArrayList<ConciergeOptionsPairs> children=data.Options.get(groupPosition).childrens; //i consider this is your child view array, need to be same dataset which used as child array
int count=0;
for(ConciergeOptionsPairs objChild:children)
{
if(count==childPosition)
objChild.isSelect=true;
else
objChild.isSelect=false;
count++;
}
yourexpandableadpater.notifyDataSetChanged();
return false;
}
});
now in your adapter in getChildView
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.concierge_options_inner_items, null);
}
TextView option_type = (TextView) convertView.findViewById(R.id.option_type);
TextView price = (TextView) convertView.findViewById(R.id.price);
TextView decimal = (TextView) convertView.findViewById(R.id.decimal);
TextView currency = (TextView) convertView.findViewById(R.id.currency);
ConciergeOptionsPairs option_pair = (ConciergeOptionsPairs) getChild(groupPosition, childPosition);
option_type.setText(option_pair.name);
double t_price = Double.parseDouble(option_pair.value);
double dec = t_price - (long) t_price;
long decim = (long) dec;
int decimal_int = (int) t_price;
price.setText("+" + String.valueOf(decimal_int));
decimal.setText("." + String.valueOf(decim));
currency.setText("$");
if(option_pair.isSelect)
convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.orange));
else
convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.transparent));
return convertView;
}
this is what i understand your code you can check with this
Weird behavior of ListView item and state selector background drawable
When working with ListView
it is very important to always keep in mind that the views are the presentation and the adapter is the data model.
This means that all of your state should be in the adapter (the data model), not in the views.
From what I can tell of your code, you have a view that is showing a check state, and that state is in the view not in the adapter. That is, when the user clicks on that item in the list, the view being used to display its item has its internal checked state changed to toggle what is shown to the user.
But since the view is not the data model, this state you are playing with here is transient and not actually associated with the adapter item being clicked.
The most obvious problem this causes comes in with view recycling. When you scroll through a ListView
, when items scroll off the end and new ones appear at the bottom, the views used to display the old items are re-used to display the new ones. This is much more efficient than having to inflate a new item view hierarchy every time a new item is shown.
Because you have your state in the view, when this recycling happens that state in the view gets randomly re-associated with some new item. This can happen in many cases, not just scrolling.
The solution is to put your check state in the adapter, and implement Adapter.getView()
to set the checked state of the view based on the state you now have in the adapter. That way whenever a view is recycled (and getView()
called to bind the new data row to it), you will update its checked state to correctly follow the new data it is displaying.
Strange behavior of in List View click with Section Header
try this. for click listener
holder.like_icon.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
ViewHolder VH = (ViewHolder)convertView.getTag();
VH.like_icon.setBackgroundResource(android.R.drawable.btn_star_big_on);
}
});
Strange behaviour in Android listview when loading images with asynctask
Actually main problem is you didn't add else condition statement in most of your if
condition statements in getView()
method of custom adapter.
Like,
put
else
hereif (chatContact.Picture != null) {
BitmapWorkerTask bitmapWorker = new BitmapWorkerTask(contactImageView, diskImageLoader);
bitmapWorker.execute(chatContact.Picture);
}
else
{
contactImageView.setImageResource(R.id.icon); // only for your reference.. add default image
}Add
else
statement in your allif-else if
statement for list item row views. Either make them gone in visibility or assign blank text or default images.
Update: As I doubt this scenario because of reference view of list row.
remove this code,
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.chat_contact_list_row, null);
}
Just use,
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.chat_contact_list_row, null);
Related Topics
"Java Dateformat Is Not Threadsafe" What Does This Leads To
How to Trim a File Extension from a String in Java
Background Timer Task in Jsp/Servlet Web Application
Absolute Positioning Graphic JPAnel Inside Jframe Blocked by Blank Sections
How to Convert Image to Byte Array in Java
Static VS Instance Variables: Difference
Math.Random, Only Generating a 0
Getting Spring Application Context
How to Make an Array of Arrays in Java
Java.Sql.Sqlexception: Column Count Doesn't Match Value Count at Row 1
How to Access a Folder Inside of a Resource Folder from Inside My Jar File
Null Check Chain VS Catching Nullpointerexception
How to Limit the Number of Characters in Jtextfield