1

我正在尝试使用 .setImageResource(identifier) 更改图像资源,但是当我使用我现在正在使用的变量时它没有显示出来。当我自己填写图像的名称时,它将起作用。

这是 Index.java 文件:

 package com.example.whs;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Index extends Activity {

    public static final Object TITLE = "title";
    public static final Object SUBTITLE = "subtitle";
    public static final Object THUMBNAIL = "thumbnail";
    protected static final String POSITION = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        buildMenu();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.index, menu);
        return true;
    }

    //Builds the menu for listview
    public void buildMenu(){
        ArrayList<HashMap<String, String>> menu = new ArrayList<HashMap<String, String>>();
        //Arrays for info
        String[] menuTitleArray = {"Updates", "Gallerij"}; 
        String[] menuSubtitleArray = {"Bekijk updates", "Bekijk foto's en geef reacties", "Bekijk de updates"};
        String[] menuThumbnailArray = {"updates", "gallery"};
        for(int i=0; i < menuTitleArray.length; i++){
            // Build Hashmap for the item
            HashMap<String, String> item = new HashMap<String, String>();
            item.put((String) TITLE, menuTitleArray[i]);
            item.put((String) SUBTITLE, menuSubtitleArray[i]);
            item.put((String) THUMBNAIL, menuThumbnailArray[i]);
            menu.add(item);
        }


        // Add adapter to the list
        MenuAdapter adapter = new MenuAdapter(this, menu);
        ListView list = (ListView)findViewById(R.id.list);
        list.setAdapter(adapter);



        // Initialize the click event
        list.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                switch(position){
                case 0:
                    Intent intent = new Intent(Index.this, Updates.class);
                    startActivity(intent);
                }
            }
        });

    }
}

这是 MenuAdapter.java 文件:

package com.example.whs;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.graphics.drawable.Drawable;
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 MenuAdapter extends BaseAdapter{
    // Define variables
    ArrayList<HashMap<String, String>> data;
    Activity activity;
    private LayoutInflater inflater=null;

    public MenuAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data = d;
        inflater = LayoutInflater.from (a);
    }

    @Override
    public int getCount() {
        return data.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null); 
        vi.setBackgroundResource(activity.getResources().getIdentifier("list_selector", "drawable", Index.class.getPackage().getName()));
        // Focus on the parts that have to be changed
        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView subtitle = (TextView)vi.findViewById(R.id.subtitle); // subtitle
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

        // Get the info from the hashmap with the arraylist position
        HashMap<String, String> item = new HashMap<String, String>();
        item = data.get(position);
        String name = (String) Index.THUMBNAIL;
        // Look for the image
        int identifier = activity.getResources().getIdentifier(name, "drawable", Index.class.getPackage().getName());

        // Setting all values in listview
        title.setText(item.get(Index.TITLE));
        subtitle.setText(item.get(Index.SUBTITLE));
        thumb_image.setImageResource(identifier);
        return vi;
    }

}

有没有办法解决这个问题?

4

1 回答 1

0

嘿,我认为问题是@THUMNAIL 声明

或者,您可以尝试以下方法

 private Integer[] mThumbIds = {           
     R.drawable.sample_2, R.drawable.sample_3,           
    R.drawable.sample_4, R.drawable.sample_5,      
   R.drawable.sample_6, R.drawable.sample_7,    
 };

thumb_image.setImageResource(mThumbIds[位置]);

如果图像来自服务器,您可以将其转换为位图并将位图设置为图像视图。

于 2013-02-27T17:53:08.607 回答