Android游标带有一个int数组行,我该如何获取?

时间:2013-10-27 16:43:27

标签: android cursor android-contentresolver

我试图通过使用游标和提供程序来获得在android中工作的简单视图。 提供者工作得很好,但我不知道如何在光标中连续获取一个int数组。

这是我的提供者:

public class RecipeProvider extends ContentProvider {
    public static final String COL_RECID = "_ID";
    public static final String COL_RECCATID = "recipeCategory";
    public static final String COL_RECIMGID = "recipeImageID";
    public static final String COL_RECDESC = "recipeDescription";
    public static final String COL_RECLOC = "recipeLocations";

    public static final String[] columnNames = {COL_RECID, COL_RECCATID, COL_RECIMGID, COL_RECDESC, COL_RECLOC };

    public static final String AUTHORITY = "be.pxl.minecraftguide.providers.recipeprovider";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/*");
    private List<Recipe> recipesList;

    @Override
    public boolean onCreate() {
        recipesList = new ArrayList<Recipe>();
        recipesList.add(new Recipe(1, 2, R.drawable.diamond_boots, "Boots (Diamond)",
                new int[] { 0,0,0, 1,0,1, 1,0,1 }, new int[] { R.drawable.diamond_ingot }));
        recipesList.add(new Recipe(2, 2, R.drawable.gold_boots, "Boots (Gold)",
                new int[] { 0,0,0, 1,0,1, 1,0,1 }, new int[] { R.drawable.gold_ingot }));
        recipesList.add(new Recipe(3, 2, R.drawable.iron_boots, "Boots (Iron)",
                new int[] { 0,0,0, 1,0,1, 1,0,1 }, new int[] { R.drawable.iron_ingot }));
        return true;
    }

    @Override
    public Cursor query(Uri arg0, String[] categories, String recipeID, String[] arg3,
            String arg4) {
        MatrixCursor mxCur = new MatrixCursor(columnNames);
        RowBuilder rb;

        if (categories != null && Integer.parseInt(categories[0]) != 1) {
            for (Recipe rcp : recipesList) {
                if (rcp.getRecipeCategory() == Integer.parseInt(categories[0])) {
                    rb = mxCur.newRow();
                    rb.add(rcp.getRecipeID());
                    rb.add(rcp.getRecipeCategory());
                    rb.add(rcp.getRecipeImageID());
                    rb.add(rcp.getRecipeDescription());
                    rb.add(rcp.getRecipeLocations());
                }
            }
        } else if (recipeID != null) {
            for (Recipe rcp : recipesList) {
                if (rcp.getRecipeID() == Integer.parseInt(recipeID)) {
                    rb = mxCur.newRow();
                    rb.add(rcp.getRecipeID());
                    rb.add(rcp.getRecipeCategory());
                    rb.add(rcp.getRecipeImageID());
                    rb.add(rcp.getRecipeDescription());
                    rb.add(rcp.getRecipeLocations());
                }
            }
        } else {
            for (Recipe rcp : recipesList) {
                rb = mxCur.newRow();
                rb.add(rcp.getRecipeID());
                rb.add(rcp.getRecipeCategory());
                rb.add(rcp.getRecipeImageID());
                rb.add(rcp.getRecipeDescription());
                rb.add(rcp.getRecipeLocations());
            }
        }
        if (mxCur.getCount() == 0) {
            rb = mxCur.newRow();
            rb.add(0);
            rb.add(0);
            rb.add(R.drawable.air);
            rb.add("No recipes found for this category");
            rb.add(new int[] { 0,0,0,0,0,0,0,0,0 });
        }
        return mxCur;
    }

但我的观点意图课程是我被困的地方

public class RecipeDetails extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipedetails);

        Bundle extras = getIntent().getExtras();
        String id = Integer.toString(extras.getInt("recipeID"));

        ImageView imgResult = (ImageView) findViewById(R.id.imgResult);
        int[] imgLocations = new int[9];
        ImageView imgIngr1 = (ImageView) findViewById(R.id.imgIngr1);
        ImageView imgIngr2 = (ImageView) findViewById(R.id.imgIngr2);
        ImageView imgIngr3 = (ImageView) findViewById(R.id.imgIngr3);
        ImageView imgIngr4 = (ImageView) findViewById(R.id.imgIngr4);
        ImageView imgIngr5 = (ImageView) findViewById(R.id.imgIngr5);
        ImageView imgIngr6 = (ImageView) findViewById(R.id.imgIngr6);
        ImageView imgIngr7 = (ImageView) findViewById(R.id.imgIngr7);
        ImageView imgIngr8 = (ImageView) findViewById(R.id.imgIngr8);
        ImageView imgIngr9 = (ImageView) findViewById(R.id.imgIngr9);


        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(RecipeProvider.CONTENT_URI, null, id, null, null);
        if (cursor.moveToFirst()) {
            imgResult.setImageResource(cursor.getInt(2));
            setTitle(cursor.getString(3));


            //________________HOW CAN I GET AN INT ARRAY OUT?_________________
            int[] intLocations = cursor.getType(4); //Cannot convert int to int[]
            int[] intUsedImages = cursor.getType(5); //Same here
                            imgIngr1.setImageResource(imgLocations[0]);
            imgIngr2.setImageResource(imgLocations[1]);
            imgIngr3.setImageResource(imgLocations[2]);
            imgIngr4.setImageResource(imgLocations[3]);
            imgIngr5.setImageResource(imgLocations[4]);
            imgIngr6.setImageResource(imgLocations[5]);
            imgIngr7.setImageResource(imgLocations[6]);
            imgIngr8.setImageResource(imgLocations[7]);
            imgIngr9.setImageResource(imgLocations[8]);
        }
    }
}

所以基本上eclipse告诉我它得到了一个int,但我知道它是一个int数组。 我该怎么办?

0 个答案:

没有答案