Custom SimpleAdapter only shows Sample Text Android

风格不统一 提交于 2019-11-29 11:55:34

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>();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!