0

Json FUnction 能够解析数据并在 sq-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);
    }

我已经显示了数据库文件应用从表名中选择 * 我已经显示了数据图像路径列及其值但是我无法显示请帮助我做错了我采取了 r.imageview

4

2 回答 2

0

通过您所做的方式定义适配器将不起作用,除非您为图像提供资源 ID(Like R.id.imagename)。

我假设您需要通过路径从 Internet 获取图像。最好创建一个扩展的自定义适配器类BaseAdapter。在里面getView()你可以有下载图像的逻辑(通过AsynTask)然后显示它。

于 2013-01-03T07:22:07.887 回答
0

我怀疑SimpleCursorAdapter可以ImageView在您的布局中找到 a ,然后将字节数组解码为 aBitmap并为其设置图像ImageView。您将不得不覆盖getView()适配器并自己在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 回答