-3

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




}
Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
  • 1
    `LISTVIEW = (ListView)getActivity(). findViewById(R.id.List);` please, do not use this ... There are bazillions answer on the question: How to properly obtain view in Fragment? ... do some research – Selvin Nov 30 '16 at 14:52
  • 1) do not caps lock your variable names. 2) strongly consider using a Java class to store one `Contact` and use a `List` over 7 separate lists... That's really poor design https://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView#using-a-custom-arrayadapter – OneCricketeer Nov 30 '16 at 15:01

1 Answers1

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

That's wrong. You trying to get link to your activity list, but your list is in fragment. You need to get it from fragment layout in onCreateView, like this:

@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;
}

More info here. There are many similar questions here.

Community
  • 1
  • 1
lewkka
  • 1,019
  • 15
  • 25
  • not working, please give more information on how to implement it. – kengodmans Nov 30 '16 at 16:57
  • i won't. Start with some basic tutorials or samples, if you don't know how to do this. btw, it's look like this is not the only mistake you have in your code. – lewkka Nov 30 '16 at 18:28