简体   繁体   中英

Custom SimpleAdapter only shows Sample Text Android

Before making my own SimpleAdapter object because I wanted to change the color of the rows, I was just using new SimpleAdapter(...). Now that I am using my own custom SimpleAdapter, the row color is changing, but my text is not getting updated. I have called adapter.notifyDataSetChanged(), but it is still showing only the sample text- "TextView". As I said, everything was working fine when I didn't create my own adapter. I suspect it might have something to do with the order I am initializing things:

public class AddScreen extends Activity implements OnClickListener,
    OnItemClickListener, OnItemLongClickListener {
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
    String[] from;

public void onCreate(Bundle savedInstanceState) {
...
listthings = (ListView) findViewById(R.id.listthings);
    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);
...
}

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;
        if (row == null) {
            LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = mInflater.inflate(R.layout.mylistlayout, parent, false);
            }
        row.setBackgroundColor(0xFF0000FF);
       TextView rw1 = (TextView)findViewById(R.id.row1);
      // TextView rw2 = (TextView)findViewById(R.id.row2);
       rw1.setText(map.get(position));
       return row;
    }

}
// 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();




}

EDIT:Added Code

This is how I am getting the data from the intent(onActivityResult()), placed before the addItem Code:

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();
    }
}

*Also, just in case this is relevant the order of placement is this: onCreate() -> custom Adapter class -> onActivityResult() -> addItem()* **

Here is a screenshot of what it looks like. The two TextView fields in each item should be filled with info(which they were, until I did this).

If it worked previously with just using new SimpleAdapter(...) then in your getView(...) implementation change the first line to this:

View row = super.getView(position, convertView, parent);

And see if that is what you're expecting. Take out the LayoutInflater stuff too.

In getView() , about where you are setting the row background, you should also set the text for the TextView .

Calling notifyDataSetChanged() , doesn't automagically set your texts right, it just causes the ListView to redraw the visible rows with the new data...practically calling getView() for each row that needs a refresh.

I also suggest setting the background color from the mylistlayout.xml file, and if the getView() function starts taking on a few findViewByID 's, you should also consider using a "view holder" approach like in this sample: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

You need to set the text in getView() . Like this:

public View getView(int position, View convertView, ViewGroup parent){

    TextView text;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.mylistlayout, parent, false);
        text = (TextView) convertView.findViewById(R.id.more_list_text);
    }
    convertView.setBackgroundColor(0xFF0000FF);
    text.setText(map.get(position));
    return convertView;
}

Also, and this is VERY important - store you map as a member variable of the SimpleAdapter
ie, put this line at the top of your object definition:

HashMap<String, String> map = new HashMap<String, String>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM