Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6208091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:44:52+00:00 2026-05-24T05:44:52+00:00

I am using an array list, a listView, and a custom simple adapter in

  • 0

I am using an array list, a listView, and a custom simple adapter in order to create my list in Android. In the following code, I use addItem() to add an Item. I need to add the item onActivityResult, using some of the extras I get in the intent. This all works fine, but the problem comes when I am trying to set the background color based on those extras that I am getting. I think it has something to do with the adapter not being refreshed, etc. My code will clear up what I am trying to say.

SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
String[] from;
**OnCreate()**{
from = new String[] { "row_1", "row_2" };
    to = new int[] { R.id.row1, R.id.row2 };

    adapter = new Adapter(this, painItems, R.layout.mylistlayout, from, to);
            listthings.setAdapter(adapter);

}

Custom Adapter code:

public class Adapter extends SimpleAdapter {
    HashMap<String, String> map = new HashMap<String, String>();

    public Adapter(Context context,
            List<? extends Map<String, String>> data, int resource,
            String[] from, int[] to) {
        super(context, data, resource, from, to);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        row = super.getView(position, convertView, parent);
        if (row == null) {
            LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = mInflater.inflate(R.layout.mylistlayout, parent, false);
        }

        if (Integer.valueOf(painItems.get(position).get("row_3")) != null) {
            if (Integer.valueOf(painItems.get(position).get("row_3")) == 2) {
                row.setBackgroundColor(0xFF0000FF);//**BACKGROUND COLOR SET HERE**
                //Toast.makeText(getApplicationContext(), "this one", Toast.LENGTH_SHORT).show();
            }
        }
        return row;

    }

}

How I am retrieving the necessary extras and adding the item:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == 1) {
        row1 = data.getStringExtra("com.painLogger.row1");
        row2 = data.getStringExtra("com.painLogger.row2");
        painLevelString = data.getStringExtra("com.painLogger.painLevel");
        painLocation = data.getStringExtra("painLocation");
        timeOfPainString = data.getStringExtra("com.painLogger.painTime");
        textTreatmentString = data
                .getStringExtra("com.painLogger.treatment");
        addItem();

    }
}

// to add the item, put it in the map, and add the map into the list
private void addItem() {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("row_1", row1);
    map.put("row_2", row2);
    map.put("row_3", painLevelString);
    map.put("row_4", painLocation);
    map.put("row_5", timeOfPainString);
    map.put("row_6", textTreatmentString);
    painItems.add(map);
    adapter.notifyDataSetChanged();

}

Again, when I add the item to my list, I want to see what the ‘painlevelString’ is. If it is a certain value, then I would like to set my background to a color. All the adding, etc is working, but it just ends up setting the background color to blue of whatever I add, regardless of the data I pass through the intent for that certain item.

Although I believe I have added all the relevant code, my full activity can be found here: http://pastebin.com/4BWQYpnj.

Thank You in advance for taking your time to solve my problem

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:44:53+00:00Added an answer on May 24, 2026 at 5:44 am

    You should set color both if your condition is true (one color) or not (another color).

    But actually I can’t get where is painItems declaration in Adapter class. It seems that you meshed up two classes code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code to populate listview Oncreate method: SimpleAdapter adapter = new
friends, i am using following custom adapter to show textview and checboxes in listview.
I use a custom list adapter and ArrayList for my ListView. This solution was
I'm using a custom listview I found here: http://developer.android.com/resources/samples/ApiDemos/res/layout/linear_layout_9.html Seems to be valid in
I'm trying to sort the list items, I'm using custom listview. In this i
I have a listview with custom array adapter, the view allows the user to
I am trying to create a custom ListView using an ArrayAdapter . POJO Item
I tried using reflection to use a custom List View for an App with
I have list of items with check boxes using this code: ProcessList = (ListView)
I have show Array in listview. with black text color. if i using android.R.color.black

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.