1

嘿,我创建了一个列表视图并使用自定义 ArrayListAdapter 并覆盖了 getView() 方法。我有一个保存按钮来保存已选中的列表视图项目中的数据。因为它们包含复选框。现在的问题是,当我使用 getchildCount 方法保存数据来循环每个项目(行,孩子)时,它只会遍历可见项目。如果我使用 getCount() 方法,它会给出 NullPointerexception,因为它没有考虑不可见的项目。我知道我必须使用我的适配器来做这一切,但是怎么做????????????这是我使用适配器适配器的课程--------------

package com.bmi.cal.hitesh;

公共类早餐扩展活动{

@Override
public void finish() {
    // TODO Auto-generated method stub
    super.finish();
    db.close();
}
float cal_needed=0;
public int counter =0,listItemCount=0,flag=1;
TextView tv_cal_count;
CheckBox        check;
public ListView         list;
ContentValues   values = new ContentValues();
SQLiteDatabase  db;
List<Employer>  data = new ArrayList<Employer>();
Cursor          cur,cur2;
String          type, title,form,descr,tv_cal_str;
int calories;
private Button done;
private TextView tv_cal_needed;
private String cust;
 int temp_diet_id[]= new int[234];
//final String CREATE_TABLE= "CREATE TABLE IF NOT EXISTS Bfast_table (" + "id INTEGER PRIMARY KEY AUTOINCREMENT," + "diet TEXT,calories INTEGER,type INTEGER);"; 
@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         final String MYPREFS = "mySharedPreferences";
         SharedPreferences mySharedPreferences = getSharedPreferences(MYPREFS,0);
        list= (ListView)findViewById(R.id.list);
        done = (Button)findViewById(R.id.button1);
        tv_cal_count = (TextView)findViewById(R.id.tv_cal_count);
        tv_cal_needed = (TextView)findViewById(R.id.tv_calories);
        check = (CheckBox)findViewById(R.id.checkBox1);
        db = openOrCreateDatabase("bmi.db",SQLiteDatabase.CREATE_IF_NECESSARY , null );
        db.setVersion(1);
        db.setLocale(Locale.getDefault());
        cust = mySharedPreferences.getString("txt_cust", null);
        db.setLockingEnabled(false);
        db.execSQL("CREATE TABLE IF NOT EXISTS cust_diet(" + "cd_id INTEGER PRIMARY KEY AUTOINCREMENT," + "cust_id Integer,diet_id INTEGER,cd_validity DATE);");
        try {
            cur = db.query("tbl_diet",null,"type=?",new String[] {"Breakfast"}, null,null, null);
            cur.moveToFirst();
            while(!cur.isAfterLast())
            {
                    title = cur.getString(1);
                    descr = cur.getString(2);
                    form = cur.getString(6);
                    type = cur.getString(5);
                    calories = cur.getInt(3);
                    data.add(new Employer(title,"Calories : " + calories,"("+descr+")",form));
                    cur.moveToNext();
            }
            list.setAdapter(new EmployerArrayAdapter(this, data));

         cur.close();

        } catch (Exception u) {
            u.printStackTrace();
        }
        done.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                int flag = 1;
                listItemCount = list.getCount();
                            // TODO Auto-generated method stub
                System.out.println(cust+"aaaaaaaaaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaa"+list.getCount());
                cur = db.query("user_bmi",null,"cust_id=?",new String[]{cust}, null,null, null);
                System.out.println(cur.getCount()+"aaaaaaaaaaaaaaaa");
                cur.moveToFirst();
                cal_needed = (Float.parseFloat(cur.getString(9)))*(0.25f);                      
                cur.close();
                System.out.println(cust+"bbbbbbb");
                tv_cal_needed.setText("you must not exceed "+ cal_needed +" calories");
                tv_cal_count.setText("calories of your selected diets are as follows : ");
                 for(int i=0; i<listItemCount; i++)
                {   TextView tv_name= (TextView) ((View)list.getChildAt(i)).findViewById(R.id.textViewName);
                     TextView tv_calorie= (TextView) ((View)list.getChildAt(i)).findViewById(R.id.textViewAddress);
                 CheckBox cbox = (CheckBox) ((View)list.getChildAt(i)).findViewById(R.id.checkBox1);
                 if(cbox.isChecked())
                  { tv_cal_str = tv_name.getText().toString(); 
                    cur = db.query("tbl_diet",null,"diet_title=?",new String[] {tv_cal_str}, null,null, null);
                    cur.moveToFirst();
                    temp_diet_id[counter]= cur.getInt(0);;
                    counter++;
                    tv_cal_count.append(tv_calorie.getText()+".");
                    System.out.println(cust+"cccccccc"+temp_diet_id[counter-1]);
                       System.out.println("not saved sill"+counter+"11111111111111111111111");
                     String temp_cal= tv_calorie.getText().toString().substring(11);
                      System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee11111111111111111111111");
                     float temp_calorie = Float.parseFloat(temp_cal);
                      System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee222222222222222222222222");
                        if(temp_calorie>cal_needed)
                        {  System.out.println("eeeeeeeeeeeeeeeeeeeeeee3333333333333333333333333");
                            System.out.println(cust+"dddddddddd");
                            flag=0;

                        }
                  }
                 cur.close();
                  }

               System.out.println("not saved sill"+counter+"22222222222222222222");

                if(counter<=3 && counter>=1)
                {  
                    for(int i=0; i<listItemCount; i++)
                    {
                    CheckBox cbox = (CheckBox) ((View)list.getChildAt(i)).findViewById(R.id.checkBox1);
                    if(cbox.isChecked())
                         cbox.setChecked(false);
                    }
                final int temp_count = counter;
                       System.out.println("not saved sill"+counter+"3333333333333333333333");
                      if(flag==1){
                        AlertDialog.Builder builder = new AlertDialog.Builder(BreakFast.this);
                        builder.setMessage("Do you want to save your BreakFast?")
                               .setCancelable(false)
                               .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int id) {
                                       System.out.println("not saved sill"+temp_count+"3333333333333333333333");
                                        for(int i=0; i<temp_count; i++)
                                                {
                                                    cur2 = db.query("user_bmi",null,"cust_id=? and bmi_active=?",new String[]{cust,"1"}, null,null, null);
                                                    cur2.moveToFirst();
                                                    System.out.println("not saved sill");
                                                    values.put("diet_id",temp_diet_id[i]);
                                                    values.put("cd_validity",cur2.getString(5));
                                                    values.put("cust_id",cust); System.out.println("saved");
                                                    db.insert("cust_diet", null, values);
                                                    cur.close();    
                                                    cur2.close();
                                                }   



                                                           tv_cal_count.setText("Your BreakFast has been saved");
                                                            tv_cal_needed.setText(null);
                                                           done.setVisibility(View.INVISIBLE);                                     }
                               })
                               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                                   public void onClick(DialogInterface dialog, int id) {
                                       for(int i=0; i<listItemCount; i++)
                                    {
                                    CheckBox cbox = (CheckBox) ((View)list.getChildAt(i)).findViewById(R.id.checkBox1);
                                    if(cbox.isChecked())
                                         cbox.setChecked(false);
                                    } 
                                       dialog.cancel();
                                       tv_cal_count.setText(null);
                                   }
                               });
                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                        else
                        {
                            AlertDialog.Builder builder = new AlertDialog.Builder(BreakFast.this);
                            builder.setMessage("please select diet which do not exceed "+cal_needed+" calories!!!")
                                   .setCancelable(false)
                                   .setPositiveButton("OK", new DialogInterface.OnClickListener() 
                                   {
                                       public void onClick(DialogInterface dialog, int id) 
                                       {
                                           tv_cal_count.setText(null);
                                           for(int i=0; i<listItemCount; i++)
                                           {
                                               CheckBox cbox = (CheckBox) ((View)list.getChildAt(i)).findViewById(R.id.checkBox1);
                                               if(cbox.isChecked())
                                               cbox.setChecked(false);
                                            }
                                       }
                                   });

                            AlertDialog alert = builder.create();
                            alert.show();

                        }
                        counter=0;
                    }
                    else
                    {
                        tv_cal_count.setText(null);
                        Dialog mesg = new Dialog(BreakFast.this);
                        TextView text =  new TextView(BreakFast.this);
                        if(counter==0)
                        text.setText("plzz select some items but not more then three!!!");
                        else
                            text.setText("can't select more then three");
                        mesg.setContentView(text);

                        mesg.show();
                        counter=0;
                        for(int i=0; i<listItemCount; i++)
                        {
                        CheckBox cbox = (CheckBox) ((View)list.getChildAt(i)).findViewById(R.id.checkBox1);
                        if(cbox.isChecked())
                             cbox.setChecked(false);
                        }
                    }  



            }
    });
}
4

1 回答 1

0

使用 try catch 块并发布堆栈跟踪。

于 2012-12-16T15:00:24.967 回答