自定义列表视图适配器,具有监听器复选框

时间:2013-06-24 11:56:34

标签: android listview checkbox

我有一个带onItemClickListener的列表视图和一个带复选框的自定义适配器。该复选框还有一个onClickListener。但我正在向适配器提供的arraylist没有显示文本。谢谢!

这是我的代码:

 public class Search_list extends Activity {

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

CheckBox cb;

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

// String[] stg1;
ArrayList<String> stg1;


ArrayList<String> ids_pos = new ArrayList<String>();

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

    LV = (ListView) findViewById(R.id.list);
    // tv1 = (TextView) findViewById(R.id.noRec);
    // tv1.setVisibility(View.GONE);

    // get_list();

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

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

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

    if (spnr == 1) {

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

        byMakeModel();
    }

    if (spnr == 2) {

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

        byBudgetFuel();

    }

    fill_list();

}


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
    // ************************************************

    System.out.println("Inside byBudgetFuel()");

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

        System.out.println("|" + str1 + "|");

        dbcarhelper.search_by_fuel(str2);

    }

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

        System.out.println("Inside Budget()");

        dbcarhelper.search_by_budget(range1, range2);

    }

    else {

        System.out.println("Inside byBudgetFuel()");

        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 String[mlist.size()];
    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[x] = stg;
        stg1.add(stg);
        x++;
    }



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

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

          System.out.println("length " + stg1.get(i));

      }

    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) {

            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);

            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<String> myAdptr;

    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<String> sList) {
        super(context, textViewResourceId, sList);
        this.myAdptr = new ArrayList<String>();
        this.myAdptr.addAll(stg1);
    }

    private class ViewHolder {
        TextView code;
        CheckBox name;
    }

    @Override
    public View getView(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.code = (TextView) convertView
                    .findViewById(R.id.adapterText1);
            holder.name = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);

            convertView.setTag(holder);

            holder.name.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);

                    CheckBox cb = (CheckBox) v;

                     Toast.makeText(getApplicationContext(), "Clicked on Checkbox: " + cb.getId(), 
                     Toast.LENGTH_LONG).show();


                }
            });

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

        String state = myAdptr.get(position);

        return convertView;
    }

}

}

Screenshot

0 个答案:

没有答案