Listview和第二个xml

时间:2011-10-21 21:27:00

标签: android android-listview simpleadapter

我有一个list_view_row xml文件。 list_view_row.xml包含editText框,我想用它们来显示我的主程序(Android.java)列表框中的项目(由SimpleAdapter收集)。 Eclipse不会让我编译代码,因为我需要在Android.java中声明这些editText框

 int[] to = new int[] { R.id.editText1, R.id.editText1 };

我该怎么做? 提前谢谢。

list_view_row.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal" 
  android:width="100dp" 
  android:height="100dp"
>

<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>

main.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 }; <<< ??????????

        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)"); 

        for (Element tdFromSecondColumn : tdsFromSecondColumn) {
            map.put("col_1", tdFromSecondColumn.text()); 
            fillMaps.add(map);

            System.out.println("Hashmap: " + map);
            System.out.println(tdFromSecondColumn.text()); 

        } 
        for (Element tdFromSecondColumn1 : tdsFromSecondColumn1) {
            map.put("col_2", tdFromSecondColumn1.text());
            fillMaps.add(map);

            System.out.println("Hashmap: " + map);
            System.out.println(tdFromSecondColumn1.text());
        }

        SimpleAdapter adapter = new SimpleAdapter(AndroidLogin.this, fillMaps, R.layout.list_view_row, from, to); 
        kp.setAdapter(adapter);

1 个答案:

答案 0 :(得分:1)

您的XML不正确,这是正确的格式:

<EditText android:inputType="textMultiLine"  
 android:id="@+id/editText1"  
 android:layout_height="wrap_content"  
 android:text="edit1"  
 android:layout_width="110dp"/>

一旦你纠正了这个,建立你的项目,应该在代码中识别出R.id.editText1和R.id.editText2。

编辑:提示 - 转到顶部的“项目”菜单,然后转到“自动构建”。这将导致您的项目在保存文件(java / xml)时构建。修改XML并保存后,它将构建并使您的editText1资源可供R.id.editText1访问。