程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-在listview中使用自己的圖像

android-在listview中使用自己的圖像

編輯:編程綜合問答
在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;
}

怎麼出現的這個錯誤呢?

最佳回答:


請看這個例子:

 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);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved