2013-04-07 53 views
1

在許多佈局我調用同一個函數RD_Database_InfoClasse(String num_classe)( 的代碼在每個佈局相同。如何創建一個獨特的類?

我想創建一個獨特的功能OD RD_Database_InfoClasse(String num_classe)。 當我嘗試這個,我在一個錯誤getActivity & getView,我該怎麼辦

這裏是代碼:

public class A_Page_Classe9 extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.a_page_classe9_layout,container, false); 
    } 
    public void onActivityCreated (Bundle savedInstanceState){ 
     RD_Database_InfoClasse("9"); 
     super.onActivityCreated(savedInstanceState); 
    } 

    public void RD_Database_InfoClasse(String num_classe) 
    { 
     DB db2 = new DB(getActivity()); 
     String st_t1 = "t1"; 
     LinearLayout layout_fiche = (LinearLayout) getView().findViewById(R.id.indic); 
     LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

     LinearLayout layout_text_nom_classe = (LinearLayout) getView().findViewById(R.id.title); 
     LayoutParams lparams2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

     SQLiteDatabase qdb2 = db2.getReadableDatabase(); 
     Cursor recordset2 = qdb2.rawQuery("SELECT * FROM ID_DANGER_CLASSE_" + num_classe + " ;",null); 

     int a=recordset2.getCount(); 
     if (a != 0) 
     { 
      if (recordset2 != null) 
      { 
       if (recordset2.moveToFirst()) 
       { 
        do 
        { 
         String type_t = recordset2.getString(recordset2.getColumnIndex("type_t")); 
         String description = recordset2.getString(recordset2.getColumnIndex("description")); 
         try 
         { 
          final String s = new String(description.getBytes(), "UTF-8"); 
         } 
         catch (UnsupportedEncodingException e) 
         { 
          Log.e("utf8", "conversion", e); 
         } 
         TextView tv=new TextView(getActivity()); 
         tv.setLayoutParams(lparams); 
         if (type_t.equals("t1"))   // 
         { 
          TextView tv_nom_classe=new TextView(getActivity()); 
          tv_nom_classe.setLayoutParams(lparams2); 

          lparams2.gravity = Gravity.CENTER_HORIZONTAL; 
          tv_nom_classe.setLayoutParams(lparams2); 

//       tv_nom_classe.setGravity(Gravity.CENTER_HORIZONTAL); 
          layout_text_nom_classe.addView(tv_nom_classe); 
          tv_nom_classe.setText(description); 
          tv_nom_classe.setTypeface(null, Typeface.BOLD); 
         } 
         else 
         { 
          tv.setText(description); 
          if (type_t.equals("t2")) 
          { 
    //       tv.setTypeface(null, Typeface.BOLD); 
    //       tv.setBackgroundColor(Color.parseColor("#DBE5F1")); 
    //       tv.setTextColor(Color.parseColor("#000000"));     
          } 
          else if (type_t.equals("t3")) 
          { 

          } 
          else if (type_t.equals("p")) 
          { 
     //       description="*" + description ; 
           tv.setTextSize(13); 
           tv.setPadding(50,0,0,0); 
          } 
          else if (type_t.equals("i")) 
          { 
           tv.setTypeface(null, Typeface.ITALIC); 
           tv.setTextSize(13); 
           tv.setPadding(50,0,0,0); 
          } 
         } 
         layout_fiche.addView(tv); 
        }while (recordset2.moveToNext()); 
        } 
       recordset2.moveToFirst();  
      } 
     } 
     else 
     { 
//   textView_code_danger.setText("Rien"); 
     } 
     while(!recordset2.isAfterLast()) 
     { 
//   textView_code_danger.setText(recordset2.getString(0)); 
      recordset2.moveToNext(); 
     } 
     db2.close();  ///?? 
    } 
} 
+0

發表您的logcat錯誤 – hardartcore 2013-04-07 10:01:13

回答

1

一個解決方案(如果我沒有理解好):

public class Base_A_Page_Classe extends Fragment { 
    public void RD_Database_InfoClasse(String num_classe) { 
     // ... 
    } 
} 

public class A_Page_Classe9 extends Base_A_Page_Classe { 
    // inherits RD_Database_InfoClasse(String) 
} 

注:有關於命名你不遵循一些通常的慣例(方法的名稱以小寫字母開頭,名字中沒有_

+0

謝謝它的工作完美。我會重命名所有的方法;) – morbak 2013-04-07 10:38:44