[英]Custom SimpleAdapter only shows Sample Text Android
在創建我自己的SimpleAdapter對象之前因為我想要更改行的顏色,我只是使用了新的SimpleAdapter(...)。 現在我使用自己的自定義SimpleAdapter,行顏色正在改變,但我的文本沒有更新。 我調用了adapter.notifyDataSetChanged(),但它仍然只顯示示例文本 - “TextView”。 正如我所說,當我沒有創建自己的適配器時,一切正常。 我懷疑它可能與我正在初始化的命令有關:
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();
}
編輯:添加代碼
這就是我從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();
}
}
*另外,如果這是相關的,則放置順序為:onCreate() - > custom Adapter class - > onActivityResult() - > addItem()* **
這是它的外觀截圖。 每個項目中的兩個TextView字段應填充信息(他們是這樣做的,直到我這樣做)。
如果之前只使用new SimpleAdapter(...)
那么在你的getView(...)
實現中將第一行更改為:
View row = super.getView(position, convertView, parent);
看看這是否是你所期待的。 取出LayoutInflater
東西。
在getView()
,關於設置行背景的位置,還應該設置TextView
的文本。
調用notifyDataSetChanged()
,不會自動設置正確的文本,它只會導致ListView
使用新數據重繪可見行...實際上為需要刷新的每一行調用getView()
。
我還建議從mylistlayout.xml
文件設置背景顏色,如果getView()函數開始使用一些findViewByID
,你還應該考慮使用像這樣的示例中的“視圖持有者”方法: http:// developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
您需要在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;
}
此外,這非常重要 - 將您的地圖存儲為SimpleAdapter
的成員變量
即,將此行放在對象定義的頂部:
HashMap<String, String> map = new HashMap<String, String>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.