列表视图不显示项目
我真的很沮丧,
我试图显示我解析的 XML 文件中的一些项目,但我无法让它们显示在我的列表视图中。 谁能帮我完成这件事吗?
如果可能的话,请提供一些示例代码,以便我可以遵循,因为我对此还很陌生。
先感谢您。
我的代码:
@Override
protected void onPostExecute(String result) {
//publishProgress(false);
// create the grid item mapping
ListView kp = (ListView)findViewById(R.id.kpn);
String[] from = new String[] {"col_1", "col_2"};
int[] to = new int[] { R.id.editText1, R.id.editText1 }; // 2 EditText fields
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
Document doc = Jsoup.parse(kpn);
Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(0)");
Elements tdsFromSecondColumn1 = doc.select("table.personaltable td:eq(1)");
// Fill the HashMaps
for (Element tdFromSecondColumn : tdsFromSecondColumn) {
map.put("col_1", tdFromSecondColumn.text());
fillMaps.add(map);
System.out.println(tdFromSecondColumn.text());
}
for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {
map.put("col_2", tdFromSecondColumn1.text());
fillMaps.add(map);
System.out.println(tdFromSecondColumn1.text());
}
SimpleAdapter adapter = new SimpleAdapter(AndroidLogin.this, fillMaps, R.layout.main, from, to);
kp.setAdapter(adapter);
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:id="@+id/kpn"
android:layout_y="350dp">
</ListView>
<EditText
android:inputType="textMultiLine"
android:id="@+id/editText1"
android:layout_height="wrap_content"
android:text="edit1"
android:layout_width="110dp">
</EditText>
<EditText
android:layout_height="wrap_content"
android:layout_width="110dp"
android:id="@+id/editText2"
android:text="edit2"
android:inputType="textMultiLine"
android:layout_marginLeft="50dp"
android:layout_below="@+id/lbl_top"
android:layout_toRightOf="@+id/editText1">
</EditText>
<RelativeLayout android:id="@+id/LinearLayout01"
android:layout_below="@+id/kpn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="User Name"
android:id="@+id/lbl_username">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="@+id/lbl_password"
android:layout_marginLeft="160dp">
</TextView>
<EditText
android:id="@+id/txt_username"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_below="@+id/lbl_username">
</EditText>
<EditText
android:layout_height="wrap_content"
android:layout_width="120dp"
android:id="@+id/txt_password"
android:password="true"
android:textSize="18sp"
android:layout_marginLeft="160dp"
android:layout_below="@+id/lbl_password">
</EditText>
<Button
android:layout_height="wrap_content"
android:layout_width="100px"
android:id="@+id/btn_login"
android:text="Login"
android:layout_below="@+id/txt_username">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_width="100px"
android:id="@+id/cancel_button"
android:text="Cancel"
android:layout_marginLeft="160dp"
android:layout_below="@+id/txt_password">
</Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="Label result"
android:id="@+id/lbl_result"
android:layout_below="@+id/btn_login">
</TextView>
<TextView android:lines="6"
android:layout_height="20dp"
android:layout_width="wrap_content"
android:typeface="sans"
android:maxLines="12"
android:text="Please Loggin First"
android:id="@+id/lbl_top"
android:textSize="14sp"
android:layout_below="@+id/lbl_result">
</TextView>
</RelativeLayout>
</RelativeLayout>
I`m realy getting frustrated,
I am trying to show some items from a XML file which i parsed but i can`t get them to show in my listview.
Can anyone please help me to get this done?
Please provide some examplecode if possible, so that i can follow, as i pretty new to this.
Thank you in advance.
My code:
@Override
protected void onPostExecute(String result) {
//publishProgress(false);
// create the grid item mapping
ListView kp = (ListView)findViewById(R.id.kpn);
String[] from = new String[] {"col_1", "col_2"};
int[] to = new int[] { R.id.editText1, R.id.editText1 }; // 2 EditText fields
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
Document doc = Jsoup.parse(kpn);
Elements tdsFromSecondColumn = doc.select("table.personaltable td:eq(0)");
Elements tdsFromSecondColumn1 = doc.select("table.personaltable td:eq(1)");
// Fill the HashMaps
for (Element tdFromSecondColumn : tdsFromSecondColumn) {
map.put("col_1", tdFromSecondColumn.text());
fillMaps.add(map);
System.out.println(tdFromSecondColumn.text());
}
for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {
map.put("col_2", tdFromSecondColumn1.text());
fillMaps.add(map);
System.out.println(tdFromSecondColumn1.text());
}
SimpleAdapter adapter = new SimpleAdapter(AndroidLogin.this, fillMaps, R.layout.main, from, to);
kp.setAdapter(adapter);
My layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:id="@+id/kpn"
android:layout_y="350dp">
</ListView>
<EditText
android:inputType="textMultiLine"
android:id="@+id/editText1"
android:layout_height="wrap_content"
android:text="edit1"
android:layout_width="110dp">
</EditText>
<EditText
android:layout_height="wrap_content"
android:layout_width="110dp"
android:id="@+id/editText2"
android:text="edit2"
android:inputType="textMultiLine"
android:layout_marginLeft="50dp"
android:layout_below="@+id/lbl_top"
android:layout_toRightOf="@+id/editText1">
</EditText>
<RelativeLayout android:id="@+id/LinearLayout01"
android:layout_below="@+id/kpn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="User Name"
android:id="@+id/lbl_username">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="@+id/lbl_password"
android:layout_marginLeft="160dp">
</TextView>
<EditText
android:id="@+id/txt_username"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_below="@+id/lbl_username">
</EditText>
<EditText
android:layout_height="wrap_content"
android:layout_width="120dp"
android:id="@+id/txt_password"
android:password="true"
android:textSize="18sp"
android:layout_marginLeft="160dp"
android:layout_below="@+id/lbl_password">
</EditText>
<Button
android:layout_height="wrap_content"
android:layout_width="100px"
android:id="@+id/btn_login"
android:text="Login"
android:layout_below="@+id/txt_username">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_width="100px"
android:id="@+id/cancel_button"
android:text="Cancel"
android:layout_marginLeft="160dp"
android:layout_below="@+id/txt_password">
</Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="Label result"
android:id="@+id/lbl_result"
android:layout_below="@+id/btn_login">
</TextView>
<TextView android:lines="6"
android:layout_height="20dp"
android:layout_width="wrap_content"
android:typeface="sans"
android:maxLines="12"
android:text="Please Loggin First"
android:id="@+id/lbl_top"
android:textSize="14sp"
android:layout_below="@+id/lbl_result">
</TextView>
</RelativeLayout>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在主布局文件中包含 editText1 和 editText2。使用列表视图时,您可以在单独的文件中指定每行的布局。像这样
list_view_row.xml
然后你可以:
查看这个教程更多细节。
You can't include editText1 and editText2 in your main layout file. When using a listview, you specify the layout for each row in a seperate file. Like so
list_view_row.xml
You then do:
Checkout this tutorial for more details.
您似乎在为 fillMaps-list 设置正确的数据时遇到一些问题。对于每一行,您应该创建新的 HashMap(在您的代码中,您只创建一个)。然后将其添加到 fillMaps 中。因此,您应该有一个循环来创建 HashMap,为其设置值(“Col_1”和“Col_2”键和值)并将该映射添加到 fillMaps-list。您应该尝试将 fillMaps 中的一些值记录到 LogCat 以查看它们是否已正确添加到列表中。
It seems that you have some problems setting the correct data for the fillMaps-list. For each row you should create new HashMap (on your code you only create one). And then add that to the fillMaps. So you should have one loop where you create the HashMap, set the values for it ("Col_1" and "Col_2" keys and values) and add that map to fillMaps-list. You should try logging some values from the fillMaps to LogCat to see if they are added correctly to the list.