Java 自定义SimpleAdapter仅显示示例文本
在创建我自己的SimpleAdapter对象之前,因为我想更改行的颜色,我只是使用了新的SimpleAdapter(…)。现在,我正在使用自己的自定义SimpleAdapter,行颜色正在更改,但我的文本没有得到更新。我调用了adapter.notifyDataSetChanged(),但它仍然只显示示例文本-“TextView”。正如我所说,当我没有创建自己的适配器时,一切都正常工作。我怀疑这可能与我初始化东西的顺序有关:Java 自定义SimpleAdapter仅显示示例文本,java,android,android-listview,simpleadapter,Java,Android,Android Listview,Simpleadapter,在创建我自己的SimpleAdapter对象之前,因为我想更改行的颜色,我只是使用了新的SimpleAdapter(…)。现在,我正在使用自己的自定义SimpleAdapter,行颜色正在更改,但我的文本没有得到更新。我调用了adapter.notifyDataSetChanged(),但它仍然只显示示例文本-“TextView”。正如我所说,当我没有创建自己的适配器时,一切都正常工作。我怀疑这可能与我初始化东西的顺序有关: public class AddScreen extends Acti
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();
}
*此外,为了防止与此相关,放置顺序如下:onCreate()->自定义适配器类->onActivityResult()->addItem()***
下面是它的屏幕截图。每个项目中的两个TextView字段都应该填充信息(在我这么做之前,它们都是这样) 在getView()
中,关于设置行背景的位置,还应设置TextView
的文本
调用notifyDataSetChanged()
,并不会自动正确设置文本,它只会导致ListView
用新数据重新绘制可见的行……实际上是为需要刷新的每一行调用getView()
我还建议从<代码> MyListDay.xml >代码>文件中设置背景颜色,如果GETVIEW()函数开始使用几个<代码> FIDVIEWBYID/<代码>,您也应该考虑使用一个“视图保持器”方法,如在这个示例中:
您需要在<代码> GETVIEW()/<代码>中设置文本。像这样:
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;
}
另外,这一点非常重要-将映射存储为simpledapter
即,将这一行放在对象定义的顶部:
HashMap<String, String> map = new HashMap<String, String>();
HashMap map=newhashmap();
如果它以前只使用新的SimpleAdapter(…)
工作,那么在getView(…)
实现中,将第一行更改为:
View row = super.getView(position, convertView, parent);
看看这是否是你所期待的。也拿出
LayoutInflater
的东西。您可以使用setText()将文本更改为“
以清除它,或者您可能需要的任何内容。@Phil:我明白了,但问题是我希望该文本成为变量row1和row2,这是我从意图中得到的。请参阅我的addItem()代码。如果我只是将该文本设置为row1和row2变量,就会得到一个nullPointerException,因为它们还没有被设置。为什么我在一般使用SimpleAdapter时甚至不需要初始化这些文本视图,但现在就需要这样做?如果只需要更改背景,就不需要扩展SimpleAdapter
,只需要将android:background=“#FF0000FF”
添加到mylistlayout.xml
中的布局中。如果您需要做更多的工作,那么可能需要扩展适配器
,并将其绑定到您的列表视图
我已经这样做了,但仍然遇到空指针。还有,(map.get(position))返回什么?由于每个项目都有两个文本视图,我应该改为painItems.get(position.get(“row_1”)
?事实上,如果你只看一下这个(我的整个活动),我想你会更容易帮助我
View row = super.getView(position, convertView, parent);