根据Column值在Android活动的ListView中填充SQLite中的不同值

时间:2017-07-02 03:49:01

标签: android listview android-sqlite simplecursoradapter

我必须显示一个ListView,每个视图都有不同的值取决于Type。所有这些值都需要从单个DB表中填充。如果我在表中有10列,那么在ListView中,某些行将包含来自少数列的值以及来自某些不同列的其他行,这取决于同一表的“类型”列的值。有些数据很常见。视图将混合所有这些不同的行。

我编写了SimpleCursorAdapter来获取所有列值,并且在setViewBinder()中,根据Type,我为不同的TextView设置值。但不知何故,在展示中,它是空白的。在调试器中,我可以看到填充和设置正确的值。

有什么方法可以解决这个问题?或任何其他方法来实现相同的目标?

请注意,只有在运行时,在查询数据库后,我才能获得该特定ROW的Type值。

以下是代码。

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    dbHelper = new BilloDatabaseHelper(getActivity());
    db = dbHelper.getReadableDatabase();

    Bundle args = getArguments();
    long billId = args.getLong(IntentConstant.SOMEID.name());

    ListView listView = (ListView) getActivity().findViewById(R.id.itemListView);

    cursor = db.query("ITEM_DETAILS",
            new String[]{"_id", "COLUMN1", "TYPE", "COLUMN2", "COLUMN3", "COLUMN4", "COLUMN5", "COLUMN6", "COLUMN7", "COLUMN8", "COLUMN9", "COLUMN10", "COLUMN11", "COLUMN12"},
            "SOME_ID = ?",           //Where clause
            new String[]{Long.toString(billId)},
            null,           //Group by
            null,
            null
    );

    SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(getActivity(),
            R.layout.item_list_view,
            cursor,
            new String[]{"COLUMN1", "TYPE", "COLUMN2", "COLUMN3", "COLUMN4", "COLUMN5", "COLUMN6", "COLUMN7", "COLUMN8", "COLUMN9", "COLUMN10", "COLUMN11", "COLUMN12"},
            new int[]{R.id.COLUMN1, R.id.TYPE, R.id.COLUMN2, R.id.COLUMN3, R.id.COLUMN4, R.id.COLUMN5, R.id.COLUMN6, R.id.COLUMN7, R.id.COLUMN8, R.id.COLUMN9, R.id.COLUMN10, R.id.COLUMN9, R.id.COLUMN10},
            0);

    final int[] TYPE = {0};

    listAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

            if(columnIndex == 2){
                TYPE[0] = cursor.getInt(cursor.getColumnIndex("TYPE"));

            }else if(columnIndex == 4){
               if(TYPE[0] == 1) {
                    String val = cursor.getString(cursor.getColumnIndex("COLUMN3"));
                    TextView COLUMN3 = (TextView) view.findViewById(R.id.COLUMN3);
                    COLUMN3.setText(val);

                   return true;
                }

            }else if(columnIndex == 9){
                if(TYPE[0] == 1) {
                    long date = cursor.getLong(cursor.getColumnIndex("COLUMN8"));

                    TextView dayText = (TextView) view.findViewById(R.id.COLUMN8);
                    dayText.setText(DateUtility.getDDMMYYYYDate(date));

                    return true;
                }


            }else if(columnIndex == 10){
                if(TYPE[0] == 1) {
                    long date = cursor.getLong(cursor.getColumnIndex("COLUMN9"));

                    TextView dayText = (TextView) view.findViewById(R.id.COLUMN9);
                    dayText.setText(DateUtility.getDDMMYYYYDate(date));

                    return true;
                }
            }else if(columnIndex == 11){
                if(TYPE[0] == 2) {
                    String sizeVal = cursor.getString(cursor.getColumnIndex("COLUMN10"));

                    TextView size = (TextView) view.findViewById(R.id.COLUMN9);
                    size.setText(sizeVal);

                    return true;
                }

            }else if(columnIndex == 12){
                if(TYPE[0] == 2) {
                    String fitVal = cursor.getString(cursor.getColumnIndex("COLUMN11"));

                    TextView fit = (TextView) view.findViewById(R.id.COLUMN10);
                    fit.setText(fitVal);

                    return true;
                }
            }
            return false;
        }
    });

    listView.setAdapter(listAdapter);

}

对于列的值COLUMN9和COLUMN10,R.id.COLUMN9和R.id.COLUMN10的值不可见。即使对于类型1,它们也设置为空白。对于类型2,列COLUMN11和COLUMN12,它们在屏幕上可见。

1 个答案:

答案 0 :(得分:0)

这是解决方案的解决方案。我将游标数据转换为List of JSONObject并传递JSONObject列表新的ListAdapter,基于Type,我可以轻松修改和更新TextView值。