3
我一直在尝试很长一段时间,我认为我有代码,但有些错误,因为该项目没有被添加到列表视图。跟随我的代码:安卓:项目文本不显示在列表视图
public class EditDoctors extends Activity implements OnClickListener {
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView editDoc;
int[] to;
String[] from;
EditText addDoc, docPos;
String row1, row2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editdoc);
addDoc = (EditText) findViewById(R.id.newDoc);
docPos = (EditText)findViewById(R.id.docPos);
editDoc = (ListView) findViewById(R.id.editDoc);
TextView textview = new TextView(this);
textview.setText("This is the Songs tab");
Button adddocs = (Button)findViewById(R.id.addbutton);
adddocs.setOnClickListener(this);
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);
editDoc.setAdapter(adapter);
}
public class Adapter extends SimpleAdapter {
HashMap<String, String> map = new HashMap<String, String>();
View row;
public Adapter(Context context,
List<? extends Map<String, String>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
}
}
private void addItem() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("row_1", row1);
map.put("row_2", row2);
painItems.add(map);
adapter.notifyDataSetChanged();
}
@Override
public void onClick(View arg0) {
row1 = addDoc.getText().toString();
Toast.makeText(this, row1, Toast.LENGTH_SHORT).show();
row2 = docPos.getText().toString();
adapter.notifyDataSetChanged();
addItem();
}
}
这应该创建一个2成荫的listItem,但它并没有,该行是空(无文本)。在OnClickMethod中看到的Toast会返回文本,但是我添加的项目没有任何内容。提前致谢。
如果覆盖适配器类中的getView方法,则可以看到这些项目 – sunriser