Handling onClick with efficient ListView adapter - java

Here's my getView function in a custom class extending ArrayAdapter.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Thing p = getItem(position);
Thing.ThingStatus thingStatus = p.getStatus();
int thingStatusIcon; // change to thingStatusResource
switch (thingStatus) {
case A:
thingStatusIcon = ICON_A;
break;
case B:
thingStatusIcon = ICON_B;
break;
default:
thingStatusIcon = ICON_C;
break;
}
// Check if an existing view is being reused, otherwise inflate the view
ThingRowHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
viewHolder = new ThingRowHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.thing_list_item, parent, false);
// put these on the top like the colors
viewHolder.thingIcon = (ImageView) convertView.findViewById(R.id.thing_icon);
viewHolder.thingId = (TextView) convertView.findViewById(R.id.thing_id_item);
viewHolder.thingStatus = (TextView) convertView.findViewById(R.id.thing_status_item);
// Cache the viewHolder object inside the fresh view
convertView.setTag(viewHolder);
} else {
// View is being recycled, retrieve the viewHolder object from tag
viewHolder = (ThingRowHolder) convertView.getTag();
}
viewHolder.thingId.setText(p.getLpId());
viewHolder.thingStatus.setText(thingStatus.toString());
viewHolder.thingIcon.setImageResource(thingStatusIcon);
viewHolder.position = position;
return convertView;
}
I'm trying to add an onClick event listener for each row in my ListView. I tried setting the listener on convertView both inside the if statement and outside the else statement. I got mixed results. Sometimes the onClick wouldn't fire at all. Other times it would report the wrong position. I've scoured SO for examples of this and I'm surprised such a common functionality isn't well documented and/or not working in my case.
Any help would be much appreciated!
edit:
private class ThingRowHolder {
ImageView thingIcon;
TextView thingId;
TextView thingStatus;
}
edit 2:
private void refreshList() {
ArrayList<Thing> things = mThingContainer.getThings();
Collections.sort(things);
mThingListAdapter.refreshThings(things);
}
edit 3:
mThingListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ThingListAdapter.ThingRowHolder holder =
(ThingListAdapter.ThingRowHolder) view.getTag();
Thing p = mThingContainer.getThings().get(holder.position);
}
});

Set position as tag to your view,
convertview.setTag(R.id.thing_icon, position);
Now in your onClick(View v),
int pos = (int)v.getTag(R.id.thing_icon);
Thing p = getItem(pos);
You can now use p as desired.
Edit 1 : Updating data in adapter
In your adapter, add the following method :
public void updateData(List<Thing> data) {
if (data != null) {
mData.clear();
mData.addAll(data);
notifyDataSetChanged();
}
}
Now just call updateData method of adapter whenever you need to update the data.

Use these inside your getView method
If you want long click
convertView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {}}
if you want normal click
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {}}

Related

Android: Changing one number picker cause other number picker to change

I have created a listview. Each item in list view has two UI elements. One is a textview and other is a number picker. Now the issue is that if i click on first number picker to change value, the fourth one also changes and vice versa. Here is my getview function
private class ViewHolder {
public TextView name;
public NumberPicker numberPicker;
public CustomListener listener;
}
public View getView(final int position, #Nullable View convertView, #NonNull final ViewGroup parent) {
ViewHolder holder;
View listItem = convertView;
currentCell=getItem(position);
currentCell.setPosition(position);
if (listItem == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
listItem = inflater.inflate(R.layout.organ_item, parent, false);
}
holder = new ViewHolder();
holder.name = (TextView) listItem.findViewById(R.id.organName);
holder.numberPicker = (NumberPicker)
listItem.findViewById(R.id.numberPicker);
holder.numberPicker.setMinValue(1);
holder.numberPicker.setMaxValue(10);
holder.numberPicker.setOnValueChangedListener(holder.listener);
holder.numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
currentCell=getItem(position);
View parentRow = (View) picker.getParent();
ListView mListView=(ListView)parentRow.getParent().getParent();
ConstraintLayout constraintLayoutView = (ConstraintLayout) mListView.getChildAt(currentCell.getPosition());
RelativeLayout relativeLayout = (RelativeLayout)constraintLayoutView.getChildAt(0);
NumberPicker p = (NumberPicker) relativeLayout.getChildAt(1);
if(position==currentCell.getPosition())
{
p.setValue(newVal);
}
else
{
p.setValue(oldVal);
}
}
});
//Set the name
TextView organName = (TextView)listItem.findViewById(R.id.organName);
organName.setText(QuickMeditationScreenInfo.getInstance().getScreenNameFromIndex(currentCell.getOrgan()));
return listItem;
}
Also even if i comment out the onValueChangeListener even then the same behaviour occurs which i assume is the default behaviour of number picker in a list. I have spent multiple hours on it but couldn't figure out the solution. I have also debugged the code and when i change a value, the debugger comes into the onValueChange code only once.
You need set numberpicker default value every time
holder = new ViewHolder();
holder.name = (TextView) listItem.findViewById(R.id.organName);
holder.numberPicker = (NumberPicker) ;
holder.numberPicker.setValue(defaultValue);//like this
Try to handle click event into adapter using interface like below
for example make one interface into adapter class ..
private onItemClick onItemClick;
public void setOnItemClick(DisplayAllData.onItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public interface onItemClick{
void onItemSelected(int position); // pass your data
}
In getView() method like number listner called all logical code into activity or fragment.
holder.mTvName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onItemClick.onItemSelected(position);
}
});
In activity or fragment after adapter set into listview or recyclerview then
adapter not null then called below code..
allDataAdapter.setOnItemClick(new DisplayAllData.onItemClick() {
#Override
public void onItemSelected(int position) {
// here called all logical part
allDataAdapter.notifyDataSetChanged();
}
});

OnCLickListener for one Item in ListView Row

I've got a ListView filled with multiple items per row. How do i set an OnCLickListener in getView() of the custon ArrayAdapter class for just one Item, not the whole row of the ListView?
#NonNull
#Override
public View getView(final int position, #Nullable final View convertView, #NonNull final ViewGroup parent) {
currentLayout = getItem(position);
myViewHolder= null;
view = convertView;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.rowlayout, parent, false);
myViewHolder= new ViewHolder(view);
view.setTag(myViewHolder);
}
myViewHolder= (ViewHolder) view.getTag();
myViewHolder.content.setText(currentLayout.getContent());
myViewHolder.number.setText("1");
return view;
}
Thank you for the answers.
To clarify first, view which you inflate is the row layout. In order to set the on click listener on a child item, simply find that item by id and then set the listener:
//It can be any type of view that supports on click listener
//Using TextView as an example
//Don't forget to assign an id to the child in xml
TextView childView = (TextView) view.findViewById(R.id.enter_child_id);
childView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do something
}
});
To have a different listener for different rows, it is better to set the listener in the onBindViewHolder not getView. getView is simply used just to inflate the view and therefore will make all your listeners on all rows non-distinguishable. In that case you can extract view or child views directly from the viewholder
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
TextView childView = holder.childView;
childView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do something
}
});
}
#NonNull
#Override
public View getView(final int position, #Nullable final View convertView, #NonNull final ViewGroup parent) {
currentLayout = getItem(position);
myViewHolder= null;
view = convertView;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.rowlayout, parent, false);
myViewHolder= new ViewHolder(view);
view.setTag(myViewHolder);
}
myViewHolder= (ViewHolder) view.getTag();
myViewHolder.content.setText(currentLayout.getContent());
myViewHolder.number.setText("1");
yourview.setonclickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
do your stuff here
}
});
return view;
}
The problem was, that I was using a LsitView instead of a RecyclerView. After fixing that issue and calling the onCLick in onBindViewHolder, everything went fine.

Search line for customArrayAdapter - Android

I've been trying to solve this one but couldn't find any answer here.
what i'm trying to do is create an EditText search for custom arrayList, using ArrayAdapter.
All is going well until I actually search for an Object (named Product in this project).
when I search for one, meaning i input chars into the EditText the list goes blank and I cannot see any Items.
This is my code:
SearchActivity , receiving the ArrayList from the mainactivity :
public class SearchActivity extends Activity {
EditText editSearch;
ProductArrayAdapter productsAdapter;
ListView products;
static ArrayList<Product> viewList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
viewList = new ArrayList<Product>(MainActivity.listToBeSent);
Toast.makeText(getBaseContext(), viewList.get(1).getName(), Toast.LENGTH_LONG).show();
productsAdapter = new ProductArrayAdapter(this,
R.layout.layout_product, viewList);
products = (ListView) findViewById(R.id.listProducts);
products.setAdapter(productsAdapter);
editSearch = (EditText)findViewById(R.id.searchLine);
editSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String charsToSearch = editSearch.getText().toString().toLowerCase(Locale.getDefault());
productsAdapter.filter(charsToSearch);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
String charsToSearch = editSearch.getText().toString().toLowerCase(Locale.getDefault());
productsAdapter.filter(charsToSearch);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ProductArrayAdapter which has the filter() method inside.
public class ProductArrayAdapter extends ArrayAdapter<Product> {
ArrayList<Product> products;
Context context;
int resource;
public ProductArrayAdapter(Context context, int resource,
ArrayList<Product> products) {
super(context, resource, products);
this.context=context;
this.resource=resource;
this.products=products;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
this.products.clear();
if (charText.length() == 0) {
this.products.addAll(SearchActivity.viewList);
} else {
for (Product singleProduct : SearchActivity.viewList) {
if (singleProduct.getName().toLowerCase(Locale.getDefault())
.contains(charText)) {
products.add(singleProduct);
}
}
}
notifyDataSetChanged();
}
static class ViewContainer {
public TextView txtName;
public TextView txtDescription;
public TextView txtPrice;
public ImageView imgProduct;
public ImageView imgOnSale;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewContainer viewContainer;
View rowView = convertView;
// means that if it is the first time and we didn't yet inflate the
// view, so inflate it now. rowView gets the already built or non-exist
// convertView.
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(resource, null);
viewContainer = new ViewContainer();
/*viewContainer.textName = (TextView) rowView
.findViewById(this.textViewResource);*/
viewContainer.txtName = (TextView) rowView
.findViewById(R.id.txtName);
viewContainer.txtDescription = (TextView) rowView
.findViewById(R.id.txtDescription);
viewContainer.txtPrice = (TextView) rowView
.findViewById(R.id.txtPrice);
viewContainer.imgProduct = (ImageView) rowView
.findViewById(R.id.productPic);
viewContainer.imgOnSale = (ImageView) rowView
.findViewById(R.id.onSaleImage);
// adding tag to each rowView , tag can be Object therefore
// viewContainer = Object.
rowView.setTag(viewContainer);
} else {
viewContainer = (ViewContainer) (rowView.getTag());
}
Toast.makeText(getContext(), products.get(1).getCategory(), Toast.LENGTH_LONG).show();
Toast.makeText(getContext(), products.get(2).getCategory(), Toast.LENGTH_LONG).show();
viewContainer.txtName.setText(products.get(position).getName());
viewContainer.txtDescription.setText(products.get(position).getDescription());
viewContainer.txtPrice.setText("Price: " + (products.get(position).getPrice().toString()));
viewContainer.imgProduct.setImageResource(products.get(position).getImage());
viewContainer.imgOnSale.setImageResource(R.drawable.logo_icon);
return rowView;
}
I think the main problem is with the viewContainer part.
Appreciate your help !
Thank you Jay, just edited my code, now it does filter some items, but it seems like it doesn't
filter by the right chars, for example , when I search the letter 'r' it does get 'r' as the charSequence but it shows only the first item , and not the correct one .
here are the edited relevant lines :
on the textChangeListener I entered :
#Override
public void afterTextChanged(Editable s) {
String charsToSearch = editSearch.getText().toString().toLowerCase();
productsAdapter.getFilter().filter(charsToSearch);
}
and on the ProductArrayAdapter I changed the getView() method to :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewContainer viewContainer;
//View rowView = convertView;
// means that if it is the first time and we didn't yet inflate the
// view, so inflate it now. rowView gets the already built or non-exist
// convertView.
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(resource, parent, false);
viewContainer = new ViewContainer();
viewContainer.txtName = (TextView) convertView
.findViewById(R.id.txtName);
viewContainer.txtDescription = (TextView) convertView
.findViewById(R.id.txtDescription);
viewContainer.txtPrice = (TextView) convertView
.findViewById(R.id.txtPrice);
viewContainer.imgProduct = (ImageView) convertView
.findViewById(R.id.productPic);
viewContainer.imgOnSale = (ImageView) convertView
.findViewById(R.id.onSaleImage);
// adding tag to each rowView , tag can be Object therefore
// viewContainer = Object.
convertView.setTag(viewContainer);
} else {
viewContainer = (ViewContainer) (convertView.getTag());
}
viewContainer.txtName.setText(products.get(position).getName());
viewContainer.txtDescription.setText(products.get(position).getDescription());
viewContainer.txtPrice.setText("Price: " + (products.get(position).getPrice().toString()));
viewContainer.imgProduct.setImageResource(products.get(position).getImage());
viewContainer.imgOnSale.setImageResource(R.drawable.logo_icon);
return convertView;
}
Any Ideas?
Remove your filter method. That is not the correct way to handle filtering with an adapter. The ArrayAdapter already supports a basic filtering mechanism. Simply do:
productsAdapter.getFilter.filter(charsToSearch);
For each product stored in the adapter, it'll do a:
singleProduct.toString().toLowerCase().startsWith(charsToSearch);
If the startsWith() logic works for you, a quick working solution would be to override the toString() method for your Product class and have it return getName(). Otherwise, there are really only two solutions available if you want a more custom solution for the filtering logic:
Write your own adapter from scratch using BaseAdapter and have it implement the Filterable interface. While a very basic implementation isn't too bad, it can be quite daunting if you never did it before.
Utilize an existing 3rd party library which allows you to add your own filtering logic.
Also note, when working with the ArrayAdapter it's very dangerous to keep your own copy of the list outside of what's constructed with the super. Especially when filtering is concerned. There's no guarantee that the list used to construct the adapter is the same one you are referencing externally. For further reading, go here.
For your getView() method, make sure you use getItem(position) instead of directly referencing products. Placing Toasts inside there is also not a good idea. When displaying your list, you will end up seeing a ton of Toasts pop up all at once. If you need debuging statements, use Log instead. Also, you need to adjust the following lines:
rowView = inflater.inflate(resource, null);
viewContainer = new ViewContainer();
To this instead:
viewContainer = inflater.inflate(resouce, parent, false);
You don't need that rowView variable at all. Just remove completely from the method and replace with viewContainer. Otherwise, your getView() method looks fine.

Android OnClickListener to update text within same ListView row

I am making a list view that contains (amongst a few other things) 2 ImageViews and a TextView. The two ImageViews are plus and minus icons, when they are pressed, they need to update the number inside the TextView. They also need to trigger code that will update the sum of the TextViews, below the ListView.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.basket_item, null);
holder.plus = (ImageView) convertView.findViewById(R.id.count_add);
holder.minus = (ImageView) convertView.findViewById(R.id.count_minus);
holder.counter = (TextView) convertView.findViewById(R.id.text_counter);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.id = position;
holder.counter.setId(position);
holder.counter.setText(count[position]);
holder.plus.setId(position);
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
if (count[id] < 999) {
count[id]++;
totalcount++;
}
}
});
holder.minus.setId(position);
holder.minus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id = v.getId();
if (count[id] > 1) {
count[id]--;
totalCount--;
}
}
});
return convertView;
}
}
class ViewHolder {
ImageView plus;
ImageView minus;
TextView counter;
int id;
}
I believe, to update the counter in the TextView, I need to somehow get my 'holder' inside the OnClickListner, however, as it is not declared as final, I can't.
Is there an easier way to do this, or am I missing a trick here?
Try implement the notifyDataSetChanged() that calls the getView once again. And after that invalidate the textView. But I am not fully sure about the need to call invalidate() here.
public void onClick(View v) {
int id = v.getId();
if (count[id] > 1) {
count[id]--;
totalCount--;
notifyDataSetChanged();
holder.counter.invalidate();
}
}
});
I am not sure about it.I hope it do. Try to paste this line under the Onclick method itself.
"holder.counter.setText('YOUR TEXT totalcount');"

Change ListView background - strange behaviour

I have a problem with changing the background of a view in a ListView.
What I need:
Change the background image of a row onClick()
What actually happens:
The background gets changed (selected) after pressing e.g. the first entry. But after scrolling down the 8th entry is selected too.
Scroll back to the top the first isn't selected anymore. The second entry is selected now.
Continue scrolling and it continues jumping...
What i'm dong in the Code:
I have channels, and onClick() I toggle an attribute of channel boolean selected
and then I change the background.
I'm doing this only onClick() thats why I don't get why it's actuelly happening on other entries too.
One thing I notices is: It seems to be only the "drawing"-part because the item which get selected "by it self" has still the selected value on false
I think it seems to have something to do with the reuse of the views in the custom ListAdapters getView(...)
Code of onClick() in ListActivity:
#Override
protected ViewHolder createHolder(View v) {
// createHolder will be called only as long, as the ListView is not
// filled
TextView title = (TextView) v
.findViewById(R.id.tv_title_channel_list_adapter);
TextView content = (TextView) v
.findViewById(R.id.tv_content_channel_list_adapter);
ImageView icon = (ImageView) v
.findViewById(R.id.icon_channel_list_adapter);
if (title == null || content == null || icon == null) {
Log.e("ERROR on findViewById",
"Couldn't find Title, Content or Icon");
}
ViewHolder mvh = new MyViewHolder(title, content, icon);
// We make the views become clickable
// so, it is not necessary to use the android:clickable attribute in
// XML
v.setOnClickListener(new ChannelListAdapter.OnClickListener(mvh) {
public void onClick(View v, ViewHolder viewHolder) {
// we toggle the enabled state and also switch the the
// background
MyViewHolder mvh = (MyViewHolder) viewHolder;
Channel ch = (Channel) mvh.data;
ch.setSelected(!ch.getSelected()); // toggle
if (ch.getSelected()) {
v.setBackgroundResource(R.drawable.row_blue_selected);
} else {
v.setBackgroundResource(R.drawable.row_blue);
}
// TESTING
Log.d("onClick() Channel", "onClick() Channel: "
+ ch.getTitle() + " selected: " + ch.getSelected());
}
});
return mvh;
}
Code of getView(...):
#Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
// When view is not null, we can reuse it directly, there is no need
// to reinflate it.
// We only inflate a new View when the view supplied by ListView is
// null.
if (view == null) {
view = mInflater.inflate(mViewId, null);
// call own implementation
holder = createHolder(view);
// TEST
// we set the holder as tag
view.setTag(holder);
} else {
// get holder back...much faster than inflate
holder = (ViewHolder) view.getTag();
}
// we must update the object's reference
holder.data = getItem(position);
// <EDIT SOLUTION>
if(getItem(position).get_id() == channelList.get(position).get_id()){
if(getItem(position).getSelected())
{
view.setBackgroundResource(R.drawable.row_blue_selected);
}
else{
view.setBackgroundResource(R.drawable.row_blue);
}
}
// </EDIT SOLUTION>
// call the own implementation
bindHolder(holder);
return view;
}
I really would appreciate any idea how to solve this! :)
If more information is needed please tell me.
Thanks in advance!
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 :)

Categories

Resources