Im trying to use ListView with custom adapter (baseAdapter) in fragmnet.
When I us this code directly inside the MainActivity everything works fine, but when I use this in fragment it did't crash but it din't show anything, it is just a blank fragment. Also when I've tried to use simple arrayAdapter to bind one textView in fragment it worked fine so the problem will be inside my custom adapter I think.
Why is it not displaying the ListView?
My code: 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);
}
}