Android从sqlite检索图像并通过listview项目将其传递给下一个活动

时间:2013-05-12 17:52:38

标签: android database image sqlite listview

提前致谢。我成功将数据从列表视图传递到下一个活动,但现在我需要将数据库从我的数据库添加到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);

        }
}

下一个活动:

    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);

    }
}

2 个答案:

答案 0 :(得分:0)

从db检索图像路径并存储在hashset或hashmap中,然后存储在sharedprefrence中,在下一个活动中,您可以从sharepreference获取图像。希望你得到所有这些东西

答案 1 :(得分:0)

在调用intent.putExtra("ipath",imagePath)之前,你可以使用startActivity(intent)传递图像路径。然后在下一个活动中获取此路径。立即加载图像..