android - ListView 和第二个 xml

标签 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);

最佳答案

您的 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 访问。

关于android - ListView 和第二个 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7855463/

相关文章:

java - onActivityResult 之后值被设置为 null

java - 从文件路径设置壁纸

java - 从 setOnItemClickListener 访问主类成员变量

android - 如何处理ListView Item LongPress手势

Android Fragment 布局超出屏幕

android - 使用简单的适配器在带有图像和文本的 ListView 中设置 OnClickListener?

android - 使用 SimpleAdapter 将背景图像应用于 ListView

android - 是否可以从 Activity 类启动 Fragment 类..使用 Intent

android - 何时使用 IntentSender 与 PendingIntent?

android - Android 中的 GPS 区域