[英]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(...). 在创建我自己的SimpleAdapter对象之前因为我想要更改行的颜色,我只是使用了新的SimpleAdapter(...)。 Now that I am using my own custom SimpleAdapter, the row color is changing, but my text is not getting updated.
现在我使用自己的自定义SimpleAdapter,行颜色正在改变,但我的文本没有更新。 I have called adapter.notifyDataSetChanged(), but it is still showing only the sample text- "TextView".
我调用了adapter.notifyDataSetChanged(),但它仍然只显示示例文本 - “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: 这就是我从addItem代码之前的intent(onActivityResult())获取数据的方法:
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()* ** *另外,如果这是相关的,则放置顺序为: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).
每个项目中的两个TextView字段应填充信息(他们是这样做的,直到我这样做)。
If it worked previously with just using new SimpleAdapter(...)
then in your getView(...)
implementation change the first line to this: 如果之前只使用
new SimpleAdapter(...)
那么在你的getView(...)
实现中将第一行更改为:
View row = super.getView(position, convertView, parent);
And see if that is what you're expecting. 看看这是否是你所期待的。 Take out the
LayoutInflater
stuff too. 取出
LayoutInflater
东西。
In getView()
, about where you are setting the row background, you should also set the text for the TextView
. 在
getView()
,关于设置行背景的位置,还应该设置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. 调用
notifyDataSetChanged()
,不会自动设置正确的文本,它只会导致ListView
使用新数据重绘可见行...实际上为需要刷新的每一行调用getView()
。
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 我还建议从
mylistlayout.xml
文件设置背景颜色,如果getView()函数开始使用一些findViewByID
,你还应该考虑使用像这样的示例中的“视图持有者”方法: http:// developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
You need to set the text in getView()
. 您需要在
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
此外,这非常重要 - 将您的地图存储为
SimpleAdapter
的成员变量
ie, put this line at the top of your object definition: 即,将此行放在对象定义的顶部:
HashMap<String, String> map = new HashMap<String, String>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.