从表中读取列到2个不同的列表视图中只有1个listview工作

时间:2016-03-10 16:25:34

标签: android sqlite listview

lv_gardu_pengukuran.setAdapter(adapter_pengukuran);您好我想通过选择列从表中显示数据库并将数据放入2个不同的listView,没有问题通过选择列获取数据,但只有1个listView显示数据,另一个listView显示空白。没有错误显示。我已经尝试将适配器/ listView切换到工作状态,并且在获取表格时没有问题。那么我怎么能看到问题出在哪里,对不起,如果它显示了很多代码,只是想说清楚。请帮帮我,谢谢。

这是我的片段,显示了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();
}

这是现在显示数据库的适配器

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

}

此适配器工作正常

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

这是我的数据库

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 " +

            ")";

以下是我获取列的方法

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

这是我的布局

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

我的列表项目布局

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

2 个答案:

答案 0 :(得分:0)

据我所知,listItem肯定会在每个地方使用相同的对象并清除和添加其他元素时出现错误。

您应该为每个ListView创建单独的。例如:

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

在你的代码上还有其他的东西,我肯定会做的不同,但这应该足以解决你的错误。

但是要记住一些事情,如果你想要更好的代码

  • RecyclerView比ListView
  • 无限好
  • 您的适配器未使用Holder模式
  • 您的适配器是数据库/游标和ArrayList的一种非常奇怪的混合。选择一个,Pick ArrayAdapter。
  • 我确信所有对handler.get___something的调用都会使一切变慢。

答案 1 :(得分:0)

如果没有一些技巧,您无法在同一页面上放置两个列表视图。每个人都想接管屏幕和所有滚动事件。您可能想要创建一个自定义适配器,将数据合并到一个列表视图中,或者在滚动视图中放置两个动态线性布局。