android viewflow从sdcard中滑动图像

发布于 2024-12-11 20:16:57 字数 6868 浏览 3 评论 0原文

我正在开发一个项目,将一些图像保存到 SD 卡,现在我必须创建视图流来显示这些图像。我通过 sdCard 的 Id 获取图像,现在的问题是如何在不同图像之间滑动。这是我正在使用的代码:

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class Cards extends Activity {

    public Cursor cursor;
    int position;
    int indexxx;
    Bitmap b;
    int objectId;
    int cardsId;

    private ViewFlow viewFlow;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.circle_layout); 
        UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1);
        userDbHelper.initialize(this);


        final int cardId = getIntent().getIntExtra("card_id",0);
        Log.i("Card Id ","Card Id : "+cardId);
        final int collId = getIntent().getIntExtra("collection_id",0);
        Log.i("Collection Id ","Collection Id : "+collId);

        position = getIntent().getIntExtra("position",0);
        Log.i("position","position : "+position);

        String cardSQL = "SELECT cm.objectId "+
         "FROM cardmedias AS cm "+
         "INNER JOIN cards AS cd "+
         "ON (cm.cardId = cd.objectId) "+
         "WHERE cd.collectionId="+collId;

        Cursor cards = userDbHelper.executeSQLQuery(cardSQL);
        if (cards.getCount() == 0) {
            Log.i("", "No Image file");
            cards.close();
        } else if (cards.getCount() > 0) {
            for (cards.move(0); cards.moveToNext(); cards.isAfterLast()) {
                cardsId = Integer.parseInt(cards.getString(cards
                        .getColumnIndex("objectId")));
                Log.i("", "cards objectId : " + cardsId);

                String path = Environment.getExternalStorageDirectory()
                        + "/.Stampii/MediaCard/" + cardsId + ".png";
                Log.i("", "path : " + path);
            }
        }

        String sql = "SELECT objectId FROM cardmedias WHERE cardId=" + cardId
                + " LIMIT 1";
        Cursor cursor = userDbHelper.executeSQLQuery(sql);
        if (cursor.getCount() == 0) {
            Log.i("", "No Image file");
            cursor.close();
        } else if (cursor.getCount() > 0) {
            for (cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
                objectId = Integer.parseInt(cursor.getString(cursor
                        .getColumnIndex("objectId")));
                Log.i("", "objectId : " + objectId);
            }
        }



        Button info = (Button) findViewById(R.id.info_button);
        info.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Cards.this, SingleCardInfo.class);
                intent.putExtra("card_id", cardId);
                intent.putExtra("collection_id", collId);
                startActivity(intent);
            }
        });

        Button back = (Button) findViewById(R.id.back_button);
        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        final ArrayList<Bitmap> images = new ArrayList<Bitmap>();
        String path = Environment.getExternalStorageDirectory()+"/.Stampii/MediaCard/"+objectId+".png";

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16*1024];

        Bitmap b = BitmapFactory.decodeFile(path, options);
        images.add(b);

        viewFlow = (ViewFlow) findViewById(R.id.viewflow);
        viewFlow.setAdapter(new ImageAdapter(this, images),position);


        ImageButton prevBtn = (ImageButton) findViewById(R.id.previous_button);
        prevBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                indexxx = viewFlow.getSelectedItemPosition()-1;
                if (indexxx>=0) {
                    viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx);
                    viewFlow.setSelectedItemPosition(indexxx);
                    Log.i("indexxx", "indexxx : " + indexxx);
                }
            }
        });

        ImageButton nextBtn = (ImageButton) findViewById(R.id.next_button);
        nextBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                indexxx = viewFlow.getSelectedItemPosition()+1;
                if (indexxx<=images.size()) {
                    viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx);
                    viewFlow.setSelectedItemPosition(indexxx);
                    Log.i("indexxx", "indexxx : " + indexxx);
                }
            }
        }); 

    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        viewFlow.onConfigurationChanged(newConfig);
    }

}

这是我的 ImageAdapter 类:

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private ArrayList<Bitmap> ids = new ArrayList<Bitmap>();
    private Bitmap bitmap;

    public ImageAdapter(Context context, ArrayList<Bitmap> images) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ids = images;
    }

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.image_item, null);
        }
        ((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ids.get(position));
        return convertView;
    }

}

知道如何通过从数据库获取它的 ID、找到具有该名称的图像并将其添加到视图流和滑动来设置下一个或上一个图像。我不想让别人代替我编写代码...我一直在想如何才能做到这一点...所以如果有人可以帮助我提供建议、想法或其他东西,那就拍吧。

提前致谢!

I'm working on a project which is saving some images to sdcard and now I have to create viewflow to show this images. I'm getting the images by it's Id from sdCard and my problem now is how to swipe between different images. Here is the code I'm using :

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class Cards extends Activity {

    public Cursor cursor;
    int position;
    int indexxx;
    Bitmap b;
    int objectId;
    int cardsId;

    private ViewFlow viewFlow;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.circle_layout); 
        UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1);
        userDbHelper.initialize(this);


        final int cardId = getIntent().getIntExtra("card_id",0);
        Log.i("Card Id ","Card Id : "+cardId);
        final int collId = getIntent().getIntExtra("collection_id",0);
        Log.i("Collection Id ","Collection Id : "+collId);

        position = getIntent().getIntExtra("position",0);
        Log.i("position","position : "+position);

        String cardSQL = "SELECT cm.objectId "+
         "FROM cardmedias AS cm "+
         "INNER JOIN cards AS cd "+
         "ON (cm.cardId = cd.objectId) "+
         "WHERE cd.collectionId="+collId;

        Cursor cards = userDbHelper.executeSQLQuery(cardSQL);
        if (cards.getCount() == 0) {
            Log.i("", "No Image file");
            cards.close();
        } else if (cards.getCount() > 0) {
            for (cards.move(0); cards.moveToNext(); cards.isAfterLast()) {
                cardsId = Integer.parseInt(cards.getString(cards
                        .getColumnIndex("objectId")));
                Log.i("", "cards objectId : " + cardsId);

                String path = Environment.getExternalStorageDirectory()
                        + "/.Stampii/MediaCard/" + cardsId + ".png";
                Log.i("", "path : " + path);
            }
        }

        String sql = "SELECT objectId FROM cardmedias WHERE cardId=" + cardId
                + " LIMIT 1";
        Cursor cursor = userDbHelper.executeSQLQuery(sql);
        if (cursor.getCount() == 0) {
            Log.i("", "No Image file");
            cursor.close();
        } else if (cursor.getCount() > 0) {
            for (cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
                objectId = Integer.parseInt(cursor.getString(cursor
                        .getColumnIndex("objectId")));
                Log.i("", "objectId : " + objectId);
            }
        }



        Button info = (Button) findViewById(R.id.info_button);
        info.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Cards.this, SingleCardInfo.class);
                intent.putExtra("card_id", cardId);
                intent.putExtra("collection_id", collId);
                startActivity(intent);
            }
        });

        Button back = (Button) findViewById(R.id.back_button);
        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        final ArrayList<Bitmap> images = new ArrayList<Bitmap>();
        String path = Environment.getExternalStorageDirectory()+"/.Stampii/MediaCard/"+objectId+".png";

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16*1024];

        Bitmap b = BitmapFactory.decodeFile(path, options);
        images.add(b);

        viewFlow = (ViewFlow) findViewById(R.id.viewflow);
        viewFlow.setAdapter(new ImageAdapter(this, images),position);


        ImageButton prevBtn = (ImageButton) findViewById(R.id.previous_button);
        prevBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                indexxx = viewFlow.getSelectedItemPosition()-1;
                if (indexxx>=0) {
                    viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx);
                    viewFlow.setSelectedItemPosition(indexxx);
                    Log.i("indexxx", "indexxx : " + indexxx);
                }
            }
        });

        ImageButton nextBtn = (ImageButton) findViewById(R.id.next_button);
        nextBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                indexxx = viewFlow.getSelectedItemPosition()+1;
                if (indexxx<=images.size()) {
                    viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx);
                    viewFlow.setSelectedItemPosition(indexxx);
                    Log.i("indexxx", "indexxx : " + indexxx);
                }
            }
        }); 

    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        viewFlow.onConfigurationChanged(newConfig);
    }

}

And here is my ImageAdapter class :

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private ArrayList<Bitmap> ids = new ArrayList<Bitmap>();
    private Bitmap bitmap;

    public ImageAdapter(Context context, ArrayList<Bitmap> images) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ids = images;
    }

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.image_item, null);
        }
        ((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ids.get(position));
        return convertView;
    }

}

Any idea how can I set the next or previous image by getting it's ID from database, finding the image with that name and adding it in viewflow and swipe. I don't want someone to write the code instead of me... I'm stuck on the idea how can I do this... So if someone can help me with suggestions, ideas or something else just shoot it.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界等同你 2024-12-18 20:16:57

我认为你已经很接近让它发挥作用了。

getCount() 应该是这样的:

@Override
public int getCount() {
    return ids.size();
}

编辑
您应该只创建一个 ImageAdapter 实例,您将在 onClick 内创建新实例。另外,您应该使用 setSelection() 而不是 setSelectedItemPosition()。看来后者是用于内部的:

        @Override
        public void onClick(View v) {
            indexxx = viewFlow.getSelectedItemPosition()-1;
            if (indexxx>=0) {
                viewFlow.setSelection(indexxx);
                Log.i("indexxx", "indexxx : " + indexxx);
            }
        }
        /* ... */
        @Override
        public void onClick(View v) {
            indexxx = viewFlow.getSelectedItemPosition()+1;
            if (indexxx<images.size()) { // Should be <, you have a wrong boundary check here!!!
                viewFlow.setSelection(indexxx);
                Log.i("indexxx", "indexxx : " + indexxx);
            }
        }

I think you are quite close to getting it work.

getCount() should be like this:

@Override
public int getCount() {
    return ids.size();
}

EDITED:
You should create only one instance of ImageAdapter, you are creating new instances inside onClick. Also you should use setSelection() instead of setSelectedItemPosition(). It seems The latter is intended for internal:

        @Override
        public void onClick(View v) {
            indexxx = viewFlow.getSelectedItemPosition()-1;
            if (indexxx>=0) {
                viewFlow.setSelection(indexxx);
                Log.i("indexxx", "indexxx : " + indexxx);
            }
        }
        /* ... */
        @Override
        public void onClick(View v) {
            indexxx = viewFlow.getSelectedItemPosition()+1;
            if (indexxx<images.size()) { // Should be <, you have a wrong boundary check here!!!
                viewFlow.setSelection(indexxx);
                Log.i("indexxx", "indexxx : " + indexxx);
            }
        }
黄昏下泛黄的笔记 2024-12-18 20:16:57

这是我解决这个问题的方法:

import java.util.ArrayList;
import java.util.HashMap;

import com.stampii.stampii.R;
import com.stampii.stampii.comm.rpc.UserDatabaseHelper;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class Cards extends Activity {

    public Cursor cursor;
    int indexxx;
    Bitmap b;
    int objectId;
    int cardsId;
    ArrayList<Integer> ids;
    String path;
    int mediaType = 5001;
    ArrayList<String> images;
    int card;
    String cardSQL;

    private ViewFlow viewFlow;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.circle_layout);
        UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1);
        userDbHelper.initialize(this);
        HashMap<Integer, String> hm = new HashMap<Integer, String>();
        ids = new ArrayList<Integer>();
        images = new ArrayList<String>();

        /*
         * Card ID - Getting the right card
         */
        final int cardId = getIntent().getIntExtra("card_id", 0);
        Log.i("Card Id ", "Card Id CARDS : " + cardId);

        /*
         * Collection ID - Getting the right Collection Cards
         */
        final int collId = getIntent().getIntExtra("collection_id", 0);
        Log.i("Collection Id ", "Collection Id : " + collId);

        // Position of Clicked Item
        int position = getIntent().getIntExtra("position", 0);
        Log.i("position", "position : " + position);

        /*
         * Indicating which activity is opening this one for selecting the right
         * cards from Database. Value 1 : All Stampii - listing all owned
         * stampii cards Value 2 : Owned Stampii - listing all owned stampii
         * cards Value 3 : Repeated Stampii - listing only repeated stampii
         * cards Value 4 : Last Acquired - listing the last acquired stampii
         * cards
         */
        int activity = getIntent().getIntExtra("activity", 1);
        Log.d("", "activity : " + activity);

        /*
         * Indicating the Sort Type : Value 1 : Sort by Tags Value 2 : Sort by
         * Categories Value 3 : Sort by Date
         * By Default the sort type is by Tags! (Bad-Ass Muthaphucka)
         */
        int sort = getIntent().getIntExtra("sort_type", 1);
        Log.d("", "sort type : " + sort);

        int extra = getIntent().getIntExtra("extra", 0);
        Log.d("", "extra : " + extra);

        switch (activity) {
        case 1: // All Stampii
                cardSQL = getOwnedCards(sort, collId, extra);
            break;
        case 2: // Owned Stampii
                cardSQL = getOwnedCards(sort, collId, extra);
            break;
        case 3: // Repeated Stampii
                cardSQL = getRepeatedCards(sort, collId, extra);
            break;
        case 4: // Last Acquired
                cardSQL = getOwnedCards(sort, collId, extra);
            break;
        }

        Cursor cards = userDbHelper.executeSQLQuery(cardSQL);
        if (cards.getCount() == 0) {
            cards.close();
        } else if (cards.getCount() > 0) {
            for (cards.move(0); cards.moveToNext(); cards.isAfterLast()) {
                cardsId = Integer.parseInt(cards.getString(cards
                        .getColumnIndex("objectId")));

                card = Integer.parseInt(cards.getString(cards
                        .getColumnIndex("cardId")));

                String path = Environment.getExternalStorageDirectory()
                        + "/.Stampii/MediaCard/" + cardsId + ".png";
                ids.add(card);
                hm.put(cardsId, path);

                path = hm.get(cardsId);
                images.add(path);
            }
        }

        Button back = (Button) findViewById(R.id.back_button);
        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        viewFlow = (ViewFlow) findViewById(R.id.viewflow);
        viewFlow.setAdapter(new ImageAdapter(this, images), position);

        ImageButton prevBtn = (ImageButton) findViewById(R.id.previous_button);
        prevBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                indexxx = viewFlow.getSelectedItemPosition() - 1;
                if (indexxx >= 0) {
                    viewFlow.setAdapter(new ImageAdapter(Cards.this, images),
                            indexxx);
                    viewFlow.setSelectedItemPosition(indexxx);
                    Log.i("indexxx", "indexxx : " + indexxx);
                }
            }
        });

        ImageButton nextBtn = (ImageButton) findViewById(R.id.next_button);
        nextBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                indexxx = viewFlow.getSelectedItemPosition() + 1;
                if (indexxx < images.size()) {
                    viewFlow.setAdapter(new ImageAdapter(Cards.this, images),
                            indexxx);
                    viewFlow.setSelectedItemPosition(indexxx);
                    Log.i("indexxx", "indexxx : " + indexxx);
                }
            }
        });

        Button info = (Button) findViewById(R.id.info_button);
        info.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Cards.this, SingleCardInfo.class);
                intent.putExtra("card_id",
                        ids.get(viewFlow.getSelectedItemPosition()));
                intent.putExtra("collection_id", collId);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        viewFlow.onConfigurationChanged(newConfig);
    }

    public String getOwnedCards(int sort, int collId, int extra) {
        String sql = "";
        switch (sort) {
        case 1: // Sort by Tags
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                        + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                        + "ON (cm.cardId = cd.objectId) "
                        + "INNER JOIN cardtags AS ct "
                        + "ON (cd.objectId = ct.cardId) "
                        + "INNER JOIn tags AS tg "
                        + "ON (tg.objectId = ct.tagId) "
                        + "WHERE cd.collectionId=" + collId
                        + " AND cm.mediaType=" + mediaType
                        + " AND tg.objectId=" + extra;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                        + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                        + "ON (cm.cardId = cd.objectId) "
                        + "INNER JOIN cardtags AS ct "
                        + "ON (cd.objectId = ct.cardId) "
                        + "INNER JOIn tags AS tg "
                        + "ON (tg.objectId = ct.tagId) "
                        + "WHERE cd.collectionId=" + collId
                        + " AND cm.mediaType=" + mediaType;
            break;
        case 2: // Sort by Categories
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cm.mediaType=" + mediaType
                    + " AND cats.objectId=" + extra;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cm.mediaType=" + mediaType;
            break;
        case 3: // Sort by Date
            String ASCDESC = getIntent().getStringExtra("ascdesc");
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cm.mediaType=" + mediaType
                    + " ORDER BY cd.dateCreated " + ASCDESC;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cm.mediaType=" + mediaType;
            break;
        }
        return sql;
    }

    public String getRepeatedCards(int sort, int collId, int extra){
        String sql="";
        switch (sort) {
        case 1: // Sort by Tags
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                        + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                        + "ON (cm.cardId = cd.objectId) "
                        + "INNER JOIN cardtags AS ct "
                        + "ON (cd.objectId = ct.cardId) "
                        + "INNER JOIn tags AS tg "
                        + "ON (tg.objectId = ct.tagId) "
                        + "WHERE cd.collectionId=" + collId
                        + " AND cd.repeatsCount>1"
                        + " AND cm.mediaType=" + mediaType
                        + " AND tg.objectId=" + extra;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                        + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                        + "ON (cm.cardId = cd.objectId) "
                        + "INNER JOIN cardtags AS ct "
                        + "ON (cd.objectId = ct.cardId) "
                        + "INNER JOIn tags AS tg "
                        + "ON (tg.objectId = ct.tagId) "
                        + "WHERE cd.collectionId=" + collId
                        + " AND cd.repeatsCount>1"
                        + " AND cm.mediaType=" + mediaType;
            break;
        case 2: // Sort by Categories
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cd.repeatsCount>1"
                    + " AND cm.mediaType=" + mediaType
                    + " AND cats.objectId=" + extra;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cd.repeatsCount>1"
                    + " AND cm.mediaType=" + mediaType;
            break;
        case 3: // Sort by Date
            String ASCDESC = getIntent().getStringExtra("ascdesc");
            Log.d("","ASCDESC : "+ASCDESC);
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cd.repeatsCount>1"
                    + " AND cm.mediaType=" + mediaType
                    + " ORDER BY cd.dateCreated " + ASCDESC;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cd.repeatsCount>1"
                    + " AND cm.mediaType=" + mediaType;
            break;
        }
        return sql;
    }

}

Here is how I solve this issue :

import java.util.ArrayList;
import java.util.HashMap;

import com.stampii.stampii.R;
import com.stampii.stampii.comm.rpc.UserDatabaseHelper;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class Cards extends Activity {

    public Cursor cursor;
    int indexxx;
    Bitmap b;
    int objectId;
    int cardsId;
    ArrayList<Integer> ids;
    String path;
    int mediaType = 5001;
    ArrayList<String> images;
    int card;
    String cardSQL;

    private ViewFlow viewFlow;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.circle_layout);
        UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1);
        userDbHelper.initialize(this);
        HashMap<Integer, String> hm = new HashMap<Integer, String>();
        ids = new ArrayList<Integer>();
        images = new ArrayList<String>();

        /*
         * Card ID - Getting the right card
         */
        final int cardId = getIntent().getIntExtra("card_id", 0);
        Log.i("Card Id ", "Card Id CARDS : " + cardId);

        /*
         * Collection ID - Getting the right Collection Cards
         */
        final int collId = getIntent().getIntExtra("collection_id", 0);
        Log.i("Collection Id ", "Collection Id : " + collId);

        // Position of Clicked Item
        int position = getIntent().getIntExtra("position", 0);
        Log.i("position", "position : " + position);

        /*
         * Indicating which activity is opening this one for selecting the right
         * cards from Database. Value 1 : All Stampii - listing all owned
         * stampii cards Value 2 : Owned Stampii - listing all owned stampii
         * cards Value 3 : Repeated Stampii - listing only repeated stampii
         * cards Value 4 : Last Acquired - listing the last acquired stampii
         * cards
         */
        int activity = getIntent().getIntExtra("activity", 1);
        Log.d("", "activity : " + activity);

        /*
         * Indicating the Sort Type : Value 1 : Sort by Tags Value 2 : Sort by
         * Categories Value 3 : Sort by Date
         * By Default the sort type is by Tags! (Bad-Ass Muthaphucka)
         */
        int sort = getIntent().getIntExtra("sort_type", 1);
        Log.d("", "sort type : " + sort);

        int extra = getIntent().getIntExtra("extra", 0);
        Log.d("", "extra : " + extra);

        switch (activity) {
        case 1: // All Stampii
                cardSQL = getOwnedCards(sort, collId, extra);
            break;
        case 2: // Owned Stampii
                cardSQL = getOwnedCards(sort, collId, extra);
            break;
        case 3: // Repeated Stampii
                cardSQL = getRepeatedCards(sort, collId, extra);
            break;
        case 4: // Last Acquired
                cardSQL = getOwnedCards(sort, collId, extra);
            break;
        }

        Cursor cards = userDbHelper.executeSQLQuery(cardSQL);
        if (cards.getCount() == 0) {
            cards.close();
        } else if (cards.getCount() > 0) {
            for (cards.move(0); cards.moveToNext(); cards.isAfterLast()) {
                cardsId = Integer.parseInt(cards.getString(cards
                        .getColumnIndex("objectId")));

                card = Integer.parseInt(cards.getString(cards
                        .getColumnIndex("cardId")));

                String path = Environment.getExternalStorageDirectory()
                        + "/.Stampii/MediaCard/" + cardsId + ".png";
                ids.add(card);
                hm.put(cardsId, path);

                path = hm.get(cardsId);
                images.add(path);
            }
        }

        Button back = (Button) findViewById(R.id.back_button);
        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        viewFlow = (ViewFlow) findViewById(R.id.viewflow);
        viewFlow.setAdapter(new ImageAdapter(this, images), position);

        ImageButton prevBtn = (ImageButton) findViewById(R.id.previous_button);
        prevBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                indexxx = viewFlow.getSelectedItemPosition() - 1;
                if (indexxx >= 0) {
                    viewFlow.setAdapter(new ImageAdapter(Cards.this, images),
                            indexxx);
                    viewFlow.setSelectedItemPosition(indexxx);
                    Log.i("indexxx", "indexxx : " + indexxx);
                }
            }
        });

        ImageButton nextBtn = (ImageButton) findViewById(R.id.next_button);
        nextBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                indexxx = viewFlow.getSelectedItemPosition() + 1;
                if (indexxx < images.size()) {
                    viewFlow.setAdapter(new ImageAdapter(Cards.this, images),
                            indexxx);
                    viewFlow.setSelectedItemPosition(indexxx);
                    Log.i("indexxx", "indexxx : " + indexxx);
                }
            }
        });

        Button info = (Button) findViewById(R.id.info_button);
        info.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Cards.this, SingleCardInfo.class);
                intent.putExtra("card_id",
                        ids.get(viewFlow.getSelectedItemPosition()));
                intent.putExtra("collection_id", collId);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        viewFlow.onConfigurationChanged(newConfig);
    }

    public String getOwnedCards(int sort, int collId, int extra) {
        String sql = "";
        switch (sort) {
        case 1: // Sort by Tags
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                        + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                        + "ON (cm.cardId = cd.objectId) "
                        + "INNER JOIN cardtags AS ct "
                        + "ON (cd.objectId = ct.cardId) "
                        + "INNER JOIn tags AS tg "
                        + "ON (tg.objectId = ct.tagId) "
                        + "WHERE cd.collectionId=" + collId
                        + " AND cm.mediaType=" + mediaType
                        + " AND tg.objectId=" + extra;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                        + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                        + "ON (cm.cardId = cd.objectId) "
                        + "INNER JOIN cardtags AS ct "
                        + "ON (cd.objectId = ct.cardId) "
                        + "INNER JOIn tags AS tg "
                        + "ON (tg.objectId = ct.tagId) "
                        + "WHERE cd.collectionId=" + collId
                        + " AND cm.mediaType=" + mediaType;
            break;
        case 2: // Sort by Categories
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cm.mediaType=" + mediaType
                    + " AND cats.objectId=" + extra;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cm.mediaType=" + mediaType;
            break;
        case 3: // Sort by Date
            String ASCDESC = getIntent().getStringExtra("ascdesc");
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cm.mediaType=" + mediaType
                    + " ORDER BY cd.dateCreated " + ASCDESC;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cm.mediaType=" + mediaType;
            break;
        }
        return sql;
    }

    public String getRepeatedCards(int sort, int collId, int extra){
        String sql="";
        switch (sort) {
        case 1: // Sort by Tags
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                        + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                        + "ON (cm.cardId = cd.objectId) "
                        + "INNER JOIN cardtags AS ct "
                        + "ON (cd.objectId = ct.cardId) "
                        + "INNER JOIn tags AS tg "
                        + "ON (tg.objectId = ct.tagId) "
                        + "WHERE cd.collectionId=" + collId
                        + " AND cd.repeatsCount>1"
                        + " AND cm.mediaType=" + mediaType
                        + " AND tg.objectId=" + extra;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                        + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                        + "ON (cm.cardId = cd.objectId) "
                        + "INNER JOIN cardtags AS ct "
                        + "ON (cd.objectId = ct.cardId) "
                        + "INNER JOIn tags AS tg "
                        + "ON (tg.objectId = ct.tagId) "
                        + "WHERE cd.collectionId=" + collId
                        + " AND cd.repeatsCount>1"
                        + " AND cm.mediaType=" + mediaType;
            break;
        case 2: // Sort by Categories
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cd.repeatsCount>1"
                    + " AND cm.mediaType=" + mediaType
                    + " AND cats.objectId=" + extra;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cd.repeatsCount>1"
                    + " AND cm.mediaType=" + mediaType;
            break;
        case 3: // Sort by Date
            String ASCDESC = getIntent().getStringExtra("ascdesc");
            Log.d("","ASCDESC : "+ASCDESC);
            if (extra != 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cd.repeatsCount>1"
                    + " AND cm.mediaType=" + mediaType
                    + " ORDER BY cd.dateCreated " + ASCDESC;
            else if (extra == 0)
                sql = "SELECT DISTINCT cm.cardId, cm.objectId "
                    + "FROM cardmedias AS cm " + "INNER JOIN cards AS cd "
                    + "ON (cm.cardId = cd.objectId) "
                    + "INNER JOIN categories AS cats "
                    + "ON (cd.categoryId = cats.objectId) "
                    + "WHERE cd.collectionId=" + collId
                    + " AND cd.repeatsCount>1"
                    + " AND cm.mediaType=" + mediaType;
            break;
        }
        return sql;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文