chitranna
chitranna

Reputation: 1639

Cannot see any content in a ListView while using a HashMap

I am trying to build a custom Listview, with an image to the right , main text and sub text. The main problem is I don't get any errors and a blank view shows up. I can click on it, but I don't see any content on my ListView. My code:

    public class MenuScreen extends Activity  {            
            TextView maintext, subtext;
            ImageView icon ;

                    private static final String [] menuitems = {"Availability","Messages","Greetings","Address Book","Calls","Settings"};
                    private static final int [] menu_icons = {R.drawable.menu_availability,R.drawable.menu_messages,R.drawable.menu_greetings,R.drawable.menu_contacts,R.drawable.menu_calls,R.drawable.menu_settings}; 
                    private static String [] submenu_items = {"1","2","3","4","5","6"};
                    @Override
                        protected void onCreate(Bundle savedInstanceState) {
                            // TODO Auto-generated method stub
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.menulogin_main);
                            maintext = (TextView)findViewById(R.id.tvMainText);
                            subtext = (TextView)findViewById(R.id.tvSubText);
                            icon = (ImageView)findViewById(R.id.iicon);

                            List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
                             for(int i=0;i<6;i++){
                                    HashMap<String, String> hm = new HashMap<String,String>();
                                    hm.put("tvMainText", "MainText : " + menuitems[i]);
                                    hm.put("tvSubText","SubText : " + submenu_items[i]);
                                    hm.put("iicon", Integer.toString(menu_icons[i]) );
                                    aList.add(hm);
                                }

                             String[] from = { "iicon","tvMainText","tvSubText" };

                             int[] to = { R.id.iicon , R.id.tvMainText, R.id.tvSubText} ;

                             SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.menulogin_main, from, to);

                               // Getting a reference to listview of main.xml layout file
                               ListView listView = ( ListView ) findViewById(R.id.listmenu);

                               // Setting the adapter to the listView
                               listView.setAdapter(adapter);
                            listView.setOnItemClickListener(new OnItemClickListener() {

                                public void onItemClick(AdapterView<?> listview, View view,
                                        int position, long id) {
                                    switch(position){

                                    case 0 :
                                        Intent avail = new Intent(MenuScreen.this,Availability.class);
                                        startActivity(avail);

                                    break;
                                    }

                                }
                            });

                    }

                }

Layout file for the ListView, menulogin_main:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         android:background="@drawable/background"
        android:orientation="vertical" >


        <ListView
            android:id="@+id/listmenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

        </ListView>

    </LinearLayout>

Layout file for the row, menulogin:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/iicon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:src="@drawable/logon_image" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/tvMainText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:layout_marginTop="5dp"
                    android:text="MAIN TEXT"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <TextView
                    android:id="@+id/tvSubText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="SUB TEXT"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>

Upvotes: 0

Views: 208

Answers (1)

user
user

Reputation: 87064

That is probably happening because of the layout file you use. You have the same layout file for the activity layout(R.layout.menulogin_main), but also you use the same layout file for the ListViews adapter. Now, you either have the ListView(with the id R.id.listmenu) and the row views(R.id.iicon , R.id.tvMainText, R.id.tvSubText) in the same layout(R.layout.menulogin_main) and the ListView covers those views or you intended to use another layout for the ListView row and not R.layout.menulogin_main(but this will probably throw an exception).

You should have a special layout file designed for the ListView's row which contains only the desired views.

Edit: Layout file for menu_login:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/iicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tvSubText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/iicon"
        android:text="SUB TEXT"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/tvMainText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/tvSubText"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/iicon"
        android:text="MAIN TEXT"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Upvotes: 1

Related Questions