如何从绘制根据自己的SQLite数据库名称来获取图像,然后在列表视图中显示它 [英] how to get image from drawable according to their names in the sqlite database and then display it in list view

查看:167
本文介绍了如何从绘制根据自己的SQLite数据库名称来获取图像,然后在列表视图中显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用SQLite和图像绘制一个Android应用程序。

i am creating an android application that use sqlite and images from drawable.

我需要的是显示图像中绘制文件夹中现有的根据自己的SQLite数据库的名称,但系统显示错误:

what i need is to display the images existing in drawable folder according to their names in the sqlite database but the system display an error :

的方法getResources()是未定义的类型CustomAdapterMatchSchedule

这是我的code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Group"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/txtDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Date"
        android:textAppearance="?android:attr/textAppearanceSmall" />



    <TextView
        android:id="@+id/textName1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentLeft="true"
        android:text="name1" />

    <TextView
        android:id="@+id/textName2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView2"
        android:layout_alignParentRight="true"
        android:text="Name2" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textLocation"
        android:layout_toRightOf="@+id/textName1"
        android:adjustViewBounds="true"
        android:maxHeight="40dp"
        android:maxWidth="40dp"
        android:scaleType="fitCenter"
        android:src="@drawable/algeria_flag" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_toLeftOf="@+id/textName2"
        android:adjustViewBounds="true"
        android:maxHeight="40dp"
        android:maxWidth="40dp"
        android:scaleType="fitCenter"
        android:src="@drawable/algeria_flag" />

    <TextView
        android:id="@+id/textLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textGroup"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="Location" />

</RelativeLayout>

CustomAdapterMatchSchedule.java

package com.devleb.expandablelistdemo3;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapterMatchSchedule extends BaseAdapter {
    ArrayList<ItemDetails> itemdetailsList;
    Context context;

    public CustomAdapterMatchSchedule(Context context, ArrayList<ItemDetails> list) {

            this.context = context;
            itemdetailsList = list;
        }




    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemdetailsList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return itemdetailsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        ItemDetails itemdetail = itemdetailsList.get(position);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_list_match_schedule, null);
        }
      //Stad_name
        TextView txtStadName = (TextView)convertView.findViewById(R.id.textLocation);
        txtStadName.setText(itemdetail.getStad_name());
        //team1
        TextView txtTeam1 = (TextView)convertView.findViewById(R.id.textName1);
        txtTeam1.setText(itemdetail.getTeam1());
        //team2
        TextView txtTeam2 = (TextView)convertView.findViewById(R.id.textName2);
        txtTeam2.setText(itemdetail.getTeam2());

        //List of images  
        //****get the Image from drwable folder according to their names in the sqlite database******//
        //
       // ImageView imgflag1 = (ImageView)convertView.findViewById(R.id.imageView1);
       // imgflag1.

        int imageid = getResources().getIdentifier("com.devleb.expandablelistdemo3:drawable/brazil_flag", null, null);
        ImageView imagenow = (ImageView)convertView.findViewById(R.id.imageView1);
        imagenow.setImageResource(imageid);





        return null;
    }

}

因此​​,如何从这里继续谁能帮助我?

so how to continue from here can anyone help me???

推荐答案

则getIdentifier是好的,但你应该使用这种方式:

getIdentifier is ok, but you should use it this way:

 int imageid = getResources().getIdentifier("brazil_flag", "drawable", context.getPackageName());

第一个参数是您正在寻找的资源的名称,第二个参数是资源的类型,第三是包在哪里寻找。

the first parameter is the name of the resource you are looking for, the second parameter is the type of the resource, the third is the package where to look for.

这篇关于如何从绘制根据自己的SQLite数据库名称来获取图像,然后在列表视图中显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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