如何assing基于查询的结果的图像,以ImageView的? [英] How to assing an Image to Imageview based on a query results?

查看:188
本文介绍了如何assing基于查询的结果的图像,以ImageView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家中午好,

我有一个快速的问题。我有我存储在一个列表/阵列的查询结果,我试图以显示ImageView的相应的图像。查询提供了一个存储在资源文件夹(RES /可绘)图象的一个名称。我得到错误的语法。我不知道如何解决这个问题。我有一个名为穗字段(数据库)存储图像的名称的数据库。

我已经试过这code:

<一个href=\"http://stackoverflow.com/questions/24755122/how-to-work-with-images-and-database-in-android\">how在Android的图像和数据库工作?

但它不该类工作。我不知道是不是因为这个类是扩展ArrayAdapter {,而不是延伸活动{

这是我的code:

zCustomUsersAdapter类:

 包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&LT; YAOYVD&GT; {      公共zCustomUsersAdapter(上下文的背景下,列表与LT; YAOYVD&GT;用户){
          超级(上下文,0,用户);       }       @覆盖
       公共查看getView(INT位置,查看convertView,父母的ViewGroup){
          //获取数据项这个职位
         //用户的用户=的getItem(位置);
           YAOYVD用户=的getItem(位置);
          //检查现有视图被重用,否则膨胀的观点
          如果(convertView == NULL){
             convertView = LayoutInflater.from(的getContext())膨胀(R.layout.zitem_user,父母,假的)。
          }
          数据人口//查询视图
          TextView的tvName =(TextView中)convertView.findViewById(R.id.tvName);
          TextView的tvHome =(TextView中)convertView.findViewById(R.id.tvHometown);          ImageView的tvImage =(ImageView的)convertView.findViewById(R.id.ivUserIcon);
          //使用数据对象填充数据插入模板视图
          tvName.setText(将String.valueOf(user.getID_YAO()));
                //。名称);
          tvHome.setText(将String.valueOf(user.getNAME_YAO()));
          //。家乡);        // tvImage.setBackgroundResource(getResourceID(将String.valueOf(user.getSPIC_YAO()),可拉伸,getApplicationContext()));
          //返回完成视图呈现在屏幕上
          tvImage.setBackgroundResource(user.getSPIC_YAO()); // getSpic_YAO这是在姚类字符串。 getSpic_YAO retrive在可绘制文件夹中的图像的名称。         返回convertView;
     } }

zCustomListActivity

 包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数据源;
     @覆盖
     保护无效的onCreate(捆绑savedInstanceState){
         super.onCreate(savedInstanceState);
         的setContentView(R.layout.zactivity_custom_list);        //调用该方法来创建资源文件夹数据库
         copydbfromassest();        //数据源=新YAODeckListDataSource(本);
        数据源=新YAOGetDataSource2(本);
         datasource.open();
         populateUsersList();    }     私人无效populateUsersList(){
         //构造数据源
         //列表&LT; YAOYVD&GT;值= datasource.SQLYVDTABLESEARCH();
         清单&LT; YAOYVD&GT; arrayOfUsers = datasource.SQLYVDTABLESEARCH();
                // getUsers()。
         //创建适配器连接到阵列转换意见
         zCustomUsersAdapter适配器=新zCustomUsersAdapter(这一点,arrayOfUsers);
         //附加适配器到ListView
         ListView控件的ListView =(ListView控件)findViewById(R.id.lvUsers);
         listView.setAdapter(适配器);
    }     // @覆盖
         公共无效的onClick(查看视图){
             // TODO自动生成方法存根        }
         //数据库打开和关闭
         @覆盖
         保护无效onResume(){
         datasource.open();
         super.onResume();
         }         @覆盖
         保护无效的onPause(){
         datasource.close();
         super.onPause();
         }         @覆盖
         保护无效的onDestroy(){
           datasource.close();
           super.onDestroy();         }
     / ****
     *新的东西考虑
     * /
         ///复制从入资产的文件夹数据库         私人无效copydbfromassest(){
             // TODO自动生成方法存根
             YAOMySQLiteHelper myhelper =新YAOMySQLiteHelper(本);            尝试{
                //创建datbase
                myhelper.importIfNotExist();
             }赶上(IOException异常五){                抛出新的错误(无法创建数据库);             }            尝试{
                myhelper.openDataBase();
            }赶上(的SQLException SQLE){                扔SQLE;
            }
         } }


解决方案

您需要检索与该图像的名称相关联的资源标识符。对于这一点,你需要让你的应用程序的资源,这需要一个上下文。

 上下文CTX =的getContext();
INT渣油= ctx.getResources()则getIdentifier(user.getSPIC_YAO(),绘制,ctx.getPackageName());
如果(残油!= 0){
    tvImage.setBackgroundResource(渣油);
}

Good afternoon everyone,

I have a quick question. I have a query result that I store in a list/array and I am trying to display the appropriate image in the imageview. The query provides a name of the image that is stored in the resource folder(res/drawable). I am getting error syntax. I am not sure how to solve this issue. I have a database that stores the name of the image in a field (database) called spic.

I have tried this code:

how to work with images and database in android?

but it doesnt work in this class. I am not sure is it because this class is "extends ArrayAdapter {" instead of "extends Activity {"

here is my code:

zCustomUsersAdapter class:

  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;
     }

 }

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;
            }
         }

 }

解决方案

You need to retrieve the resource identifier associated with that image's name. For this, you will need to get your app's Resources, which requires a context.

Context ctx = getContext();
int resId = ctx.getResources().getIdentifier(user.getSPIC_YAO(), "drawable", ctx.getPackageName());
if(resId != 0){
    tvImage.setBackgroundResource(resId);
}

这篇关于如何assing基于查询的结果的图像,以ImageView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆