0

Json FUnction は、データを解析して sql-lite データベースに値を挿入できます

for (int i = 0; i < jArray6.length(); i++) {
                    catagoryid = 5;
                    json_data = jArray6.getJSONObject(i);
                    // news_id = Integer.parseInt((json_data.getString("news_id")));
                    news_id = Double.parseDouble(json_data.getString("news_id"));
                    news_title = json_data.getString("news_title");
                    imagepath = json_data.getString("imagepath").trim().getBytes();
                    news_short_description = json_data
                            .getString("news_short_description");
                    news_detail = json_data.getString("news_detail_description");
                    news_date = json_data.getString("news_date");
                    website_link = json_data.getString("website_link").trim();
                    dh = new DBhelper(TaxMann.this);
                    record_id = dh.AddMsgt1(news_id, catagoryid, news_title,
                            imagepath, news_short_description, news_date,
                            news_detail, website_link);
                }

これは私のデータベースクラス関数です

@Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + topstories
                + "(_id INTEGER PRIMARY KEY AUTOINCREMENT," + news_id
                + " double, " + catagory_Id + " integer," + news_title
                + " text," + imagepath + " BLOB, " + short_description
                + " text, " + news_date + " text, " + detailed_news + " text, "
                + website_link + " text)");
        db.execSQL("CREATE TABLE " + savednews
                + "(_id INTEGER PRIMARY KEY AUTOINCREMENT," + news_id
                + " double," + news_title + " text," + imagepath + " BLOB,  "
                + detailed_news + " text)");
        db.execSQL("CREATE TABLE " + date
                + "(_id INTEGER PRIMARY KEY AUTOINCREMENT," + modified_date
                + " integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("drop table if exists " + topstories);
        db.execSQL("drop table if exists " + savednews);
        db.execSQL("drop table if exists " + date);
        onCreate(db);
    }

    // records for newstable

public int AddMsgt1(double newsid, Integer cat_id, String title,
        byte[] image, String desc, String date, String detail,
        String website) {
    db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(news_id, newsid);
    cv.put(catagory_Id, cat_id);
    cv.put(news_title, title);
    cv.put(imagepath, image);
    cv.put(short_description, desc);
    cv.put(news_date, String.valueOf(date));
    cv.put(detailed_news, detail);
    cv.put(website_link, website);

    long record_id = db.insert(topstories, news_title, cv);
    db.close();
    return (int) record_id;
}

public Cursor fetchtitle(int catagoryid) {
            SQLiteDatabase db1 = this.getReadableDatabase();
            cursor = db1.query(topstories,
                    new String[] { "_id", "news_title", "imagepath",
                            "short_description", "news_date", "detailed_news", },
                    "catagory_id=" + catagoryid, null, null, null, null, null);
            return cursor;
        }

これから、タイトルと説明を表示することはできますが、画像を表示することはできません

public void simple_ad(Cursor cursor,int category_id)
    {
        dh = new DBhelper(this);
        ListView list = (ListView)findViewById(R.id.expertscommentsListView);
        String[] colummnames = { "news_title","imagepath","descripton"}; 
        cursor = dh.fetchtitle(category_id);
        int []id=  { R.id.titleTextView,R.id.descriptionTextView,R.id.imagview2};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,  R.layout.new_list, cursor , colummnames,id);
        list.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        list.setOnItemClickListener(this);
    }

私はデータベースファイルを表示しましたテーブル名から選択*を適用しますデータのIMageパス列とその値を表示しましたしかし表示できません私が間違っているところを助けてください私はr.imageviewを取りました

4

2 に答える 2

0

SimpleCursorAdapterあなたのレイアウトでを見つけてImageView、バイト配列を にデコードし、Bitmapその のイメージを設定できるとは思えませんImageViewgetView()アダプタをオーバーライドして、自分で行う必要がありますgetView()

例:

データクラス:

public class NewsItem{
  public final String newsTitle;
  public final byte[] imageData;
  public final String description;

  public NewsItem (String title, byte[] data, String desc){
    this.newsTitle = title;
    this.imageData = data;
    this.description = desc;
  }
}

アダプター:

ArrayAdapter<NewsItem> adapter = new ArrayAdapter<NewsItem>(this){
  @Override
  public View getView(int position, View convertView, View parent){

    if(convertView == null){
       convertView = //--inflate list item layout
    }

    NewsItem item = getItem(position);

    ImageView i = convertView.findViewById(R.id.myImageView);
    Bitmap b = BitmapFactory.decodeByteArray(item.imageData,0,item.imageData.length);
    i.setDrawable(new BitmapDrawable(b));

    //--other stuff

    return convertView;
  }
};

アダプターを満たします。

Cursor c = dh.fetchtitle(category_id);
while (c.moveToNext()){
  adapter.add(new NewsItem(c.getString(0),c.getBlob(1),c.getString(2)));  
}
于 2013-01-03T06:48:47.160 に答える
0

あなたが行った方法でアダプターを定義しても、画像のリソースIDを提供しない限り機能しません(のようにR.id.imagename)。

パスでインターネットから画像を取得する必要があると想定しました。を拡張する 1 つのカスタム アダプタ クラスを作成することをお勧めしますBaseAdapter。内部getView()には、( を介して) 画像をダウンロードして表示するためのロジックを含めることができますAsynTask

于 2013-01-03T07:22:07.887 に答える