Android:项目文本未显示在列表视图中
我已经尝试了很长时间,并且我认为我已经编写了代码,但是出了点问题,因为该项目没有添加到 listView 中。遵循我的代码:
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 很好地返回了文本,但我添加的项目中没有任何内容。提前致谢。
I've been trying for a long time, and I think I have the code in place, but something is wrong because the item is not being added to the listView. Follows my code:
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();
}
}
This should create a 2-lined listItem, but it doesn't, the lines are empty(no text). The Toast you see in the OnClickMethod returns the text just fine, but the item I add has nothing in it. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用的是 Ed Burnette 的《Hello Android》一书(很棒的书)中的数据绑定示例。
我将项目布局从相对布局更改为线性布局;但是,我在进行更改时没有添加方向。
一旦我添加了 android:orientation="vertical" 一切工作正常。
我生命中的两个小时都花在了这件事上。
I was using the Data Binding example from the book Hello Android by Ed Burnette (great book).
I changed the item layout from A RelativeLayout to a LinearLayout; however, I did not add an orientation when I made the change.
Once I added android:orientation="vertical" everything worked fine.
Two hours of my life on this one.