【问题标题】:Listview and 2nd xml列表视图和第二个 xml
【发布时间】:2011-10-21 21:26:59
【问题描述】:

我有一个 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);

【问题讨论】:

  • Lars,你提问的能力越来越强了。干得好! :)
  • 嗨,Kurtis,是的,我正在尝试 :)

标签: android android-listview simpleadapter


【解决方案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 访问。

【讨论】:

  • 嗨杰克,谢谢它的工作,有什么解释为什么我必须使用结束标签 /> 而不是 ?我将不得不处理 SimpleAdapter 中的代码,因为它不能正确显示项目,edit1 只包含地图 0,00 的最后一个值,而 edit2 不包含任何内容。
  • 我认为这是因为在没有任何子项(edittext、textview 等)的项目上不需要完整的结束标记。如果您将 "110dp" /> 更改为 "110dp" >,它可能仍然有效
猜你喜欢
  • 1970-01-01
  • 2020-10-29
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多