processing in ArrayAdapter - android

i am trying to show the contents using a list activity in android.
I am getting a list from backend. now i want to fetch each list value and see whether that item is shown in red color or in green color. this value will also be set in customVO.
I tried refering to few article on internet like this. but here they are using List only but i need VO so that i can fetch the value to decide whether that menu item should be red or green.
P.S i am beginner in android so excuse me if my question seems to be bit stupid.
thanks in advance :)

Just override the getView method to set the background:
final ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(
getActivity(),
R.layout.list_item,
myArray) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
/* Set values of TextViews here */
MyClass currentItem = getItem(position);
if (currentItem.getColor() == MyVoClass.GREEN) {
convertView.setBackgroundColor(0x0000FF00);
} else if (currentItem.getColor() == MyVoClass.RED) {
convertView.setBackgroundColor(0x00FF0000);
} else {
convertView.setBackgroundColor(0x00FFFFFF);
}
return convertView;
}
};

Related

Update ListView background color when bluetooth get a new read

I'm trying to update each element every time an read comes from the Bluetooth, I found this
deviceListAdapter = new ArrayAdapter<String>(view.getContext(),
android.R.layout.simple_list_item_1, mylist) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
if(getItem(position).startsWith("something"))
{
// do something change color
row.setBackgroundColor (Color.GREEN); // some color
}
else
{
// default state
row.setBackgroundColor (Color.RED); // default coloe
}
return row;
}
};
It does updates the color, but here is the thing I have a list like this one
The list is loaded through a call to a server with Volley.
The things is that when the Bluetooth receive data, check if he id exists in the ListView and update the color to green.
I this even possible? Any thoughts on this can I do this?
All you need to do is update the item in myList and then call deviceListAdapter.notifyDataSetChanged() to refresh the list views.
So I found what It was missing in the conditional to look if the id has being read.
I had to add testArray.contains() with that part it works as expected.
if(testArray.contains(getItem(position)))
{
row.setBackgroundColor (Color.GREEN); // some color
}
else
{
row.setBackgroundColor (Color.RED); // default coloe
}
return row;

Android - Keeping clicked on items highlighted in ListView (and in memory)

I have an app whereby i am accessing a RESt webservice and presenting the request in listview (ListFrragment). It is essentially a list of articles. What i am trying to achieve is when a user clicks on an item in listview (expands article in ViewPager) is stays highlighted (indicating that the article has been read). The next time the user reloads this listview, all the item that have been read is still highlighted.
I have have created a boolean variable "read" for each article. When a user clicks on this item, the "read" variable is assigned to TRUE. Then in the custom Adapter(extends arrayAdpater), i assign the background colour (in this case green) in getView for articles whose "read" value is TRUE. This does work, but what i am finding is that other items in the listview is also randomly turning green when i start to scroll up and down the listview without actually clicking on it. I'm really stuck as i do not understand what is happening. Please can someone advise?
public void onListItemClick(ListView l, View v, int postion, long id) {
Article a = ((toReadListAdapter) getListAdapter()).getItem(postion);
// set read attribute to TRUE
a.setRead(true);
saveToReadList(toReadList);
// Open via ViewPager
Intent i = new Intent(getActivity(), ReadingList_PagerActivity.class);
startActivity(i);
Customer Adapter:
// Defining custom adapter
private class toReadListAdapter extends ArrayAdapter<Article> {
public toReadListAdapter(ArrayList<Article> listToRead) {
super(getActivity(), 0, listToRead);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.new_articlelistfragment, null);
}
Article en = getItem(position);
Log.d(TAG, "Showing articles: " + position + "/ pmid: " + en.getId());
Log.e(TAG, "isRead value: " + en.isRead());
if(en.isRead() == true){
convertView.setBackgroundColor(Color.parseColor("#C8E6C9"));
}
......
return convertView;
Inside the getView
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.new_articlelistfragment, null);
}
Article en = getItem(position);
if(en.isRead() == true){
convertView.setBackgroundColor(Color.parseColor("#C8E6C9"));
}
else{
// put the default color
convertView.setBackgroundColor( );
}
......
return convertView;
}
You are not applying the default color. Add an else statement in get view method

Android ListView - change background of element based on values

I have a list which lists EXPENSES and INCOMES. I want ListViewer to automatically change background to either green for Incomes or red for Expenses, would this be possible?
final DbCon database = new DbCon(this);
final ListView Viewer = (ListView)findViewById(R.id.historyViewer);
//date1, time1, desc1, val1, cat1, type1
final SimpleAdapter La = new SimpleAdapter(this, database.GetMonths(), R.layout.mviewer, new String[] {"month"}, new int[] {R.id.month_name});
Viewer.setAdapter(La);
Without seeing any code, its hard to give any "real" help but it is possible. You can change the background color with something like
rowView.setBackgroundColor(Color.GREEN);
assuming rowView is your current View in the List. You can also change styles and themes depending on what you want.
Yes just put the logic in the getView method of your Adapter, e.g.:
public View getView(int position, View convertView, ViewGroup parent) {
View myView;
if(convertView == null) {
myView = new MyView();
} else {
myView = (MyView) convertView;
}
if(myList.get(position).isExpense) {
myView.setBackgroundColor(Color.RED);
} else {
myView.setBackgroundColor(Color.GREEN);
}
return myView;
}
edit:
Yes, I see that you are using a SimpleAdapter. That will not be sufficient to do what you want. You need to create your own adapter by subclassing BaseAdapter (or something similar) and overriding the necessary methods, e.g. getView as I showed above.

My getView() Position is repeating In Custom List View?

I am using custom ListView .Its working fine. I am using this ListView for differentiate Read & Unread Messages I pick up message read Id which is 0 for unread message & 1 for read message.
My getView() code is following:---
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null)
{
convertView=mInflater.inflate(R.layout.custom_home_list, null);
holder=new ViewHolder();
holder.address=(TextView)convertView.findViewById(R.id.person_name);
holder.body=(TextView)convertView.findViewById(R.id.full_msg);
holder.date=(TextView)convertView.findViewById(R.id.msg_time);
convertView.setTag(holder);
}else
{
holder=(ViewHolder)convertView.getTag();
}
int size=mArrList.size();
if ((mArrList != null) || size > 0)
{
if(mArrList.get(position).read.equalsIgnoreCase("1")){
holder.address.setText(mArrList.get(position).address);
holder.body.setText(mArrList.get(position).body);
holder.date.setText(mArrList.get(position).date);
}else{
holder.address.setText(mArrList.get(position).address);
holder.body.setText(mArrList.get(position).body);
holder.body.setTextColor(mArrList.get(position).color);
holder.date.setText(mArrList.get(position).date);
}
}
return convertView;
}
here i use this condition for differentiate :-
if(mArrList.get(position).read.equalsIgnoreCase("1")){
}
But Position Repeat After 5 items so my condition not working.
I have lots of search for this But I am not getting it.
Please Help me .
Thanks In Advance!
Regards Deepanker
In your code you don't explicitely set the color in case of a 'read' state (the first section of your if/else).
It means that if your view is recycled and it was previously used for an 'unread' item, then the color will remain the same.
You need to explicitely say which color you want in both cases, because you cannot know what the initial color is.

Row color based on contents in the ListView of my RSS reader

I am very much an Android newbie and I have built a simple RSS reader application based around the free IBM android RSS tutorial. I would like to change the background color of each row if the category of that row is equal to a particular String.
I wrote the following "for loop" which discovers the category of each item and runs an if statement should that category be equal to "News". At the moment the background colour of the entire listview gets changed as soon as a feed is supplied with a News category.
Does anyone out there feel like helping out a beginner?
for(int i = 0; i < feed.getItemCount(); i++)
{
if (feed.getItem(i).getCategory().equals("News"))
{
ListView.setBackgroundColor(0x77ee0044);
}
}
You're going to need to do this inside the getView() method of your adapter.
Before you return the view in getView(), you can call setBackgroundColor() on the view or use one of the other setBackgroundFoo() methods.
Edit - given your code, when you create your Adapter, try:
ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(
this,android.R.layout.simple_list_item_1,feed.getAllItems(­)) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (getItem(position).getCategory().equals("News")) {
view.setBackgroundColor(0xffff0000); // red
}
return view;
}
};
This overrides getView() to adjust the background of your list item views properly.

Categories

Resources