how to get image from drawable according to their

2019-08-29 10:53发布

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

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 :

The method getResources() is undefined for the type CustomAdapterMatchSchedule

this is my code:

row_list_match_schedule.xml

<?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???

1条回答
Luminary・发光体
2楼-- · 2019-08-29 11:11

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.

查看更多
登录 后发表回答