Android 如何显示(检索)sqlite数据库中的图像
Json函数能够解析数据并在sq lite数据库中插入值Android 如何显示(检索)sqlite数据库中的图像,android,Android,Json函数能够解析数据并在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
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);
}
由此我可以显示标题和描述,但我不能显示图像
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我已显示数据图像路径列及其值,但无法显示请帮助我哪里做错了我已拍摄r.imageview我怀疑
SimpleCursorAdapter
是否能在布局中找到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;
}
};
除非您为映像提供资源id(如
R.id.imagename
),否则按照您所做的方式定义适配器将不起作用
我假设您需要通过路径从internet获取图像。最好创建一个扩展
BaseAdapter
的自定义适配器类。在getView()
中,您可以拥有下载图像的逻辑(通过AsynTask
)然后显示它。您能告诉我谁将执行此操作吗?你可以修改编码吗?请给我一些代码,我可以显示标题和描述字符串值,但不能显示图像。你可以编写图像显示代码吗?请解释奇点,尝试创建自定义适配器。用于下载和显示您可以使用的图像
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)));
}