简体   繁体   中英

How to display content from sqlite using ViewFlipper in android

In my scenario i am having 3 records in sqlite which were fetched from websercive that contains id,name,description and image now i want to display the first record and second one after flipping the first record.and 3 one after second record flips..

What i have done so far is

  while (cursor.moveToNext())
    {

         String temp_bookid = cursor.getString(0);
         Log.i("temp_bookid",temp_bookid);
         String temp_desc=cursor.getString(1);
         byte[] temp_image1 = cursor.getBlob(2);
         byte[] temp_image2 = cursor.getBlob(3);
         String temp_id=cursor.getString(4);
         String temp_name = cursor.getString(5);
         String temp_bname=cursor.getString(6);
         mapId.add(temp_id);
         mapBname.add(temp_bname);
         mapBookId.add(temp_bookid);
         mapName.add(temp_name);
         mapDesc.add(temp_desc);
         map2.add(temp_image2);
         map1.add(temp_image1);
            Log.i("temp_id of the page in sqlite", temp_id);

            TextView txtDesc = (TextView) findViewById(R.id.tDescription);
            text = (TextView) findViewById(R.id.tTitle);
            text.setText(temp_name);

            txtDesc.setText(temp_desc);

            ImageView img = (ImageView)findViewById(R.id.smallImage);


            img.setImageBitmap(BitmapFactory.decodeByteArray(temp_image1, 0,temp_image1.length));
            txtName1 = (TextView) findViewById(R.id.tvName);
            txtName1.setText(temp_bname);

            break;
    }   

            mContext = this;
        vf = (ViewFlipper) this.findViewById(R.id.vfShow);
        vf.setOnTouchListener(new OnTouchListener() {
        @Override
            public boolean onTouch(final View view, final MotionEvent event) {
                detector.onTouchEvent(event);
                Log.i("ew","ae");

                return true;
            }
        });

I am able to display first record data when i set break; statement otherwise it is displaying last record data..How can proceed for this approach?

First fetch all the records and store in List<YourDataClass> myList object from onCreate method. As I have just giving you the examples

Now after fetching the data and stored in your list object you need to implement flipper change event so when flipper was change get the current position and using this position get the data as object from your list object. As you move right/left it will giving you the current position and you just need to use this position to fetch the records from list object

For example

Your POJO class will store the data MyData.java

public class MyData{
    public long id = -1;
    public String name = "";
    public String description = "";
    public String imagePath = ""; /// I don't know you want exactly, you need to implement image as per your requirement.
}

In your activity

private List<MyData> myList = new ArrayList<MyData>();


oncreate(){

fetchData(); // you need to implement this in AsyncTask so UI was not block just implement AsyncTask and call this method in doInBackground().

// after this you can implment View flipper and add view and set the data by fetch the list object using current view flipper position.

}

private void fetchData(){

    while (cursor.moveToNext()){
         MyData myData = new MyData();
         myData.id = cursor.getLong(0);
         myData.desc=cursor.getString(1);
         myData.name = cursor.getString(5);
         list.add(myData);
         myData = null;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM