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);
}
我已经显示数据库文件apply select * from table name我已经显示了数据IMage路径列及其值但是我无法显示请帮助我做错了我已经采取了r.imageview
答案 0 :(得分:0)
我怀疑SimpleCursorAdapter
可以在您的布局中找到ImageView
,然后将字节数组解码为Bitmap
并为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)));
}
答案 1 :(得分:0)
通过您已完成的方式定义适配器将无法工作,除非您为图像提供资源ID(如R.id.imagename
)。
我认为您需要通过路径从互联网上获取图像。最好创建一个扩展BaseAdapter
的自定义适配器类。在getView()
内,你可以拥有下载图像的逻辑(通过AsynTask
)然后显示它。