Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

database - Android retrieve image from sqlite and pass it to next activity through listview item

Thanks in advance. I sucess to pass data from list view to next activity but now i need to add an image from my database to listview and then pass it to the next activity. Or if it's easeiest just pass the image to the next activity without show it in the listview.

package com.example.assfar.travel_guide;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.example.assfar.R;
import com.example.assfar.database.DBHelper;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;



public class TravelGuide extends ListActivity {

    private DBHelper dataBase;
    private List<String> guide_title_list= new ArrayList<String>();
    HashMap<String,String> details= new HashMap<String,String>();
    private Cursor cursor;
    String title;
    Integer id;
    String desc;
    private ImageView imgv;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.travelguide);

        imgv = (ImageView) findViewById(R.id.image);

        dataBase=  DBHelper.instance();
        SQLiteDatabase db= dataBase.getWritableDatabase();

        cursor= db.rawQuery("select * from Tour_Guide", null);

        if(cursor!=null) {
             if(cursor.moveToFirst());
             {
                do
                  {

         desc = cursor.getString(cursor.getColumnIndex("tour_Description"));
         title= cursor.getString(cursor.getColumnIndex("guide_title")); 
         id= cursor.getInt(cursor.getColumnIndex("_id"));
               guide_title_list.add("" + title +id );
                 details.put("" + title +id  ,title+desc+imgv);

                  } while(cursor.moveToNext());

             }

         }

        this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, guide_title_list));

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
       super.onListItemClick(l, v, position, id);

       //String selectedItem= ((TextView) v).getText().toString();

       String selectedValue= (String) getListAdapter(). getItem(position);
       String itemTitleDesc=(String) details.get(selectedValue);

       Intent i = new Intent(TravelGuide.this, Travel_Guide_Details.class);

        i.putExtra("selected item", selectedValue);
        i.putExtra("selected item", itemTitleDesc);
        startActivity(i);

        }
}

next activity:

    package com.example.assfar.travel_guide;

import com.example.assfar.R;
import com.example.assfar.database.DBHelper;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class Travel_Guide_Details extends Activity {
    private DBHelper dataBase;


    private TextView textView;
    private ImageView imgv;
    Bitmap bmp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.travel_guide_details);

        //tour guide description in text view
        textView = (TextView)findViewById(R.id.guide_desc);



        Intent i= getIntent();

        //getting attached intent data
        String item = i.getStringExtra("selected item");

        //displaying selected item name
        textView.setText(item);

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

YOu can pass the image path using intent.putExtra("ipath",imagePath) before calling the startActivity(intent). And then get this path in your next activity. Load the image now..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...