.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;
}
}
これを修正する方法はありますか?