Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different Icons in List View

I have to create a list view , with each item getting a separate icon along the right hand side. I have written a code , but its not working ? how do I proceed ? application crashes

CODE

public class LoginMenu extends ListActivity
{

TextView maintext, subtext;
QuickContactBadge icon;

private static final String[] menuitems = { "Availability", "Messages",
"Greetings", "Address Book", "Calls", "Settings" };

@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
maintext = (TextView) findViewById(R.id.tvMainText);
subtext = (TextView) findViewById(R.id.tvSubText);
setListAdapter(new IconicAdapter());    
}    
class IconicAdapter extends ArrayAdapter<String>
{    
public IconicAdapter()
{
super(LoginMenu.this, R.layout.menu, R.id.tvMainText, menuitems);
// TODO Auto-generated constructor stub
}   
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View row = super.getView(position, convertView, parent);
icon = (QuickContactBadge) findViewById(R.id.iContactBadge);
if (menuitems[0] != null)
{
icon.setImageResource(R.drawable.menu_availability);
}
else if (menuitems[1] != null)
{
icon.setImageResource(R.drawable.menu_messages);
}    
else if (menuitems[2] != null)
{
icon.setImageResource(R.drawable.menu_greetings);
}
else if (menuitems[3] != null)
{
icon.setImageResource(R.drawable.menu_contacts);    
}
else if (menuitems[4] != null)
{
icon.setImageResource(R.drawable.menu_calls);
}
else if (menuitems[5] != null)
{
icon.setImageResource(R.drawable.menu_settings);
}
return (row);
}    
}
}

IMAGE My XML file contains a image , textview and another subtext arranged according to the figure below

Image of the screen needed

LOGCAT

10-11 04:46:15.088: E/AndroidRuntime(726): FATAL EXCEPTION: main
10-11 04:46:15.088: E/AndroidRuntime(726): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.avst.callxpressmobile/com.example.avst.callxpressmobile.MenuScreen}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.os.Looper.loop(Looper.java:137)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-11 04:46:15.088: E/AndroidRuntime(726):  at java.lang.reflect.Method.invokeNative(Native Method)
10-11 04:46:15.088: E/AndroidRuntime(726):  at java.lang.reflect.Method.invoke(Method.java:511)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-11 04:46:15.088: E/AndroidRuntime(726):  at dalvik.system.NativeStart.main(Native Method)
10-11 04:46:15.088: E/AndroidRuntime(726): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:259)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.Activity.setContentView(Activity.java:1867)
10-11 04:46:15.088: E/AndroidRuntime(726):  at com.example.avst.callxpressmobile.MenuScreen.onCreate(MenuScreen.java:22)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.Activity.performCreate(Activity.java:5008)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-11 04:46:15.088: E/AndroidRuntime(726):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-11 04:46:15.088: E/AndroidRuntime(726):  ... 11 more
like image 613
chitranna Avatar asked Oct 11 '12 11:10

chitranna


People also ask

How do I add an icon to a SharePoint list?

Click the chain icon to open the drop-down options and select the columns from your SharePoint list that you would like to display information from. For the icon field, select the name of the column with the icon values. In this example, we use Icons.

How do I add Icons to my list on flutter?

You can use Icon() widget to add icons to your Flutter App. You have to pass the icon data as an icon to this widget. You can use default available Material icons with Icons class.

What does list view look like?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.

What is the function of list view?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down.


2 Answers

You can get simple Code here, which shows an image and some text in each row of the list:

http://www.mkyong.com/android/android-listview-example/

enter image description here

but if the number of rows is high, and if the image in each row is to be fetched from a server then you better use this:

https://github.com/thest1/LazyList

like image 161
Archie.bpgc Avatar answered Oct 13 '22 13:10

Archie.bpgc


Just Use A HashMap<String,String> that will contain your row details (image, two text). Refer to this link that will help you to achieve what you want.

enter image description here

like image 44
K_Anas Avatar answered Oct 13 '22 13:10

K_Anas