Android Listview不显示项目
我真的很沮丧 我试图显示我解析过的XML文件中的一些项目,但我无法让它们显示在我的listview中。 谁能帮我完成这件事 如果可能的话,请提供一些示例代码,这样我就可以理解了,因为我对这个很陌生 先谢谢你 我的代码:Android Listview不显示项目,android,android-layout,android-listview,simpleadapter,Android,Android Layout,Android Listview,Simpleadapter,我真的很沮丧 我试图显示我解析过的XML文件中的一些项目,但我无法让它们显示在我的listview中。 谁能帮我完成这件事 如果可能的话,请提供一些示例代码,这样我就可以理解了,因为我对这个很陌生 先谢谢你 我的代码: @Override protected void onPostExecute(String result) { //publishProgress(false); // create the grid item mapping
@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);
@覆盖
受保护的void onPostExecute(字符串结果){
//出版进度(假);
//创建网格项映射
ListView kp=(ListView)findViewById(R.id.kpn);
字符串[]from=新字符串[]{“col_1”、“col_2”};
int[]to=new int[]{R.id.editText1,R.id.editText1};//2个EditText字段
List fillMaps=new ArrayList();
HashMap=newHashMap();
Document doc=Jsoup.parse(kpn);
元素tdsFromSecondColumn=doc.select(“table.personaltable td:eq(0)”;
元素tdsFromSecondColumn1=doc.select(“table.personaltable td:eq(1)”;
//填充哈希映射
for(元素tdFromSecondColumn:tdsFromSecondColumn){
put(“col_1”,tdFromSecondColumn.text());
fillMaps.add(map);
System.out.println(tdFromSecondColumn.text());
}
for(元素tdFromSecondColumn1:tdsFromSecondColumn1){
put(“col_2”,tdFromSecondColumn1.text());
fillMaps.add(map);
System.out.println(tdFromSecondColumn1.text());
}
simpledapter adapter=newsimpledapter(AndroidLogin.this、fillMaps、R.layout.main、from、to);
kp.setAdapter(适配器);
我的布局:
<?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>
似乎您在为fillMaps列表设置正确的数据时遇到了一些问题。对于每一行,您应该创建新的HashMap(在您的代码中,您只创建一个)。然后将其添加到fillMaps。因此,您应该在创建HashMap的地方有一个循环,为其设置值(“Col_1”和“Col_2”键和值)并将该映射添加到fillMaps列表。您应该尝试将fillMaps中的一些值记录到LogCat,以查看它们是否正确添加到列表中。似乎在为fillMaps列表设置正确的数据时遇到了一些问题。对于每一行,您都应该创建新的HashMap(在代码中,您只创建一个)。然后将其添加到fillMaps。因此,您应该有一个循环,在其中创建HashMap,为其设置值(“Col_1”和“Col_2”键和值)并将该映射添加到fillMaps列表中。您应该尝试将fillMaps中的一些值记录到LogCat,以查看它们是否正确添加到列表中。您不能在主布局文件中包含editText1和editText2。使用listview时,您可以在单独的文件中为每一行指定布局。类似这样
list_view_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="horizontal"
android:width="fillParent"
android:height="fillParent"
/>
<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"
</EditText>
</LinearLayout>
有关详细信息,请签出此项。您不能在主布局文件中包含editText1和editText2。使用listview时,您可以在单独的文件中为每一行指定布局。如下所示 list_view_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="horizontal"
android:width="fillParent"
android:height="fillParent"
/>
<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"
</EditText>
</LinearLayout>
有关更多详细信息,请查看此项。快速浏览时,它看起来没问题。除了您的“to”变量指向同一编辑文本外,快速浏览时,它看起来没问题。除了您的“to”变量指向同一个EditTextHi Kurtis,感谢您为我指出了正确的方向,但我使用list_view_row.xml得到了一个未绑定前缀解析错误。好的,我知道了,但我如何在main.xml中指向list_view_row中的2个edittext字段?main不再识别edit1和2:int[]to=new int[]{R.id.editText1,R.id.editText1};您不必这样做。在main.xml中,main.xmlHmm中不应该有引用列表_view_row.xml或任何内容,但我无法编译为edittext字段:int[]to=new int[]{R.id.editText1,R.id.editText1};无法解决。请帮助。Lars,如果您有新问题,请打开一个新问题。别忘了对有帮助的回答进行投票;)嗨,Kurtis,谢谢您为我指出了正确的方向,但我在使用list_view_row.xml时遇到了一个未绑定前缀解析错误。好的,我知道了,但是如何将main.xml指向list_view_row?Main中的2个edittext字段不再识别edit1和2:int[]to=new int[]{R.id.editText1,R.id.editText1};您不必这样做。在Main.xml中,Main.xmlHmm中应该没有引用list_view_row.xml或其中的任何内容,但我无法编译为edittext字段:int[]to=new int[]{R.id.editText1,R.id.editText1};无法解决。请帮助。拉尔斯,如果你有新问题,请提出新问题。别忘了对有帮助的回答进行投票;)嗨,莉莎,你能看一下这个吗:提前谢谢。嗨,莉莎,你能看一下这个吗:提前谢谢。