简体   繁体   English

Android:项目文本未显示在listView中

[英]Android: Item Text not showing in listView

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. 我已经尝试了很长时间,我认为我已经有了代码,但是有些东西是错误的,因为该项目没有被添加到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). 这应该创建一个2行的listItem,但它没有,行是空的(没有文本)。 The Toast you see in the OnClickMethod returns the text just fine, but the item I add has nothing in it. 您在OnClickMethod中看到的Toast返回文本就好了,但我添加的项目中没有任何内容。 Thanks in advance. 提前致谢。

I was using the Data Binding example from the book Hello Android by Ed Burnette (great book). 我正在使用Ed Burnette的书“ Hello Android”中的数据绑定示例(好书)。

I changed the item layout from A RelativeLayout to a LinearLayout; 我将项目布局从A RelativeLayout更改为LinearLayout; however, I did not add an orientation when I made the change. 但是,当我做出改变时,我没有添加方向。

Once I added android:orientation="vertical" everything worked fine. 一旦我添加了android:orientation =“vertical”一切正常。

Two hours of my life on this one. 我生命中的两个小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM