我正在尝试构建一个自定义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>