2 xiaoyan 12 xiaoyan_12 于 2012.12.11 17:27 提问

在listview中使用自己的图像

我创建的程序里,想把10个图像显示在 ListView视图中
创建的类如下;

public class LayoutAdapterObj {
 public String addString;
 public ImageView leftImage;
 public String backgroundImageName;
 public LayoutAdapterObj(String addString,ImageView leftImageName,String backgroundImageName){
     this.addString = addString;
     this.leftImage = leftImageName;
     this.backgroundImageName = backgroundImageName;
 }

LayoutAdaptor对象

aArray.add(new LayoutAdapterObj("1",new ImageView(null, null, R.drawable.frame1),"R.drawable.layout"));
    aArray.add(new LayoutAdapterObj("2",new ImageView(null, null, R.drawable.frame2),"R.drawable.layout"));
    LayoutListAdapter m_adapter;
    m_adapter = new LayoutListAdapter(this, R.layout.topbarlayout, aArray);

adapter类

 public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        v = vi.inflate(R.layout.layout1, null);
                }
                LayoutAdapterObj o = items.get(position);
                if (o != null) {
                        TextView addText = (TextView) v.findViewById(R.id.addtext);                     
                        ImageView leftImage=(ImageView)v.findViewById(R.id.layoutframe);
                        if(!o.backgroundImageName.equals(""))
                        {

                        }
                        if(addText!=null)
                        {
                            addText.setText(o.addString);
                        }

错误信息:

java.lang.IllegalStateException: Could not execute method of the activity

if(leftImage != null)
{
    leftImage= o.leftImage;
}

怎么出现的这个错误呢?

3个回答

hxn_217
hxn_217   2012.12.19 13:27
已采纳

请看这个例子:

 class UserListAdapter extends ArrayAdapter<String> 
        {

                @Override
            public int getCount() {
                // TODO Auto-generated method stub
                int temp;
                if(SharedVariables.is_traffic_search_ON_flag){
                    temp=search_cases_trafficTicketsList.size();
                }else{
                    temp=search_cases_criminalTicketsList.size();
                }
                return temp;
            }

            Activity context;

            public UserListAdapter(Activity context) {
                super(context, 0);

                Log.d("hh","sgbdfjfdgfk");

                this.context = context;
            }

             class ViewHolder {

                 RelativeLayout rl2;
                 ImageView genderimage;
                 TextView ticketcounttt;
                 TextView name;
                 TextView city;
                 TextView added_date;
            }

            @SuppressWarnings("unused")
            public View getView(final int position, View convertView, ViewGroup parent){//here we inflating the layout "R.layout.cars_row"
                ViewHolder holder;
                View rowView = convertView;
                if (rowView == null) {

                    LayoutInflater inflater = context.getLayoutInflater();

                    if(SharedVariables.is_traffic_search_ON_flag)rowView = inflater.inflate(R.layout.traffic_row, null, true);
                    else rowView = inflater.inflate(R.layout.criminal_row, null, true);

                    holder = new ViewHolder();


                    holder.rl2=(RelativeLayout) rowView.findViewById(R.id.rl2); 

                    try{
                    holder.genderimage=(ImageView) rowView.findViewById(R.id.genderimage);
                    }catch (Exception e) {
                        e.printStackTrace();
                        // TODO: handle exception
                    }
                    holder.ticketcounttt=(TextView) rowView.findViewById(R.id.ticketcounttt);
                    holder.name=(TextView) rowView.findViewById(R.id.textView1);
                    holder.city=(TextView) rowView.findViewById(R.id.textView2);
                    holder.added_date=(TextView) rowView.findViewById(R.id.textView333);

                    //Traffic cases...
                    if(SharedVariables.is_traffic_search_ON_flag)
                    {

                        if(search_cases_trafficTicketsList.get(position).CaseID!=0)holder.rl2.setBackgroundResource(R.drawable.green_color1); //Pink color...
                        else  holder.rl2.setBackgroundResource(R.drawable.pink_color); //Green color...

            if(search_cases_trafficTicketsList.get(position).Sex.equals("M"))holder.genderimage.setBackgroundResource(R.drawable.man);
                        else holder.genderimage.setBackgroundResource(R.drawable.woman);
            holder.ticketcounttt.setTypeface(SharedVariables.font);
            holder.name.setTypeface(SharedVariables.font);
            holder.city.setTypeface(SharedVariables.font);
            holder.added_date.setTypeface(SharedVariables.font);
            holder.name.setText(search_cases_trafficTicketsList.get(position).FirstName+" "+search_cases_trafficTicketsList.get(position).LastName);
            holder.city.setText(search_cases_trafficTicketsList.get(position).City+" City"+"("+search_cases_trafficTicketsList.get(position).ZIP+")");

                        try{

                            String s[]=search_cases_trafficTicketsList.get(position).ViolationDate.split("/");

                            String month=SharedVariables.convertFromNumberToMonth(Integer.parseInt(s[0]));
                            String day=s[1];

                            String year=s[2].substring(0,4);

                        holder.added_date.setText("Added: "+Integer.parseInt(day)+" "+month+" "+year);  
                        }catch (Exception e) {
                            e.printStackTrace();
                            // TODO: handle exception
                        }           

                        try{
                            holder.ticketcounttt.setText(""+search_cases_trafficTicketsList.get(position).Violations.split(",").length);
                        }catch (Exception e) {
                            e.printStackTrace();
                            // TODO: handle exception
                        }       

                    }

                    //Criminal casess...
                    else{

                        if(search_cases_criminalTicketsList.get(position).CaseID!=0)holder.rl2.setBackgroundResource(R.drawable.green_color1); //Pink color...
                        else  holder.rl2.setBackgroundResource(R.drawable.pink_color); //Green color...

                holder.ticketcounttt.setTypeface(SharedVariables.font);
            holder.name.setTypeface(SharedVariables.font);
            holder.city.setTypeface(SharedVariables.font);
            holder.added_date.setTypeface(SharedVariables.font);
            holder.name.setText(search_cases_criminalTicketsList.get(position).FirstName+" "+search_cases_criminalTicketsList.get(position).LastName);
            holder.city.setText(search_cases_criminalTicketsList.get(position).City+" City");

                        try{

                            String s[]=search_cases_criminalTicketsList.get(position).ViolationDate.split("/");

                            String month=SharedVariables.convertFromNumberToMonth(Integer.parseInt(s[0]));
                            String day=s[1];

                            String year=s[2].substring(0,4);

                        holder.added_date.setText("Added: "+Integer.parseInt(day)+" "+month+" "+year);  
                        }catch (Exception e) {
                            e.printStackTrace();
                            // TODO: handle exception
                        }           

                        try{
                            holder.ticketcounttt.setText(""+search_cases_criminalTicketsList.get(position).Violations.split(",").length);
                        }catch (Exception e) {
                            e.printStackTrace();
                            // TODO: handle exception
                        }       

                    }


                    rowView.setTag(holder);                 

                }
                else
                {
                    holder = (ViewHolder) rowView.getTag();
                }

                return rowView;

            }

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


        }

然后再调用 adapter

UserListAdapter a=new UserListAdapter(); yurlistview.setAdapter(a);
A_G_E_N
A_G_E_N   2012.12.12 10:31

不用传drawable对象的,把R.drawable.frame1传进LayoutAdapterObj就行了,在adapter里
context.getResource().getDrawable(o.什么)就可以的

iamkila
iamkila   2012.12.12 09:42

楼主log太少了...
另外 leftImage= o.leftImage; 一般不会把一个控件传递给另一个控件
如果想设置图像 可以传递drawable对象

xiaoyan_12
xiaoyan_12 请问如何传递drawable对象啊?
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!