2016-11-30 115 views
-3

我試圖在fragmnet中使用帶有自定義適配器(baseAdapter)的ListView。在片段中設置ListView適配器

當我直接在MainActivity中使用這段代碼時,一切正常,但是當我在片段中使用它時,它並沒有崩潰,但它沒有顯示任何內容,它只是一個空白片段。此外,當我試圖使用簡單的arrayAdapter綁定一個textView片段它工作正常,所以問題將在我的自定義適配器,我認爲。

爲什麼不顯示ListView?

我的代碼:Fragment1.java

private SQLiteDatabase dataBaseall; 
    HashMap<String, String> ChatqueryValues; 
    HashMap<String, String> ChatqueryValuesB; 

    ChatDbHelper Chatcontroller = new ChatDbHelper(getActivity()); 
    ChatAllDbHelper controller_all = new ChatAllDbHelper(getActivity()); 

    private AlertDialog.Builder build; 

    Cursor cursor; 
    Contact_Activity_Adapter ListAdapter; 
    private ArrayList<String> conId = new ArrayList<String>(); 
    private ArrayList<String> con_fullname = new ArrayList<String>(); 
    private ArrayList<String> con_number = new ArrayList<String>(); 
    private ArrayList<String> con_username = new ArrayList<String>(); 
    private ArrayList<String> con_userid = new ArrayList<String>(); 
    private ArrayList<String> con_pic = new ArrayList<String>(); 
    private ArrayList<String> con_ischat = new ArrayList<String>(); 

    ListView LISTVIEW; 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
          ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.frag1,container,false); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     // ListView listView = (ListView) getActivity().findViewById(R.id.List); 

     LISTVIEW = (ListView)getActivity(). findViewById(R.id.List); 
     SQLITEHELPER = new ContactDB(getActivity()); 
     mHelperall = new ChatAllDbHelper(getActivity()); 

    } 

    @Override 
    public void onResume() { 

     ShowSQLiteDBdata(); 

     super.onResume(); 
    } 


    private void ShowSQLiteDBdata() { 

     SQLITEDATABASE = SQLITEHELPER.getWritableDatabase(); 

     cursor = SQLITEDATABASE.rawQuery("SELECT * FROM contact ", null); 

     conId.clear(); 
     con_fullname.clear(); 
     con_number.clear(); 
     con_username.clear(); 
     con_userid.clear(); 
     con_pic.clear(); 
     con_ischat.clear(); 


     if (cursor.moveToFirst()) { 
      do { 
       conId.add(cursor.getString(cursor.getColumnIndex(ContactDB.KEY_ID))); 
       con_fullname.add(cursor.getString(cursor.getColumnIndex(ContactDB.KEY_CON_FULLNAME))); 
       con_number.add(cursor.getString(cursor.getColumnIndex(ContactDB.KEY_CON_NUMBER))); 
       con_username.add(cursor.getString(cursor.getColumnIndex(ContactDB.KEY_CON_USERNAME))); 
       con_userid.add(cursor.getString(cursor.getColumnIndex(ContactDB.KEY_CON_USERID))); 
       con_pic.add(cursor.getString(cursor.getColumnIndex(ContactDB.KEY_CON_PIC))); 
       con_ischat.add(cursor.getString(cursor.getColumnIndex(ContactDB.KEY_CON_ISCHAT))); 

      } while (cursor.moveToNext()); 
     } 

     ListAdapter = new Contact_Activity_Adapter(getActivity(), 

       conId, 
       con_fullname, 
       con_number, 
       con_username, 
       con_userid, 
       con_pic, 
       con_ischat 

     ); 

     Collections.sort(con_fullname, String.CASE_INSENSITIVE_ORDER); 

     LISTVIEW.setAdapter(ListAdapter); 
     ListAdapter.notifyDataSetChanged(); 

     cursor.close(); 
     // Collections.sort(con_fullname); 
    } 




} 
+1

'LISTVIEW =(ListView)getActivity()。 findViewById(R.id.List);'請不要使用這個......在這個問題上有bazillions的回答:如何在片段中正確地獲取視圖? ...做一些研究 – Selvin

+0

1)不要大寫鎖定你的變量名稱。 2)強烈考慮使用Java類來存儲一個'Contact'並在7個單獨的列表中使用'List '......這真是糟糕的設計https://guides.codepath.com/android/Using-an-ArrayAdapter- with-ListView#using-a-custom-arrayadapter –

回答

0
LISTVIEW = (ListView)getActivity(). findViewById(R.id.List); 

這是不對的。你試圖鏈接到你的活動列表,但你的列表是分段的。您需要從onCreateView中的片段佈局中獲取它,如下所示:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.frag1,container,false); 
    LISTVIEW = (ListView) view.findViewById(R.id.List); 
    return view; 
} 

更多信息here。這裏有很多類似的問題。

+0

無法正常工作,請詳細說明如何實現它。 – kengodmans

+0

我不會。從一些基本的教程或樣本開始,如果你不知道如何做到這一點。順便說一句,它看起來像這不是你的代碼中唯一的錯誤。 – lewkka