我正在关注该教程:
http://hmkcode.com/android-custom-listview-titles-icons-counter/
我有以下代码:
../布局/ target_item.xml
<?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="48dp"
android:background="#F3F3F3">
<!-- icon -->
<ImageView
android:id="@+id/item_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/ic_action_help"
/>
<!-- title -->
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/item_icon"
android:layout_alignBaseline="@+id/item_counter"
android:textSize="18dp" />
<!-- counter -->
<TextView
android:id="@+id/item_counter"
android:layout_width="50dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/rectangle"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
* .. /布局/ group_header_item.xml
<?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="30dp"
android:background="#5490CC">
<!-- title -->
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="12dp"
android:gravity="center_vertical"
android:textColor="@color/blanco"
android:textSize="16dp"
android:textStyle="bold" />
<!-- divider -->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="1dp"
android:layout_alignParentBottom="true"
android:background="#DADADC" ></View>
</RelativeLayout>
/ SCR /.../ Model.java
public class Model{
private int icon;
private String title;
private String counter;
private boolean isGroupHeader = false;
public Model(String title) {
this(-1,title,null);
isGroupHeader = true;
}
public Model(int icon, String title, String counter) {
super();
this.icon = icon;
this.title = title;
this.counter = counter;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCounter() {
return counter;
}
public void setCounter(String counter) {
this.counter = counter;
}
public boolean isGroupHeader() {
return isGroupHeader;
}
public void setGroupHeader(boolean isGroupHeader) {
this.isGroupHeader = isGroupHeader;
}
}
的src /.../ MyAdapter.java
public class MyAdapter extends ArrayAdapter<Model> {
private final Context context;
private final ArrayList<Model> modelsArrayList;
public MyAdapter(Context context, ArrayList<Model> modelsArrayList) {
super(context, R.layout.target_item, modelsArrayList);
this.context = context;
this.modelsArrayList = modelsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = null;
if(!modelsArrayList.get(position).isGroupHeader()){
rowView = inflater.inflate(R.layout.target_item, parent, false);
// 3. Get icon,title & counter views from the rowView
ImageView imgView = (ImageView) rowView.findViewById(R.id.item_icon);
TextView titleView = (TextView) rowView.findViewById(R.id.item_title);
TextView counterView = (TextView) rowView.findViewById(R.id.item_counter);
// 4. Set the text for textView
imgView.setImageResource(modelsArrayList.get(position).getIcon());
titleView.setText(modelsArrayList.get(position).getTitle());
counterView.setText(modelsArrayList.get(position).getCounter());
}`enter code here`
else{
rowView = inflater.inflate(R.layout.group_header_item, parent, false);
TextView titleView = (TextView) rowView.findViewById(R.id.header);
titleView.setText(modelsArrayList.get(position).getTitle());
}
// 5. retrn rowView
return rowView;
}
}
的src /.../ MainActivity.java
public class MainActivity extends ListActivity {
public void onCreate(Bundle b) {
super.onCreate(b);
MyAdapter adapter = new MyAdapter(this, generateData());
setListAdapter(adapter);
}
private ArrayList<Model> generateData(){
ArrayList<Model> models = new ArrayList<Model>();
models.add(new Model("group1"));
models.add(new Model(R.drawable.ic_action_help,"item 1","1"));
models.add(new Model(R.drawable.ic_action_search,"item2","25"));
return models;
}
}
我的问题
好的,我想添加一个ListView,因为我需要在项目上使用clic事件。
在教程上,我可以在MainActicity上阅读:
//if extending Activity
//setContentView(R.layout.activity_main);
//if extending Activity 2. Get ListView from activity_main.xml
//ListView listView = (ListView) findViewById(R.id.listview);
我尝试过但不行,我不知道怎么做。
更新
public class MainActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list);
//.......
我在listView中添加了名为R.id.list
的activity_main.xmlactivity_main.xml中
<?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" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
错误
06-05 09:40:43.819: E/AndroidRuntime(1217): FATAL EXCEPTION: main
06-05 09:40:43.819: E/AndroidRuntime(1217): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listprueba/com.example.listprueba.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.os.Looper.loop(Looper.java:137)
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-05 09:40:43.819: E/AndroidRuntime(1217): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 09:40:43.819: E/AndroidRuntime(1217): at java.lang.reflect.Method.invoke(Method.java:525)
06-05 09:40:43.819: E/AndroidRuntime(1217): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-05 09:40:43.819: E/AndroidRuntime(1217): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-05 09:40:43.819: E/AndroidRuntime(1217): at dalvik.system.NativeStart.main(Native Method)
06-05 09:40:43.819: E/AndroidRuntime(1217): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
06-05 09:40:43.819: E/AndroidRuntime(1217): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.app.Activity.setContentView(Activity.java:1895)
06-05 09:40:43.819: E/AndroidRuntime(1217): at com.example.listprueba.MainActivity.onCreate(MainActivity.java:15)
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.app.Activity.performCreate(Activity.java:5133)
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-05 09:40:43.819: E/AndroidRuntime(1217): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
06-05 09:40:43.819: E/AndroidRuntime(1217): ... 11 more
更新2
MainActivity.java
public class MainActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// extending Activity
setContentView(R.layout.activity_main);
// 1. pass context and data to the custom adapter
MyAdapter adapter = new MyAdapter(this, generateData());
// Get ListView from activity_main.xml
ListView list = (ListView) findViewById(android.R.id.list);
// 3. setListAdapter
list.setAdapter(adapter); //extending Activity
//setListAdapter(adapter);
}
//......
activity_main.xml中
<?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" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
我如何实现ListView?
解
在 MainActivity.java 上我添加了 ListView list = getListView(); 而不是ListView list =(ListView)findViewById(android.R.id.list);
并在 activity_main.xml 上添加了
//.....
android:id="@android:id/list"
//....
由于
答案 0 :(得分:1)
您需要ANDROID.R.id.list,其操作如下:
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
</ListView>
答案 1 :(得分:0)
您的logcat说Your content must have a ListView whose id attribute is 'android.R.id.list'
在您的R.layout.activity_main
中,您需要ListView
代码为android.R.id.list
,因为您的活动是从ListActivity
延伸的。它期望ListView
没有找到。所以,在MainActivity
:
public class MainActivity extends ListActivity {
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
}
}
然后,在您的activity_main.xml
中,您需要有一个ID为android.R.id.list