Question

I`m realy getting frustrated,

I am trying to show some items from a XML file which i parsed but i can`t get them to show in my listview. Can anyone please help me to get this done?

Please provide some examplecode if possible, so that i can follow, as i pretty new to this.

Thank you in advance.

My code:

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

My layout:

<?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> 
Was it helpful?

Solution

You can't include editText1 and editText2 in your main layout file. When using a listview, you specify the layout for each row in a seperate file. Like so

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>

You then do:

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

Checkout this tutorial for more details.

OTHER TIPS

It seems that you have some problems setting the correct data for the fillMaps-list. For each row you should create new HashMap (on your code you only create one). And then add that to the fillMaps. So you should have one loop where you create the HashMap, set the values for it ("Col_1" and "Col_2" keys and values) and add that map to fillMaps-list. You should try logging some values from the fillMaps to LogCat to see if they are added correctly to the list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top