2

我使用具有 imageview、textview 和 checkBox 的自定义适配器制作了一个列表视图

---------------------------------------
<ImageView --- textView --- checkbox >
---------------------------------------

列表视图有一个 onClickListener,复选框有 onCheckListener..

我使用了一个布尔数组(命名为状态)在开始时将复选框设置为选中或未初始化为 false。

假设我在列表中有 19 个项目,并且我检查了它正常工作的第一项的复选框,并且布尔数组的索引 0 更改为 true。但是当我滚动并检查列表的最后一项时,列表的第三个位置被反映并且布尔数组的索引 3 变为真。

许多其他复选框也会被自动选中。

Java 代码

public class Search_list extends Activity {

static String str1 = "v1", str2 = "v2";
String s, item, idd;
static int spnr, range1, range2;
private CarHelper dbcarhelper = null;
private Cursor ourCursor;
CarHelper h = null;
List<String[]> names2 = null;
ListView LV;
MyCustomAdapter dataAdapter = null;
private ArrayList<Boolean> status;

byte[] Image = null;
Cursor c;
Bitmap decodedByte;

String c1_make = null, c1_model = null, c1_ver = null, c2_make = null,
        c2_model = null, c2_ver = null;

int cntr = 0;

Button b1;

CheckBox cb;

protected ProgressDialog dialog;

List<String[]> mlist = new ArrayList<String[]>();
TextView tv1;

ArrayList<String> stg1;

ArrayList<String> arr_make = new ArrayList<String>();
ArrayList<String> arr_model = new ArrayList<String>();
ArrayList<String> arr_ver = new ArrayList<String>();
ArrayList<Bitmap> arr_img = new ArrayList<Bitmap>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_list);

    dialog = ProgressDialog.show(Search_list.this, "Loading",
            "Please Wait...");

    status = new ArrayList<Boolean>(); /

    LV = (ListView) findViewById(R.id.list);

    //------------------ Compare button onClick() ------------------------------

    b1 = (Button) findViewById(R.id.btnCmp);
    b1.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {

            int count = 1;

            if (cntr == 1) {
                Toast.makeText(getApplicationContext(),
                        "Please select atleast two cars!",
                        Toast.LENGTH_LONG).show();
            } else {

                for (int i = 0; i < status.size(); i++) {

                    System.out.println("Compare click Position= " + i + " Status: "
                            + status.get(i));
                    // System.out.println();

                    if (status.get(i)) {

                        if (count == 2) {
                            c2_make = arr_make.get(i);
                            c2_model = arr_model.get(i);
                            c2_ver = arr_ver.get(i);

                            count++;
                        }

                        if (count == 1) {
                            c1_make = arr_make.get(i);
                            c1_model = arr_model.get(i);
                            c1_ver = arr_ver.get(i);

                            count++;
                        }

                    } else {

                    }

                }

                Intent intent = new Intent(Search_list.this, DispComp.class);
                Bundle b = new Bundle();

                b.putString("car1", c1_make);
                b.putString("carm1", c1_model);
                b.putString("carv1", c1_ver);

                b.putString("car2", c2_make);
                b.putString("carm2", c2_model);
                b.putString("carv2", c2_ver);

                intent.putExtras(b);
                startActivity(intent);
            }
        }
    });

    //--------------------------------------------------------------------------

    dbcarhelper = new CarHelper(this);
    dbcarhelper.createDatabase();
    dbcarhelper.openDataBase();

    if (s != null) {
        startManagingCursor(ourCursor);
    }

    if (spnr == 1) {

        byMakeModel();
    }

    if (spnr == 2) {

        byBudgetFuel();

    }

    fill_list();

    for (int i = 0; i < arr_make.size(); i++) {
        status.add(false);
    }

    System.out.println("Inside onCreate() ... status all null..! ");

    dialog.dismiss();

}

public void SetId(String s1, String s2, int s) {

    System.out.println("SetMake spnr= |" + spnr + "|");
    System.out.println("SetMake s= |" + s + "|");

    str1 = s1;
    str2 = s2;
    spnr = s;

    System.out.println("SetMake aftr spnr= |" + spnr + "|");

}

public void SetBudgetFuel(String s1, String s2, int s, int r1, int r2) {

    System.out.println("SetBudget spnr= |" + spnr + "|");
    System.out.println("SetBudget s= |" + s + "|");

    System.out.println("SetBudget r1= |" + r1 + "|");
    System.out.println("SetBudget r2= |" + r2 + "|");

    str1 = s1;
    str2 = s2;
    spnr = s;
    range1 = r1;
    range2 = r2;

    System.out.println("SetMake aftr spnr= |" + spnr + "|");

}

// -------------------------------------------------------------------------

public void byMakeModel() {

    // make n model selected *******************************

    if (str2.equalsIgnoreCase("(Select)")) {

        dbcarhelper.search_by_make(str1);

    } else {

        dbcarhelper.search_by_makemodel(str1, str2);

    }

}

// -------------------------------------------------------------------------

public void byBudgetFuel() {

    // budget or fuel selected
    // ************************************************

    if (str1.equalsIgnoreCase("Any Budget")) {

        dbcarhelper.search_by_fuel(str2);

    }

    else if (str2.equalsIgnoreCase("All")) {

        dbcarhelper.search_by_budget(range1, range2);

    }

    else {

        dbcarhelper.search_by_budgetfuel(str2, range1, range2);

    }

}

// -------------------------------------------------------------------------

public void fill_list() {
    final CarHelper h = new CarHelper(getApplicationContext());

    Toast.makeText(getApplicationContext(), "Inside if case",
            Toast.LENGTH_LONG).show();

    mlist = h.selectAllb2();

    stg1 = new ArrayList<String>();

    int x = 0;
    String stg;

    for (String[] srch : mlist) {

        stg = "Make: " + srch[0] + "\nModel: " + srch[1] + " \nVersion: "
                + srch[2] + "\nPrice: Rs. " + srch[3];

        stg1.add(stg);

        arr_make.add(srch[0]);
        arr_model.add(srch[1]);
        arr_ver.add(srch[2]);

        System.out.println("srch 0" + srch[0] + "\nsrch 1" + srch[1]
                + "\nsrch 2" + srch[2] + "\nsrch 3" + srch[3]);

        x++;
    }

    System.out.println("length--------------------- " + stg1.size());

    // ----------------Image--------------------
    int run = 0;
    c = dbcarhelper.fetchImgRow("1");

    if (c.moveToFirst()) {
        do {

            Image = c.getBlob(c.getColumnIndex("img_str"));

            System.out.println("Image = " + Image);

            System.out.println("run = " + run);

            byte[] decodedString = Base64.decode(Image, Base64.DEFAULT);
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
                    decodedString.length);

            arr_img.add(decodedByte);

            System.gc();

            run++;

        } while (c.moveToNext());

    }
    dbcarhelper.close();

    // ----------------Image--------------------

    System.out.println("run after close() = " + run);

    dataAdapter = new MyCustomAdapter(this, R.layout.new_search_adptr, stg1);

    LV.setAdapter(dataAdapter);

    int[] colors = { 0, 0xff00ffff, 0 };
    LV.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
    LV.setDividerHeight(4);

    LV.setOnItemClickListener(new OnItemClickListener() {

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

            New_Details det = new New_Details();

            System.out.println("Inside Click !");

            String car1 = h.srch_make.get(position);
            String carm1 = h.srch_model.get(position);
            String carv1 = h.srch_ver.get(position);

            det.setImg(arr_img.get(position));

            // Bitmap car_img = arr_img.get(position);
            // System.out.println("position: " + position);
            // System.out.println("arr_img position: " +
            // arr_img.get(position));

            Intent intent = new Intent(Search_list.this, New_Details.class);
            Bundle b = new Bundle();
            b.putString("car1", car1);
            b.putString("carm1", carm1);
            b.putString("carv1", carv1);

            intent.putExtras(b);
            startActivity(intent);

        }
    });

}

private class MyCustomAdapter extends ArrayAdapter<String> {

    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<String> sList) {
        super(context, textViewResourceId, sList);

    }

    private class ViewHolder {
        TextView text;
        CheckBox chkbox;
        ImageView imageview;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        ViewHolder holder = null;


        //Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.new_search_adptr, null);

            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.adapterText1);
            holder.chkbox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);

            //---------- Setting the checkBoxes -----------------------------------

            if (status.get(position)) {

                holder.chkbox.setChecked(true);

            } else {

                holder.chkbox.setChecked(false);
            }

            //-----------------------------------------------------------------

            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.imageView1);

            convertView.setTag(holder);

            holder.chkbox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    CheckBox cb = (CheckBox) v;
                    if (cb.isChecked()) {

                        // User checked (selected) a car
                        // *********************************

                        if (cntr > 1) {

                            // More than two cars selected

                            Toast.makeText(getApplicationContext(),
                                    "Please select only two cars!",
                                    Toast.LENGTH_LONG).show();

                            cb.setChecked(false);

                        } else {

                            // car selected


                            status.set(position, true);
                            cntr++;

                            System.out.println("Chk List Position= " + position );

                            for (int i = 0; i < status.size(); i++) {

                                System.out.println("Chkbox chkd Position= " + i + " Status: "
                                        + status.get(i));
                            }
                        }

                    } else {

                        // User Unchecked (de - selected) a car
                        // *********************************


                        cntr--;
                        status.set(position, false);

                        System.out.println("Unchk List Position= " + position );

                        for (int i = 0; i < status.size(); i++) {

                            System.out.println("UnChkbox chkd Position= " + i + " Status: "
                                    + status.get(i));
                        }

                    }

                }
            });

        } else {

            holder = (ViewHolder) convertView.getTag();
            holder.chkbox.setOnCheckedChangeListener(null);

        }

        holder.text.setText(stg1.get(position).toString());

        // setting image
        holder.imageview.setImageBitmap(arr_img.get(position));

        return convertView;
    }
}

}

请帮我找到解决方案。谢谢你。

堆栈跟踪

08-17 06:58:16.798: W/dalvikvm(698): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-17 06:58:16.808: E/AndroidRuntime(698): FATAL EXCEPTION: main
08-17 06:58:16.808: E/AndroidRuntime(698): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:405)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:418)
08-17 06:58:16.808: E/AndroidRuntime(698):  at com.MyAuto.Search_list.fill_list(Search_list.java:334)
08-17 06:58:16.808: E/AndroidRuntime(698):  at com.MyAuto.Search_list.onCreate(Search_list.java:196)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.os.Looper.loop(Looper.java:123)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-17 06:58:16.808: E/AndroidRuntime(698):  at java.lang.reflect.Method.invokeNative(Native Method)
08-17 06:58:16.808: E/AndroidRuntime(698):  at java.lang.reflect.Method.invoke(Method.java:507)
08-17 06:58:16.808: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-17 06:58:16.808: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-17 06:58:16.808: E/AndroidRuntime(698):  at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

1

供参考使用示例

使用 Listview 中的复选框获取所选项目

将您的适配器类更改为以下。

您需要覆盖getCount 并使用SparseBooleanArray

private class MyCustomAdapter extends ArrayAdapter<String> implements CompoundButton.OnCheckedChangeListener{

    ArrayList<String> myList; 
    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<String> sList) {
        super(context, textViewResourceId, sList);
        mCheckStates = new SparseBooleanArray(sList.size());
         myList= sList;
    }
    private SparseBooleanArray mCheckStates;
    private class ViewHolder {
        TextView text;
        CheckBox chkbox;
        ImageView imageview;
    }

    @Override
    public int getCount() {
        return stg1.size();
    }

    @Override
    public View getView(final int position, View convertView,ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.new_search_adptr, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.adapterText1);
            holder.chkbox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.imageView1);
            convertView.setTag(holder);          

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.chkbox.setTag(position);
        holder.chkbox.setChecked(mCheckStates.get(position, false));
        holder.chkbox.setOnCheckedChangeListener(this);
        holder.text.setText(stg1.get(position).toString());
        holder.imageview.setImageBitmap(arr_img.get(position));
        return convertView;
    }

     public boolean isChecked(int position) {
         return mCheckStates.get(position, false);
     }

     public void setChecked(int position, boolean isChecked) {
         mCheckStates.put(position, isChecked);

     }

     public void toggle(int position) {
         setChecked(position, !isChecked(position));

     }
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {


         mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

    }
}

要上按钮单击

StringBuilder result = new StringBuilder();
for(int i=0;i<youradapter.mCheckStates.size();i++)
{
if(youradapter.mCheckStates.get(i)==true)
{
result.append(arr_make.get(i).toString()+" "+arr_model.get(i).toString()+" "+arr_ver.get(i).toString());
result.append("\n");
}
}
Toast.makeText(MainActivity.this, result, 1000).show();
}
于 2013-08-12T06:10:53.773 回答
1

在您的getView()情况下,您只需在新创建的视图上设置选中状态。当视图被回收(convertView != null)时,检查状态将是旧的回收视图的状态。

将“设置复选框”代码部分的缩进级别移到该if (converView == null) ... else部分之后。也就是说,从这里更改您的代码:

    if (convertView == null) {

        //...
        //create view and init holder

        //---------- Setting the checkBoxes -----------------------------------

        if (status.get(position)) {
            holder.chkbox.setChecked(true);
        } else {
            holder.chkbox.setChecked(false);
        }

        //...

    } else {
        holder = (ViewHolder) convertView.getTag();
        holder.chkbox.setOnCheckedChangeListener(null);
    }

对此:

    if (convertView == null) {

        //...

        //create view and init holder

    } else {
        holder = (ViewHolder) convertView.getTag();
        holder.chkbox.setOnCheckedChangeListener(null);
    }


    //---------- Setting the checkBoxes -----------------------------------

    if (status.get(position)) {
        holder.chkbox.setChecked(true);
    } else {
        holder.chkbox.setChecked(false);
    }
于 2013-08-12T06:40:13.860 回答
0

我在自定义适配器中声明了布尔数组。我也删除了 if 语句,即

如果(转换视图 == 空)

public class Search_list extends Activity {

static String str1 = "v1", str2 = "v2";
String s, item, idd;
static int spnr, range1, range2;
private CarHelper dbcarhelper = null;
private Cursor ourCursor;
CarHelper h = null;
List<String[]> names2 = null;
ListView LV;
MyCustomAdapter dataAdapter = null;

byte[] Image = null;
Cursor c;
Bitmap decodedByte;

String c1_make = null, c1_model = null, c1_ver = null, c2_make = null,
        c2_model = null, c2_ver = null;

int cntr = 0;

Button b1;

CheckBox cb;

protected ProgressDialog dialog;

List<String[]> mlist = new ArrayList<String[]>();
TextView tv1;

ArrayList<String> stg1;

ArrayList<String> arr_make = new ArrayList<String>();
ArrayList<String> arr_model = new ArrayList<String>();
ArrayList<String> arr_ver = new ArrayList<String>();
ArrayList<Bitmap> arr_img = new ArrayList<Bitmap>();



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_list);

    dialog = ProgressDialog.show(Search_list.this, "Loading",
            "Please Wait...");


    LV = (ListView) findViewById(R.id.list);

    //------------------ Compare button onClick() ------------------------------

    b1 = (Button) findViewById(R.id.btnCmp);
    b1.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {

            int count = 1;

            if (cntr == 1) {
                Toast.makeText(getApplicationContext(),
                        "Please select atleast two cars!",
                        Toast.LENGTH_LONG).show();
            } else {

                for (int i = 0; i < dataAdapter.status.size(); i++) {

                    System.out.println("Compare click Position= " + i + " Status: "
                            + dataAdapter.status.get(i));
                    // System.out.println();

                    if (dataAdapter.status.get(i)) {

                        if (count == 2) {
                            c2_make = arr_make.get(i);
                            c2_model = arr_model.get(i);
                            c2_ver = arr_ver.get(i);

                            count++;
                        }

                        if (count == 1) {
                            c1_make = arr_make.get(i);
                            c1_model = arr_model.get(i);
                            c1_ver = arr_ver.get(i);

                            count++;
                        }

                    } else {

                    }

                }

                Intent intent = new Intent(Search_list.this, DispComp.class);
                Bundle b = new Bundle();

                b.putString("car1", c1_make);
                b.putString("carm1", c1_model);
                b.putString("carv1", c1_ver);

                b.putString("car2", c2_make);
                b.putString("carm2", c2_model);
                b.putString("carv2", c2_ver);

                intent.putExtras(b);
                startActivity(intent);
            }
        }
    });

    //--------------------------------------------------------------------------    

            dbcarhelper = new CarHelper(this);
    dbcarhelper.createDatabase();
    dbcarhelper.openDataBase();

    if (s != null) {
        startManagingCursor(ourCursor);
    }

    if (spnr == 1) {

        byMakeModel();
    }

    if (spnr == 2) {

        byBudgetFuel();

    }

    fill_list();

    System.out.println("Inside onCreate() ... status all null..! ");

    dialog.dismiss();

}

public void SetId(String s1, String s2, int s) {

    System.out.println("SetMake spnr= |" + spnr + "|");
    System.out.println("SetMake s= |" + s + "|");

    str1 = s1;
    str2 = s2;
    spnr = s;

    System.out.println("SetMake aftr spnr= |" + spnr + "|");

}

public void SetBudgetFuel(String s1, String s2, int s, int r1, int r2) {

    System.out.println("SetBudget spnr= |" + spnr + "|");
    System.out.println("SetBudget s= |" + s + "|");

    System.out.println("SetBudget r1= |" + r1 + "|");
    System.out.println("SetBudget r2= |" + r2 + "|");

    str1 = s1;
    str2 = s2;
    spnr = s;
    range1 = r1;
    range2 = r2;

    System.out.println("SetMake aftr spnr= |" + spnr + "|");

}

// -------------------------------------------------------------------------

public void byMakeModel() {

    // make n model selected *******************************

    if (str2.equalsIgnoreCase("(Select)")) {

        dbcarhelper.search_by_make(str1);

    } else {

        dbcarhelper.search_by_makemodel(str1, str2);

    }

}

// -------------------------------------------------------------------------

public void byBudgetFuel() {

    // budget or fuel selected
    // ************************************************

    if (str1.equalsIgnoreCase("Any Budget")) {

        dbcarhelper.search_by_fuel(str2);

    }

    else if (str2.equalsIgnoreCase("All")) {

        dbcarhelper.search_by_budget(range1, range2);

    }

    else {

        dbcarhelper.search_by_budgetfuel(str2, range1, range2);

    }

}

// -------------------------------------------------------------------------

public void fill_list() {
    final CarHelper h = new CarHelper(getApplicationContext());

    Toast.makeText(getApplicationContext(), "Inside if case",
            Toast.LENGTH_LONG).show();

    mlist = h.selectAllb2();

    stg1 = new ArrayList<String>();

    int x = 0;
    String stg;

    for (String[] srch : mlist) {

        stg = "Make: " + srch[0] + "\nModel: " + srch[1] + " \nVersion: "
                + srch[2] + "\nPrice: Rs. " + srch[3];

        stg1.add(stg);

        arr_make.add(srch[0]);
        arr_model.add(srch[1]);
        arr_ver.add(srch[2]);


        System.out.println("srch 0" + srch[0] + "\nsrch 1" + srch[1]
                + "\nsrch 2" + srch[2] + "\nsrch 3" + srch[3]);

        x++;
    }

    System.out.println("length--------------------- " + stg1.size());

    // ----------------Image--------------------
    int run = 0;
    c = dbcarhelper.fetchImgRow("1");

    if (c.moveToFirst()) {
        do {

            Image = c.getBlob(c.getColumnIndex("img_str"));

            System.out.println("Image = " + Image);

            System.out.println("run = " + run);

            byte[] decodedString = Base64.decode(Image, Base64.DEFAULT);
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
                    decodedString.length);

            arr_img.add(decodedByte);

            System.gc();

            run++;

        } while (c.moveToNext());

    }
    dbcarhelper.close();

    // ----------------Image--------------------

    System.out.println("run after close() = " + run);

    dataAdapter = new MyCustomAdapter(this, R.layout.new_search_adptr, stg1);

    LV.setAdapter(dataAdapter);

    int[] colors = { 0, 0xff00ffff, 0 };
    LV.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
    LV.setDividerHeight(4);

    LV.setOnItemClickListener(new OnItemClickListener() {

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

            New_Details det = new New_Details();

            System.out.println("Inside Click !");

            String car1 = h.srch_make.get(position);
            String carm1 = h.srch_model.get(position);
            String carv1 = h.srch_ver.get(position);

            det.setImg(arr_img.get(position));


            Intent intent = new Intent(Search_list.this, New_Details.class);
            Bundle b = new Bundle();
            b.putString("car1", car1);
            b.putString("carm1", carm1);
            b.putString("carv1", carv1);

            intent.putExtras(b);
            startActivity(intent);

        }
    });

}

private class MyCustomAdapter extends ArrayAdapter<String> {

     private ArrayList<Boolean> status = new ArrayList<Boolean>();

    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<String> sList) {
        super(context, textViewResourceId, sList);

        for (int i = 0; i < arr_make.size(); i++) {
            status.add(false);
        }

        System.out.println("Inside My Custom Adapter.. status all null..! ");

    }

    private class ViewHolder {
        TextView text;
        CheckBox chkbox;
        ImageView imageview;
    }


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

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

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        ViewHolder holder = null;


        //Log.v("ConvertView", String.valueOf(position));

        //if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.new_search_adptr, null);

            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.adapterText1);
            holder.chkbox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);

            //---------- Setting the checkBoxes ------------------------

            if (status.get(position)) {

                holder.chkbox.setChecked(true);

            } else {

                holder.chkbox.setChecked(false);
            }

            //----------------------------------------------------------

            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.imageView1);

            convertView.setTag(holder);

            holder.chkbox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    CheckBox cb = (CheckBox) v;
                    if (cb.isChecked()) {

                        // User checked (selected) a car
                        // *********************************

                        if (cntr > 1) {

                            // More than two cars selected

                            Toast.makeText(getApplicationContext(),
                                    "Please select only two cars!",
                                    Toast.LENGTH_LONG).show();

                            cb.setChecked(false);

                        } else {

                            // car selected

                            status.set(position, true);
                            cntr++;

                            System.out.println("Chk List Position= " + position );

                            for (int i = 0; i < status.size(); i++) {

                                System.out.println("Chkbox chkd Position= " + i + " Status: "
                                        + status.get(i));
                            }
                        }

                    } else {

                        // User Unchecked (de - selected) a car
                        // *********************************


                        cntr--;
                        status.set(position, false);

                        System.out.println("Unchk List Position= " + position );

                        for (int i = 0; i < status.size(); i++) {

                            System.out.println("UnChkbox chkd Position= " + i + " Status: "
                                    + status.get(i));
                        }

                    }

                }
            });


        holder.text.setText(stg1.get(position).toString());

        // setting image
        holder.imageview.setImageBitmap(arr_img.get(position));

        return convertView;
    }
}

}
于 2013-08-12T08:35:57.600 回答