【问题标题】:Read columns from table into 2 different list view only 1 listview working将表中的列读取到 2 个不同的列表视图中,只有 1 个列表视图工作
【发布时间】:2016-03-11 01:39:43
【问题描述】:

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>

【问题讨论】:

    标签: android sqlite listview


    【解决方案1】:

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

    【讨论】:

    • 我明白了,谢谢。科里·罗伊。我会尝试你的建议,因为我需要分隔每个 ListView,所以如果我没有错,我必须在滚动视图中制作两个或更多动态线性布局?请纠正我。如果可以,您能否通过示例或链接来帮助我如何做到这一点?抱歉打扰你了..
    • 我正在学习这个问题stackoverflow.com/questions/3495890/…。这是应该的吗?
    • 该答案会给您一些提示,但请注意 Romain Guy 的评论。他是谷歌的主要安卓人之一。您可以考虑使用线性布局,而不是遵循特定的答案。但无论哪种方式,你现在都在正确的轨道上。
    • 您好,谢谢 Cory Roy,抱歉刚刚回复您,抱歉现在是凌晨 3 点。现在可以了。我只是删除了 ScrollView :) 我明白它为什么不起作用。
    【解决方案2】:

    据我所知,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 组合。选择一个,选择 ArrayAdapter。
    • 我敢肯定,所有对 handler.get___something 的调用都会让一切变得更慢。

    【讨论】:

    • 嗨,Budius,感谢您帮助我。我之前尝试过使用两个单独的对象列表项。但是 lv_gardu_pengukuran.setAdapter(adapter_pengukuran);仍然显示空白。有其他建议,谢谢
    • 我已经编辑了我的代码,并把它分开,没有崩溃或错误,但仍然显示空白:(
    • 我的建议是仔细检查您的布局(由于某种原因,其中一个列表不是不可见的吗?)。还要检查这些列表的大小(也许它是空白的,因为列表是空的。
    • 我已经放了布局,正如我提到的,我试图切换listView,它显示了数据。所以表不是空的。但我没有使用模拟器,实际上只是直接连接到我的安卓手机..
    • 布局很大,分成4个布局的listView,现在发现问题我只放了两个布局的ListView
    猜你喜欢
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 2013-09-18
    • 1970-01-01
    相关资源
    最近更新 更多