自定义SimpleAdapter只显示示例文本的Android
在做我自己的SimpleAdapter对象,因为我想改变行的颜色,我只是用新的SimpleAdapter(...)。现在,我用我自己的自定义SimpleAdapter,该行的颜色是变化的,但我的文字是不会得到更新。我呼吁adapter.notifyDataSetChanged(),但它仍然只显示文本的样本的TextView。正如我所说,一切工作的时候我没有创造我自己的适配器罚款。我怀疑这可能是与我初始化事物的秩序:
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();
}
编辑:添加code
这是我如何从意图(onActivityResult()),置于前的addItem 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();
}
}
的*此外,为了以防万一,这是有关安置的顺序是这样的:的onCreate() - >自定义适配器类 - > onActivityResult() - >的addItem()* **
*Also, just in case this is relevant the order of placement is this: onCreate() -> custom Adapter class -> onActivityResult() -> addItem()* **
下面是什么样子的屏幕截图。在每个项目两个TextView的领域应该充满信息(其中他们,直到我这样做)。
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).
如果它的工作previously只使用新SimpleAdapter(...)
然后在您的 getView(...)
的实施改变的第一行是:
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);
,看看是否是你期待什么。取出 LayoutInflater
的东西太多。