0

我正在尝试构建一个自定义Listview,右侧有图像,主文本和子文本。主要问题是我没有收到任何错误,并且出现了空白视图。我可以点击它,但我的ListView. 我的代码:

    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;
                                    }

                                }
                            });

                    }

                }

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>

行的布局文件,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>
4

1 回答 1

1

这可能是由于您使用的布局文件而发生的。Activity 的 layout( ) 有相同的布局文件,但s 适配器R.layout.menulogin_main也使用相同的布局文件。ListView现在,您要么在同一布局( )中拥有ListView(带有 id R.id.listmenu)和行视图(R.id.iicon, R.id.tvMainText, ),并且 ListView 覆盖了这些视图,要么您打算对行使用另一个布局而不是(但这可能会抛出一个例外)。R.id.tvSubTextR.layout.menulogin_mainListViewR.layout.menulogin_main

您应该有一个为ListView' 行设计的特殊布局文件,其中包含所需的视图。

编辑: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>
于 2012-10-12T12:35:33.420 回答