Search code examples
androidandroid-listviewsimpleadapter

Listview and 2nd xml


I have a list_view_row xml file. The list_view_row.xml contains editText boxes, i want to use them to display the items (collected by a SimpleAdapter) in the listbox in my main program (Android.java). Eclipse won`t let me compile the code as i need to declare these editText boxes in Android.java

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

How do i do that? Thank you in advance.

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

The code in 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);

Solution

  • Your XML is incorrect, here is the correct format:

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

    Once you correct this, build your project and the R.id.editText1 and R.id.editText2 should be recognized in code.

    EDIT: Hint - go to your Project menu at the top, and then down to "Build Automatically." This will cause your project to build upon saving a file (java / xml). After you modify the XML, and save, it will build and make your editText1 resource available to access by R.id.editText1.