将多维字符串数组转换为游标

时间:2015-03-09 06:40:30

标签: android arrays database cursor

我有一个旧的实现,它使用数据库中的条目并使用游标对象来填充列表视图。但是在新实现中我有一个多维字符串数组。有12列和可变行数

String[][] detailsarray;  

我需要将此字符串数组转换为游标对象,以便我可以重用旧代码。

用于打开带有列表适配器的新片段

private void callProductListFragment(Cursor cursor) {
    productListFragment = new ProductListFragment(cursor);
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.addToBackStack("list");
    ft.add(R.id.fl_container, productListFragment, "FragList").commit();
}

--- ProductListFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     View view =  inflater.inflate(R.layout.product_list_fragment,container, false);
       product_list = (ListView) view.findViewById(R.id.product_list); 
        ProductAdapter productAdapter = new ProductAdapter(getActivity(), cursor, false);
        product_list.setAdapter(productAdapter);
        product_list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Cursor cursor = ((CursorAdapter) parent.getAdapter()).getCursor();
                cursor.moveToPosition(position);
                ProductObject productObject = new ProductObject(String.valueOf(cursor.getInt(cursor.getColumnIndex(ProductsData._ID))),cursor.getString(cursor.getColumnIndex(ProductsData.PRODUCT_ID)),cursor.getString(cursor.getColumnIndex(ProductsData.PRODUCT_TITLE)),cursor.getString(cursor.getColumnIndex(ProductsData.PRODUCT_DEALER_NAME)), cursor.getString(cursor.getColumnIndex(ProductsData.PRODUCT_DEALER_DESC)), cursor.getString(cursor.getColumnIndex(ProductsData.PRODUCT_DEALER_LOCATION)), cursor.getString(cursor.getColumnIndex(ProductsData.PRODUCT_DEALER_ADDRESS)), cursor.getString(cursor.getColumnIndex(ProductsData.PRODUCT_DEALER_TYPE)), cursor.getString(cursor.getColumnIndex(ProductsData.PRODUCT_DEALER_NO)), cursor.getString(cursor.getColumnIndex(ProductsData.PRODUCT_IMAGE)));
                productDeatailsFragment = new ProductDeatailsFragment(productObject);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.addToBackStack("details");
                ft.add(R.id.fl_container, productDeatailsFragment, "FragList").commit();
            }
        });
        return view;
}

我该怎么办......请提供建议。

1 个答案:

答案 0 :(得分:1)

尝试MatrixCursor我发现了一个类似的问题 Create a cursor from hardcoded array instead of DB

MatrixCursor继承自Cursor

所以你可以使用

String[] columns = new String[] { "_id", "item", "description" };
Cursor cursor= new MatrixCursor(columns);