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();        ///??
    }  
}
4

1 回答 1

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

注意:有一些您不遵循的命名惯例(方法名称以小写字母开头,_名称中没有)

于 2013-04-07T09:59:18.390 回答