Android 如何根据查询结果将图像分配到Imageview?
各位下午好, 我有一个简短的问题。我有一个存储在列表/数组中的查询结果,我正在尝试在imageview中显示相应的图像。查询提供存储在资源文件夹(res/drawable)中的图像的名称。我得到了错误的语法。我不知道如何解决这个问题。我有一个数据库,它将图像的名称存储在一个名为spic的字段(数据库)中 我尝试过以下代码: 但是这门课不起作用。我不确定是不是因为这个类是“extends ArrayAdapter{”而不是“extends Activity{” 这是我的密码: zCustomUsersAdapter类:Android 如何根据查询结果将图像分配到Imageview?,android,database,image,sqlite,imageview,Android,Database,Image,Sqlite,Imageview,各位下午好, 我有一个简短的问题。我有一个存储在列表/数组中的查询结果,我正在尝试在imageview中显示相应的图像。查询提供存储在资源文件夹(res/drawable)中的图像的名称。我得到了错误的语法。我不知道如何解决这个问题。我有一个数据库,它将图像的名称存储在一个名为spic的字段(数据库)中 我尝试过以下代码: 但是这门课不起作用。我不确定是不是因为这个类是“extends ArrayAdapter{”而不是“extends Activity{” 这是我的密码: zCustomUs
package com.example.yao;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class zCustomUsersAdapter extends ArrayAdapter<YAOYVD> {
public zCustomUsersAdapter(Context context, List<YAOYVD> users) {
super(context, 0, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
// User user = getItem(position);
YAOYVD user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.zitem_user, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
ImageView tvImage = (ImageView) convertView.findViewById(R.id.ivUserIcon);
// Populate the data into the template view using the data object
tvName.setText(String.valueOf(user.getID_YAO()));
//.name);
tvHome.setText(String.valueOf(user.getNAME_YAO()));
//.hometown);
// tvImage.setBackgroundResource( getResourceID (String.valueOf(user.getSPIC_YAO()), "drawable",getApplicationContext() ));
// Return the completed view to render on screen
tvImage.setBackgroundResource(user.getSPIC_YAO()); //getSpic_YAO this is string in the YAO class. getSpic_YAO retrive the name of the image in the drawable folder.
return convertView;
}
}
package com.example.yao;
导入java.util.ArrayList;
导入java.util.List;
导入android.content.Context;
导入android.graphics.drawable.drawable;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类zCustomUsersAdapter扩展了ArrayAdapter{
公共zCustomUsersAdapter(上下文,列表用户){
超级(上下文,0,用户);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//获取此职位的数据项
//用户=获取项目(位置);
YAYVD user=getItem(位置);
//检查是否正在重用现有视图,否则会膨胀视图
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.zitem_user,parent,false);
}
//数据填充的查找视图
TextView-tvName=(TextView)convertView.findViewById(R.id.tvName);
TextView tvHome=(TextView)convertView.findViewById(R.id.tvHome);
ImageView tImage=(ImageView)convertView.findViewById(R.id.ivUserIcon);
//使用数据对象将数据填充到模板视图中
tvName.setText(String.valueOf(user.getID_-YAO());
//(姓名);
tvHome.setText(String.valueOf(user.getNAME_-YAO());
//(八)家乡;;
//setBackgroundResource(getResourceID(String.valueOf(user.getSPIC_-YAO()),“drawable”,getApplicationContext());
//返回要在屏幕上渲染的已完成视图
tvImage.setBackgroundResource(user.getSPIC_-YAO());//getSPIC_-YAO这是YAO类中的字符串。getSPIC_-YAO在可绘制文件夹中检索图像的名称。
返回视图;
}
}
zCustomListActivity
package com.example.yao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
public class zCustomListActivity extends Activity {
private YAOGetDataSource2 datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zactivity_custom_list);
//calling the method to create the database from asset folder
copydbfromassest();
// datasource = new YAODeckListDataSource(this);
datasource = new YAOGetDataSource2(this);
datasource.open();
populateUsersList();
}
private void populateUsersList() {
// Construct the data source
//List<YAOYVD> values = datasource.SQLYVDTABLESEARCH();
List<YAOYVD> arrayOfUsers = datasource.SQLYVDTABLESEARCH();
//.getUsers();
// Create the adapter to convert the array to views
zCustomUsersAdapter adapter = new zCustomUsersAdapter(this, arrayOfUsers);
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.lvUsers);
listView.setAdapter(adapter);
}
//@Override
public void onClick(View view) {
// TODO Auto-generated method stub
}
//database open and close
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
@Override
protected void onDestroy(){
datasource.close();
super.onDestroy();
}
/****
* new things to considered
*/
///copy the database from assest folder
private void copydbfromassest() {
// TODO Auto-generated method stub
YAOMySQLiteHelper myhelper = new YAOMySQLiteHelper (this);
try{
//create datbase
myhelper.importIfNotExist();
}catch (IOException e){
throw new Error("Unable to Create the Database ");
}
try{
myhelper.openDataBase();
}catch (SQLException sqle){
throw sqle;
}
}
}
package com.example.yao;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入android.app.Activity;
导入android.content.Context;
导入android.database.SQLException;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.ImageView;
导入android.widget.ListView;
公共类zCustomListActivity扩展了活动{
私有YAOGetDataSource2数据源;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.zactivity\u自定义\u列表);
//调用方法从资产文件夹创建数据库
copydbfromassest();
//datasource=新的ListDataSource(此);
datasource=新的YAOGetDataSource2(此);
datasource.open();
populateUsersList();
}
私有无效的populateUserList(){
//构建数据源
//列表值=datasource.SQLYVDTABLESEARCH();
List ArrayFuser=datasource.SQLYVDTABLESEARCH();
//.getUsers();
//创建适配器以将阵列转换为视图
zCustomUsersAdapter adapter=新的zCustomUsersAdapter(这是ArrayFuser);
//将适配器连接到ListView
ListView ListView=(ListView)findViewById(R.id.lvUsers);
setAdapter(适配器);
}
//@凌驾
公共void onClick(视图){
//TODO自动生成的方法存根
}
//数据库打开和关闭
@凌驾
受保护的void onResume(){
datasource.open();
super.onResume();
}
@凌驾
受保护的void onPause(){
datasource.close();
super.onPause();
}
@凌驾
受保护的空onDestroy(){
datasource.close();
super.ondestory();
}
/****
*需要考虑的新事物
*/
///从assest文件夹复制数据库
私有void copydbfromassest(){
//TODO自动生成的方法存根
YAOMySQLiteHelper myhelper=新的YAOMySQLiteHelper(此);
试一试{
//创建数据库
myhelper.importIfNotExist();
}捕获(IOE异常){
抛出新错误(“无法创建数据库”);
}
试一试{
myhelper.openDataBase();
}捕获(SQLException sqle){
抛出sqle;
}
}
}
您需要检索与该图像名称关联的资源标识符。为此,您需要获取应用程序的资源,这需要上下文
Context ctx = getContext();
int resId = ctx.getResources().getIdentifier(user.getSPIC_YAO(), "drawable", ctx.getPackageName());
if(resId != 0){
tvImage.setBackgroundResource(resId);
}
请确保您在
spic
中保存的内容不包含任何路径或扩展名-只有文件名.spic只包含一个单词而不是扩展名。嗨,我删除了此代码“tvmimage.setBackgroundResource(user.getSPIC_());”我添加了你的代码。现在,我得到了一个关于未找到资源的错误。这意味着什么?在我的数据库中有8220个唯一记录,而我只有4400个图像。这可能是问题吗?有没有一种方法可以使if条件验证if未找到,并将其替换为另一个图像?是否包含ch