Android初学者:带图像和复选框的ListView

时间:2012-05-17 00:12:47

标签: java android eclipse android-layout android-intent

我是初学程序员,所以请耐心等待。我已经能够创建一个包含图像的列表视图,并认为我可以修改它以添加一个复选框。但是,此设置不起作用。有关如何修复代码以创建包含图像和复选框的列表视图的任何建议吗?

 import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CheckboxList extends ListActivity{
        private LayoutInflater mInflater;
        private Vector<RowData> data;
        RowData rd;
        static CheckBox box0,box1,box2,box3;

        static final String[] title = new String[] {
                "Box 1", "Box 2",   

        "Box 3 ","Box 4"        };

        static final CheckBox[] boxes= new CheckBox[] {box0,box1,box2,box3};

        private Integer[] imgid = {
          R.drawable.magnum,R.drawable.mg,R.drawable.mk,
          R.drawable.mk46};

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mInflater = (LayoutInflater) getSystemService(
        Activity.LAYOUT_INFLATER_SERVICE);
        data = new Vector<RowData>();
        for(int i=0;i<title.length;i++){

        try {
            rd = new RowData(i,title[i],boxes[i]);
            } catch (ParseException e) {
                e.printStackTrace();
           }
           data.add(rd);
        }
           CustomAdapter adapter = new CustomAdapter(this, R.layout.check_list,
                                             R.id.title, data);
           setListAdapter(adapter);
           getListView().setTextFilterEnabled(true);
        }
           public void onListItemClick(ListView parent, View v, int position,
                                                                        long id) {          

           Toast.makeText(getApplicationContext(), "You have selected "
                            +(position+1)+"th item",  Toast.LENGTH_SHORT).show();
        }
               private class RowData {
               protected int mId;
               protected String mTitle;
               protected CheckBox mCBox;
               RowData(int id,String title,CheckBox cbox){
               mId=id;
               mTitle = title;
               mCBox=cbox;
            }
               @Override
               public String toString() {
                       return mId+" "+mTitle+" "+mCBox;
               }
        }
          private class CustomAdapter extends ArrayAdapter<RowData> {

          public CustomAdapter(Context context, int resource,
                                int textViewResourceId, List<RowData> objects) {               

         super(context, resource, textViewResourceId, objects);
        }
              @Override
               public View getView(int position, View convertView, ViewGroup parent) {   

               ViewHolder holder = null;
               TextView title = null;
               CheckBox box = null;
               ImageView i11=null;
               RowData rowData= getItem(position);
               if(null == convertView){
                    convertView = mInflater.inflate(R.layout.check_list, null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
         }
                     holder = (ViewHolder) convertView.getTag();
                     title = holder.gettitle();
                     title.setText(rowData.mTitle);

                     box = holder.getcheckbox();
                     box.setChecked(false);                                                    

                     i11=holder.getImage();
                     i11.setImageResource(imgid[rowData.mId]);
                     return convertView;
        }
                    private class ViewHolder {
                    private View mRow;
                    private TextView title = null;
                    private CheckBox cbox = null;
                    private ImageView i11=null; 

                    public ViewHolder(View row) {
                    mRow = row;
         }
                 public TextView gettitle() {
                     if(null == title){
                         title = (TextView) mRow.findViewById(R.id.title);
                        }
                    return title;
                 }     

                 public CheckBox getcheckbox() {
                     if(null == cbox){
                          cbox = (CheckBox) mRow.findViewById(R.id.checkBox);
                            }
                   return cbox;
                 }
                public ImageView getImage() {
                     if(null == i11){
                          i11 = (ImageView) mRow.findViewById(R.id.img);
                                              }
                        return i11;
                }
             }
           } }

1 个答案:

答案 0 :(得分:1)