Reputation: 4497
I'm following that tutorial:
http://hmkcode.com/android-custom-listview-titles-icons-counter/
I have following codes:
../layout/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>
*../layout/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;
}
}
My problem
Okay, I want add one ListView because I need clic events on items.
On tutorial I can read that on 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);
I tried do that but not work, I don't know how I can do that.
UPDATE
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);
//.......
And I added activity_main.xml with ListView called R.id.list
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="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
ERROR
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
UPDATE 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>
How I can implement ListView?
SOLUTION
On MainActivity.java I added ListView list = getListView(); instead ListView list = (ListView) findViewById(android.R.id.list);
and on activity_main.xml I added
//.....
android:id="@android:id/list"
//....
Thanks
Upvotes: 0
Views: 1809
Reputation: 81549
You need ANDROID.R.id.list, which is done as the following:
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
</ListView>
Upvotes: 1
Reputation: 6220
Your logcat says Your content must have a ListView whose id attribute is 'android.R.id.list'
In your R.layout.activity_main
you need a ListView
with id android.R.id.list
because your Activity is extending from ListActivity
. And it expect an ListView
who is not finding. So, in your MainActivity
:
public class MainActivity extends ListActivity {
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
}
}
And then, in your activity_main.xml
you need to have a listiview with id android.R.id.list
Upvotes: 0