ListView with Image and text problem...

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

ListView with Image and text problem...

Postby yysiow » Mon May 03, 2010 10:21 am

I want to do a ListView with image and text. and i have a custom adapter code. call IconSimpleAdapter.java

Code: Select all
package com.yysiow.clubmarc;

import java.util.ArrayList;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class IconSimpleAdapter extends BaseAdapter {
   private LayoutInflater mInflater;
   private ArrayList<Map<String, Object>> mList;
   private String[] mIndex;
   
   public IconSimpleAdapter(Context context, String[] index, ArrayList<Map<String, Object>> ArrayList) {
          mInflater = LayoutInflater.from(context); 
          mList = ArrayList; 
          mIndex = index; 
   }

   @Override
   public int getCount() {
      // TODO Auto-generated method stub
      return mList.size();
   }

   @Override
   public Object getItem(int position) {
      // TODO Auto-generated method stub
      return mList.get(position);
   }

   @Override
   public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      try{
         ViewHolder holder = null;
         if (convertView == null) {
            Bitmap mIcon = (Bitmap) mList.get(position).get(mIndex[0]);
            String mItem = " " + (String) mList.get(position).get(mIndex[1]);
            convertView = mInflater.inflate(R.layout.simple_list_item_icon, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.simple_list_item_icon_tv);
            holder.icon = (ImageView) convertView.findViewById(R.id.simple_list_item_icon_iv);
            holder.text.setText(mItem);
            holder.icon.setImageBitmap(mIcon);
            convertView.setTag(holder);
         }else{
            holder = (ViewHolder) convertView.getTag(); 
         }
         return convertView;
      }catch(Exception e){
         Log.e("Error", e.toString());
      }
      return null;
   }
   
   
   private class ViewHolder { 
          TextView text; 
          ImageView icon; 
   }     
}


and my main.java

Code: Select all
for(int subicount = 0; subicount < 21; subicount++){

  bitmap = BitmapFactory.decodeFile("/sdcard/a.jpg");

HashMap<String, Object> item2 = new HashMap<String, Object>();
setTitle(subnamestr);
item2.put(cv.ITEM_ICON, bitmap);
item2.put(cv.ITEM_TEXT, subicount ); 
data2.add(item2);

}

ListView lv = (ListView) findViewById(R.id.ListView01); 
IconSimpleAdapter adapter = new IconSimpleAdapter(this, new String[] {cv.ITEM_ICON, cv.ITEM_TEXT}, data); 
lv.setAdapter(adapter);


now i have a problem. why my list result is not start from 0 to 20, is like below, every 10 repeat.

0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9

who can help my problem..please....urgent.....:( :(
yysiow
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Apr 28, 2010 4:09 am

Top

Postby Beneesh » Mon May 03, 2010 12:12 pm

Hi, may be this code will help u..........

import java.io.File;
import java.util.List;



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;



public class Adapter extends BaseAdapter {
private LayoutInflater inflater;
private Bitmap upIcon;
private Bitmap folderIcon;

private List<String> items;
private List<String> paths;
private float scale;
public Adapter(Context context, List<String> it, List<String> pa) {
inflater = LayoutInflater.from(context);
items = it;
paths = pa;
upIcon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.back02);
folderIcon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.folder);

}

public int getCount() {
// TODO Auto-generated method stub
return items.size();
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.file_row, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

File f = new File(paths.get(position).toString());
holder.icon.setBackgroundColor(0);
if (items.get(position).toString().equals("Home")) {
holder.text.setText("Home");
holder.icon.setImageBitmap(homeIcon);
}


return convertView;
}

/** class ViewHolder */
private class ViewHolder {
TextView text;
ImageView icon;
}

}
Beneesh
Developer
Developer
 
Posts: 26
Joined: Tue Apr 06, 2010 8:18 am
Location: Kerala,India

Postby yysiow » Tue May 04, 2010 7:48 am

Thanks Freshman,

but how to set the Adapter on my main.java?

can you give me some example. Thanks.

like

Code: Select all
for(int subicount = 0; subicount < 21; subicount++){

  bitmap = BitmapFactory.decodeFile("/sdcard/a.jpg");

HashMap<String, Object> item2 = new HashMap<String, Object>();
setTitle(subnamestr);
item2.put(cv.ITEM_ICON, bitmap);
item2.put(cv.ITEM_TEXT, subicount );
data2.add(item2);

}

ListView lv = (ListView) findViewById(R.id.ListView01);
IconSimpleAdapter adapter = new IconSimpleAdapter(this, new String[] {cv.ITEM_ICON, cv.ITEM_TEXT}, data);
lv.setAdapter(adapter);


because your function already different, you have to List.
yysiow
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Apr 28, 2010 4:09 am

Postby Beneesh » Tue May 04, 2010 9:55 am

if u want to call this adapter from main class use


setListAdapter(new Adapter(main.this, arraylist, arraylist));
Beneesh
Developer
Developer
 
Posts: 26
Joined: Tue Apr 06, 2010 8:18 am
Location: Kerala,India

Postby yysiow » Wed May 05, 2010 4:01 am

Thanks Beneesh,I have slove my problem..i just change modify my code..than everything ok lo..

Code: Select all

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      try{
         ViewHolder holder = null;
         if (convertView == null) {

            convertView = mInflater.inflate(R.layout.simple_list_item_icon, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.simple_list_item_icon_tv);
            holder.icon = (ImageView) convertView.findViewById(R.id.simple_list_item_icon_iv);

            convertView.setTag(holder);
         }else{
            holder = (ViewHolder) convertView.getTag();
         }

            Bitmap mIcon = (Bitmap) mList.get(position).get(mIndex[0]);
            String mItem = " " + (String) mList.get(position).get(mIndex[1]);
            holder.text.setText(mItem);
            holder.icon.setImageBitmap(mIcon);

         return convertView;
      }catch(Exception e){
         Log.e("Error", e.toString());
      }
      return null;
   }

yysiow
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Apr 28, 2010 4:09 am

Postby yysiow » Thu May 06, 2010 3:22 am

Now i have another problem on this adapter.

If i want to click the imageview and go to another class. how should i do? below is my code.
I use Intent got error, because my productlistitem.this got problem. how to slove the problem. Thanks.

Code: Select all

      holder.productimg.setOnClickListener(new OnClickListener(){
         
         @Override
         public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.e("TEST", mList.get(position).get(mIndex[1]).toString());
            Intent intent = new Intent(productlistitem.this, submenu.class);
         
         }
         
      });

yysiow
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Apr 28, 2010 4:09 am

Top

Postby Beneesh » Thu May 06, 2010 9:26 am

Hi,

just try with this code



listview= getListView();
listview.setTextFilterEnabled(true);
listview.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {


Intent intent = new Intent(Classname.this,newclass.class);

startActivity(intent);
}

});
Beneesh
Developer
Developer
 
Posts: 26
Joined: Tue Apr 06, 2010 8:18 am
Location: Kerala,India

Postby yysiow » Thu May 06, 2010 9:49 am

Thanks Beneesh, but this one is when click the listview,
but i want to click the button in the listview. any idea??
yysiow
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Apr 28, 2010 4:09 am

Postby Beneesh » Thu May 06, 2010 10:28 am

Hi,
try the list of functions applicable to list view.
Beneesh
Developer
Developer
 
Posts: 26
Joined: Tue Apr 06, 2010 8:18 am
Location: Kerala,India

Postby yysiow » Thu May 06, 2010 10:35 am

sorry, i dont understand. do you have any sample?? Thanks.
yysiow
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Apr 28, 2010 4:09 am

Postby Beneesh » Thu May 06, 2010 10:38 am

Hi,
try

listview.getOnItemClickListener that most probably work
Beneesh
Developer
Developer
 
Posts: 26
Joined: Tue Apr 06, 2010 8:18 am
Location: Kerala,India

Re: ListView with Image and text problem...

Postby PrashantAdesara » Sat Oct 23, 2010 11:38 am

Hello Friends,
I giving your small example for
how to add icon with listview in android.

Below link will help you to how to add icon with listview.
So, Please download and check by yourself.

http://www.4shared.com/file/-MuD6_V4/Ho ... h_Ico.html

and if you have any doubts for that then freely give me comment.

Thanks,
Prashant Adesara
PrashantAdesara
Freshman
Freshman
 
Posts: 6
Joined: Sat Aug 28, 2010 7:24 am

Re: ListView with Image and text problem...

Postby bhargav00200 » Wed Feb 09, 2011 6:41 am

Here is a linkhttp://blog.sptechnolab.com/2011/02/01/android/android-custom-listview-items-and-adapters/. This is also good article which works perfectly without any error.
bhargav00200
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Feb 09, 2011 6:38 am

Top

Return to View, Layout & Resource Problems

Who is online

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