0

lv_gardu_pengukuran.setAdapter(adapter_pengukuran);Hi I want to show database from a table by select columns and put the data into 2 different listView, no problem getting the data by select columns, but only 1 listView showing the data, the other listView showing blank. No error showed. I've tried to switch the adapter / listView to the works one, and no problem in getting the table. So how can I see where the problem is, sorry if it shows a lot of code, just want to make it clear. Please help me, Thank you.

Here is my fragment showing the 2 listView

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Context context = getActivity();

    View v = inflater.inflate(R.layout.fragment_inspection_ss_copy, container, false);

    databaseHandler = new DatabaseHandler(getActivity());

     lv_gardu_awal = (ListView) v.findViewById(R.id.listviewgarduawal);
    listItem_awal = databaseHandler.getInspectionSSItemsDetails();
    adapter_awal = new ItemListAdapterInspectionSS(getActivity(), listItem_awal);
    lv_gardu_awal.setAdapter(adapter_awal);


    lv_gardu_pengukuran = (ListView) v.findViewById(R.id.listviewgardupengukuran);
    listItem_pengukuran = databaseHandler.getInspectionSS4ItemsDetails();
    adapter_pengukuran = new ItemListAdapterInspectionSS4(getActivity(), listItem_pengukuran);
    lv_gardu_pengukuran.setAdapter(adapter_pengukuran);


    return v;
}
@Override
public void onResume() {
    super.onResume();

    listItem_awal.clear();
    listItem_awal.addAll(databaseHandler.getInspectionSSItemsDetails());
    adapter_awal.notifyDataSetChanged();

    listItem_pengukuran.clear();
    listItem_pengukuran.addAll(databaseHandler.getInspectionSS4ItemsDetails());
    adapter_pengukuran.notifyDataSetChanged();
}

@Override
public void onPause() {
    super.onPause();

    listItem_awal.clear();
    listItem_awal.addAll(databaseHandler.getInspectionSSItemsDetails());
    adapter_awal.notifyDataSetChanged();

    listItem_pengukuran.clear();
    listItem_pengukuran.addAll(databaseHandler.getInspectionSS4ItemsDetails());
    adapter_pengukuran.notifyDataSetChanged();
}

Here is the adapter that now showing the database

public class ItemListAdapterInspectionSS4 extends BaseAdapter {

private List<ItemsDetails> list;
private LayoutInflater inflater;
private DatabaseHandler handler;
private ItemsDetails itemsDetails;
private TextView COL_ID;
private TextView COL_LWBP_MEASUREMENT_PHASE_R_MAIN ;
private TextView COL_LWBP_MEASUREMENT_PHASE_S_MAIN ;
private TextView COL_LWBP_MEASUREMENT_PHASE_T_MAIN ;
private TextView COL_LWBP_MEASUREMENT_PHASE_N_MAIN ;

public ItemListAdapterInspectionSS4(Activity activity, List<ItemsDetails> list) {
    this.list = list;
    //this.ACTION = action;
    this.activity = activity;
    this.inflater = LayoutInflater.from(activity);
    handler = new DatabaseHandler(activity);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    //return list.get(position);
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
    //return position;
}

@Override
public View getView(int position, View view, ViewGroup root) {
    if (view == null) {
        view = inflater.inflate(R.layout.listview_item_inspection_ss_4, root, false);
    }
    LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.main_layout);

    COL_ID = (TextView) view.findViewById(R.id.COL_ID);
    COL_LWBP_MEASUREMENT_PHASE_R_MAIN =  (TextView) view.findViewById(R.id.COL_LWBP_MEASUREMENT_PHASE_R_MAIN);
    COL_LWBP_MEASUREMENT_PHASE_S_MAIN =  (TextView) view.findViewById(R.id.COL_LWBP_MEASUREMENT_PHASE_S_MAIN);
    COL_LWBP_MEASUREMENT_PHASE_T_MAIN =  (TextView) view.findViewById(R.id. COL_LWBP_MEASUREMENT_PHASE_T_MAIN);
    COL_LWBP_MEASUREMENT_PHASE_N_MAIN =  (TextView) view.findViewById(R.id.COL_LWBP_MEASUREMENT_PHASE_N_MAIN);

    COL_ID.setText("" + (position + 1));

    COL_LWBP_MEASUREMENT_PHASE_R_MAIN.setText("" + list.get(position).getfasaRUtamaLWBP());
    COL_LWBP_MEASUREMENT_PHASE_S_MAIN.setText("" + list.get(position).getfasaSUtamaLWBP());
    COL_LWBP_MEASUREMENT_PHASE_T_MAIN.setText("" + list.get(position).getfasaTUtamaLWBP());
    COL_LWBP_MEASUREMENT_PHASE_N_MAIN.setText("" + list.get(position).getfasaNUtamaLWBP());

    return view;
}

}

This adapter works fine

@Override
public View getView(int position, View view, ViewGroup root) {
    if (view == null) {
        view = inflater.inflate(R.layout.listview_item_inspection_ss, root, false);
    }
    LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.main_layout);

    COL_ID = (TextView) view.findViewById(R.id.COL_ID);

    COL_LOCATION_ID = (TextView) view.findViewById(R.id.COL_LOCATION_ID);
    COL_SECTION_ID = (TextView) view.findViewById(R.id.COL_SECTION_ID);
    COL_INSPECTION_DATE = (TextView) view.findViewById(R.id.COL_INSPECTION_DATE);
    COL_INSPECTION_TYPE_ID = (TextView) view.findViewById(R.id.COL_INSPECTION_TYPE_ID);

    COL_ID.setText("" + (position + 1));
    COL_LOCATION_ID.setText("" + handler.getCategoryFromID("" + list.get(position).getunitID()).getName());
    COL_SECTION_ID.setText("" + handler.getGarduFromID("" + list.get(position).getgarduID()).getName());
    COL_INSPECTION_DATE.setText("" + list.get(position).gettanggalInspeksi());
    COL_INSPECTION_TYPE_ID.setText("" + handler.getTipeInspeksiFromID("" + list.get(position).gettipeInspeksiID()).getName());

    return view;
}

Here is my database

String CREATE_INSPECTION_SS_TABLES = " CREATE TABLE IF NOT EXISTS " + INSPECTIONS_SS_TABLE_NAME
            + "(" + COL_ID + " INTEGER PRIMARY KEY, " +

            COL_LOCATION_ID + " INTEGER, " +
            COL_SECTION_ID + " INTEGER, " +
            COL_INSPECTION_DATE + " TEXT, " +
            COL_INSPECTION_TYPE_ID + " INTEGER, " +

            COL_LWBP_MEASUREMENT_PHASE_R_MAIN + " INTEGER, " +
            COL_LWBP_MEASUREMENT_PHASE_S_MAIN + " INTEGER, " +
            COL_LWBP_MEASUREMENT_PHASE_T_MAIN + " INTEGER, " +
            COL_LWBP_MEASUREMENT_PHASE_N_MAIN + " INTEGER " +

            ")";

Here is how I get the columns

 public List<ItemsDetails> getInspectionSSItemsDetails() {
    List<ItemsDetails> detailsList = new ArrayList<ItemsDetails>();
    String selectQuery = "SELECT _id, location_id, section_id, inspection_date, inspection_type_id  FROM " + INSPECTIONS_SS_TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            try {
                ItemsDetails details = new ItemsDetails();

                details.setId(cursor.getInt(0));
                details.setunitID(cursor.getInt(1));
                details.setgarduID(cursor.getInt(2));
                details.settanggalInspeksi(cursor.getString(3));
                details.settipeInspeksiID(cursor.getInt(4));

                detailsList.add(details);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return detailsList;
}

public List<ItemsDetails> getInspectionSS4ItemsDetails() {
    List<ItemsDetails> detailsList = new ArrayList<ItemsDetails>();
    String selectQuery = "SELECT _id, lwbp_measurement_phase_r_main, lwbp_measurement_phase_s_main, lwbp_measurement_phase_t_main, lwbp_measurement_phase_n_main FROM " + INSPECTIONS_SS_TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            try {
                ItemsDetails details = new ItemsDetails();

                details.setId(cursor.getInt(cursor.getColumnIndex("_id")));
details.setfasaRUtamaLWBP(cursor.getString(cursor.getColumnIndex("lwbp_measurement_phase_r_main")));
                details.setfasaSUtamaLWBP(cursor.getString(cursor.getColumnIndex("lwbp_measurement_phase_s_main")));
                details.setfasaTUtamaLWBP(cursor.getString(cursor.getColumnIndex("lwbp_measurement_phase_t_main")));
                details.setfasaNUtamaLWBP(cursor.getString(cursor.getColumnIndex("lwbp_measurement_phase_n_main")));

                detailsList.add(details);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return detailsList;
}

Here is my layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<LinearLayout
    android:id="@+id/layout_tab_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="4"
    android:padding="8dp">


    <LinearLayout
        android:id="@+id/layout_sub_parent_1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_weight="1"
        android:orientation="horizontal">


        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="40dp"
                            android:layout_height="wrap_content"
                            android:background="@drawable/cell_shape"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="@string/title_number"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="200dp"
                            android:layout_height="wrap_content"
                            android:background="@drawable/cell_shape"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="@string/COL_LOCATION_ID"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="200dp"
                            android:layout_height="wrap_content"
                            android:background="@drawable/cell_shape"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="@string/COL_SECTION_ID"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="200dp"
                            android:layout_height="wrap_content"
                            android:background="@drawable/cell_shape"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="@string/COL_INSPECTION_DATE"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="200dp"
                            android:layout_height="wrap_content"
                            android:background="@drawable/cell_shape"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="@string/COL_INSPECTION_TYPE_ID"
                            android:textStyle="bold" />


                    </LinearLayout>


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ListView
                        android:id="@+id/listviewgarduawal"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />

                </LinearLayout>

            </LinearLayout>

        </HorizontalScrollView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_sub_parent_4"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_weight="1"
        android:orientation="horizontal">


        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="40dp"
                            android:layout_height="wrap_content"
                            android:background="@drawable/cell_shape"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="@string/title_number"
                            android:textStyle="bold" />


                        <TextView
                            android:layout_width="300dp"
                            android:layout_height="wrap_content"
                            android:background="@drawable/cell_shape"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="@string/COL_LWBP_MEASUREMENT_PHASE_R_MAIN"
                            android:textStyle="bold" />



                        <TextView
                            android:layout_width="300dp"
                            android:layout_height="wrap_content"
                            android:background="@drawable/cell_shape"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="@string/COL_LWBP_MEASUREMENT_PHASE_S_MAIN"
                            android:textStyle="bold" />



                        <TextView
                            android:layout_width="300dp"
                            android:layout_height="wrap_content"
                            android:background="@drawable/cell_shape"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="@string/COL_LWBP_MEASUREMENT_PHASE_T_MAIN"
                            android:textStyle="bold" />



                        <TextView
                            android:layout_width="300dp"
                            android:layout_height="wrap_content"
                            android:background="@drawable/cell_shape"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="@string/COL_LWBP_MEASUREMENT_PHASE_N_MAIN"
                            android:textStyle="bold" />




                    </LinearLayout>


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ListView
                        android:id="@+id/listviewgardupengukuran"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />

                </LinearLayout>

            </LinearLayout>


        </HorizontalScrollView>


    </LinearLayout>

</LinearLayout>


<!-- end here -->
</ScrollView>

My List Item Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"

android:weightSum="2">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/COL_ID"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="5dp"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/COL_LWBP_MEASUREMENT_PHASE_R_MAIN"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="5dp"
        android:textStyle="bold" />



    <TextView
        android:id="@+id/COL_LWBP_MEASUREMENT_PHASE_S_MAIN"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="5dp"
        android:textStyle="bold" />



    <TextView
        android:id="@+id/COL_LWBP_MEASUREMENT_PHASE_T_MAIN"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="5dp"
        android:textStyle="bold" />



    <TextView
        android:id="@+id/COL_LWBP_MEASUREMENT_PHASE_N_MAIN"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@drawable/cell_shape"
        android:gravity="center"
        android:padding="5dp"
        android:textStyle="bold" />


</LinearLayout>

Charlie Brown
  • 65
  • 1
  • 12

2 Answers2

0

as far as I could see there's definitely an error on the listItem you're using the same object every where and clearing and adding other elements.

you should create separate for each ListView. For example:

lv_gardu_awal = (ListView) v.findViewById(R.id.listviewgarduawal);
listItem_awal = databaseHandler.getInspectionSSItemsDetails();
adapter_awal = new ItemListAdapterInspectionSS(getActivity(), listItem_awal);
lv_gardu_awal.setAdapter(adapter_awal);


lv_gardu_pengukuran = (ListView) v.findViewById(R.id.listviewgardupengukuran);
listItem_pengukuran = databaseHandler.getInspectionSS4ItemsDetails();
adapter_pengukuran = new ItemListAdapterInspectionSS4(getActivity(), listItem_pengukuran);
lv_gardu_pengukuran.setAdapter(adapter_pengukuran);

there're other stuff that on your code that I would certainly do different, but that should be enough to fix your error.

But some stuff to have in mind, if you want a better code

  • RecyclerView is infinitely better than ListView
  • Your adapters are not using the Holder pattern
  • Your adapter is a very weird mix of database/cursor and ArrayList. Pick one, Pick ArrayAdapter.
  • I'm sure all those calls to handler.get___something is making everything slower.
Budius
  • 39,391
  • 16
  • 102
  • 144
  • Hi Budius, thank you for helping me. I've tried to use two separate object listitem before. But lv_gardu_pengukuran.setAdapter(adapter_pengukuran); is still showing blank. Any other suggestions, thank you – Charlie Brown Mar 10 '16 at 16:55
  • I've edited my code, and put it separate, no crash or errors, but still showing blank :( – Charlie Brown Mar 10 '16 at 16:58
  • my suggestion is to double check your layout (isn't for some reason one of the lists invisible?). also check what are the sizes of those lists (maybe it's blank because the list is empty. – Budius Mar 10 '16 at 17:00
  • I have put the layout, and as I mentioned, I've tried to switch the listView, and it showed data. So the table is not empty. But I'm not using emulator, just direct to my android phone actually.. – Charlie Brown Mar 10 '16 at 17:10
  • the layout is very big, it separate into 4 layout listView, but now to find the problem I only put it two layout ListView – Charlie Brown Mar 10 '16 at 17:12
0

You can't put two listviews on the same page without some tricks. Each one will want to take over the screen and all the scroll events. You may want to create a custom adapter that combines the data into one listview or puts two dynamic linear layouts inside a scrollview.

Cory Roy
  • 5,379
  • 2
  • 28
  • 47
  • I see, thank you. Cory Roy. I will try your suggestions, as I need to separate each ListView, so If I'm not wrong I must make two or more dynamic linear layouts inside a scrollview? Please correct me. If it is okay, could you help me by giving me example or links for how to do that? sorry bothering you .. – Charlie Brown Mar 10 '16 at 17:58
  • I'm learning this question http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing/3495908#3495908. Is this what it should be? – Charlie Brown Mar 10 '16 at 18:00
  • That answer will give you some tips, but notice the comment from Romain Guy. He is one of the main android people at google. You may consider using linear layouts rather than follow that particular answer. But either way you are on the right track now. – Cory Roy Mar 10 '16 at 18:23
  • 1
    Hi thank you Cory Roy, sorry just reply you, sorry it was 3 AM. It works now. I simply remove the ScrollView :) I understand why it is not working. – Charlie Brown Mar 11 '16 at 00:28