ListActivity with icons on each row?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

ListActivity with icons on each row?

Postby p0lar » Fri Jul 03, 2009 7:37 pm

I have a listactivity that I populate with a list of books. how can I add a graphic to the beginning of each line? I have the graphics for each book and I want to display them before each books title in the list.

Any suggestions on where to start?

p0lar
p0lar
Freshman
Freshman
 
Posts: 9
Joined: Thu Feb 05, 2009 12:07 am

Top

Samples have solutions

Postby abhsax130778 » Fri Jul 10, 2009 9:39 am

You can find solution by having a good look in the samples provided with Android SDK.

I can get you the path:

<installation folder>\Samples\ApiDemos\src\com\example\android\apis\view\list14.java

you may be knowing how to customize it if you are a good programmer.

Please see the associated layout XML. This XML only lets you position the items with graphics.

you can change the bitmaps with the array of bitmaps with each one corresponding to the book.

one item is added at a time, and the only thing common between the two arrays are position coming from the

overridden function that returns View.

Regards
Attachments
listWithGraphics.png
If this is you want
listWithGraphics.png (527.36 KiB) Viewed 1706 times
Thanks and regards
Abhinav
abhsax130778
Junior Developer
Junior Developer
 
Posts: 13
Joined: Thu Jul 09, 2009 6:11 am
Location: NOIDA, UP, India

I need ur source code..for displaying icon in the Listview

Postby Raghav_s » Wed Sep 23, 2009 12:17 pm

hi,
I need code for Displaying icon in List view....
Raghav_s
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Sep 23, 2009 11:25 am
Location: Chennai

Postby Shalni » Wed Oct 07, 2009 8:08 am

Hi,

here is the code...

public class List14 extends ListActivity {

private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;

public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);

// Icons bound to the rows.
mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
}

/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return DATA.length;
}

/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position) {
return position;
}

/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}

/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;

// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);

convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}

// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

return convertView;
}

static class ViewHolder {
TextView text;
ImageView icon;
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new EfficientAdapter(this));
}

private static final String[] DATA = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam"};
}


This is there in API Demos. U can refer the same
Shalni
Junior Developer
Junior Developer
 
Posts: 17
Joined: Wed Apr 29, 2009 6:31 am

Listview Indicator....

Postby Raghav_s » Wed Oct 07, 2009 8:23 am

Thanks for ur kind reply Shalni.....

Here i want one more.....wat means....i dont know how to display Listview Indicator....
for example i m having 4 listitems ....for each listitem i want to display indicator ">" sign...it shows there is another child view in listitem.....for example visit.....http://code.google.com/intl/nl/android/ ... html?id=17

Thanks in Advance......

Raghav.S
Raghav_s
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Sep 23, 2009 11:25 am
Location: Chennai

Re: Listview Indicator....

Postby android2 » Wed Oct 07, 2009 11:31 am

then you can put a working example with source code
Raghav_s wrote:Thanks for ur kind reply Shalni.....

Here i want one more.....wat means....i dont know how to display Listview Indicator....
for example i m having 4 listitems ....for each listitem i want to display indicator ">" sign...it shows there is another child view in listitem.....for example visit.....http://code.google.com/intl/nl/android/ ... html?id=17

Thanks in Advance......

Raghav.S
android2
Developer
Developer
 
Posts: 48
Joined: Sun Oct 04, 2009 8:34 pm

Top

Postby Raghav_s » Wed Oct 07, 2009 12:28 pm

Here is my Code:

package my.country;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class welcomeuser extends ListActivity {
/** Called when the activity is first created. */

TextView selection;
String[] items={"India","Australia","South Africa"
,"America"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.main);
setListAdapter(new IconicAdapter(this));

selection=(TextView)findViewById(R.id.selection);
getListView().setTextFilterEnabled(true);

}
public void onListItemClick(ListView parent, View v,
int position, long id) {

super.onListItemClick(parent, v, position, id);

if(position==0)

{
//some codes here
}
else if(position == 1)
{
//some codes here
}
else if(position == 2)
{

//some codes here
}

else if(position==3)
{

//some codes here
}

}
class IconicAdapter extends ArrayAdapter {
Activity context;
@SuppressWarnings("unchecked")
IconicAdapter(Activity context) {
super(context, R.layout.main, items);
this.context=context;
}
public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View row=inflater.inflate(R.layout.userwelcome, null);
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(items[position]);
ImageView icon=(ImageView)row.findViewById(R.id.icon);
if (items[position].toString().equals("Categories"))
{
icon.setImageResource(R.drawable.wedding1);
}

else if (items[position].toString().equals("My Wedding Day"))
{
icon.setImageResource(R.drawable.wedding);
}
else if (items[position].toString().equals("My Wedding To Do List"))
{
icon.setImageResource(R.drawable.todo11);
}
else if (items[position].toString().equals("Organizer"))
{
icon.setImageResource(R.drawable.organizer);
}
else if (items[position].toString().equals("My Wedding")) {
icon.setImageResource(R.drawable.mywedding);
}
else if (items[position].toString().equals("")) {
icon.setImageResource(R.drawable.weddingmain);
}
return(row);
}
}
}
Raghav_s
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Sep 23, 2009 11:25 am
Location: Chennai

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests