2012-12-16 3 views
1

안녕하세요 저는 listview를 만들고 사용자 정의 ArrayListAdapter를 사용하고 getView() 메소드를 오버라이드했습니다. 체크 된 목록보기 항목에서 데이터를 저장하는 저장 버튼이 있습니다. 체크 박스가 포함되어 있습니다. 이제 문제는 각 항목 (Row, Child)을 반복하기 위해 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); 
         } 
        } 



      } 
    }); 
} 

답변

0

사용이 catch 블록을 시도하고 스택을 게시 확장 자취.

+0

plz 더 구체적으로 말하려는 것을 이해하지 못한다 ......... 내가 실제로 묻고있는 것을 확인해 주겠다 .. – Arpit

+0

오류 줄을 알 수 있도록 로그 고양이를 게시하십시오. . –

+0

선생님, 목록보기 항목을 반복하는 것이 표시되지 않는 항목에 대한 액세스 권한을 얻으려고하면 표시되는 항목에만 제한되기 때문에 오류가 발생합니다. 사용할 수 없으므로보기가 없으므로 null 포인터 예외가 발생합니다. listview .... – Arpit

관련 문제