0

我正在尝试将 KEY_THUMB_URL 传递给 SingleMenuItemActivity.class 以显示 Image onclick listview。我已经标记了这些区域 //Not Working 。请更正我的编码,我对 android 很陌生。

CustomizedListView.class

    public class CustomizedListView extends Activity {
    // All static variables
    static final String URL = "http://api.androidhive.info/music/music.xml";
    // XML node keys
    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "artist";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";

    ListView list;
    LazyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }


        list=(ListView)findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, songsList);        
        list.setAdapter(adapter);


        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {


                String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
                String description = ((TextView) view.findViewById(R.id.artist)).getText().toString();


                // Starting new intent
                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);

                in.putExtra(KEY_TITLE, title);
                in.putExtra(KEY_THUMB_URL, R.id.list_image); //Modified as told by Nandeesh 
                in.putExtra(KEY_ARTIST, description);
                startActivity(in);  }


        });     
    }   
}

SingleMenuItemActivity.class

    public class SingleMenuItemActivity  extends Activity {


    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "artist";  
    static final String KEY_THUMB_URL = "thumb_url";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_list_item);

        Intent in = getIntent();

        String title = in.getStringExtra(KEY_TITLE);
        String description = in.getStringExtra(KEY_ARTIST);

       // int thumb_image = in.getIntExtra(KEY_THUMB_URL); // ERROR : The method getIntExtra(String, int) in the type Intent is not applicable for the arguments (String)

        TextView lblName = (TextView) findViewById(R.id.name_label);
        TextView lblDesc = (TextView) findViewById(R.id.description_label);

       // ImageView thumb = (ImageView) findViewById(R.id.imageView1); 

        lblName.setText(title);
       // thumb.setImageResource(thumb_image); //modified as told by Nandeesh                    
        lblDesc.setText(description);

    }
}

LazyAdapter.class

    public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

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

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
        TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);

        // Setting all values in listview
        title.setText(song.get(CustomizedListView.KEY_TITLE));
        artist.setText(song.get(CustomizedListView.KEY_ARTIST));
        duration.setText(song.get(CustomizedListView.KEY_DURATION));
        imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
        return vi;
    }
}
4

2 回答 2

1

因为您没有添加LazyAdapter代码,所以我尝试猜测并执行此操作:

     list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {


HashMap<String, String> map = yourAdapter.getItem(position); // create an Adapter in your `onCreate` . yourAdapter = new LazyAdapter(this, songsList); 
                    // Starting new intent
                    Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                        in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
                        in.putExtra(KEY_THUMB_URL, map.get(KEY_THUMB_URL)); //make sure this is putExtra(String name, String value);
                        in.putExtra(KEY_ARTIST, map.get(KEY_ARTIST));
                        startActivity(in);  }


            });     

奖励:在CustomizedListView这样做LazyAdapter adapter = new LazyAdapter(this, songsList);

所以setOnItemClickListenerHashMap<String, String> map = adapter.getItem(position); // get data of the clicked item. 在 LazyAdapter 中将你的方法更改getItem

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

在您SingleMenuItemActivity.class从 Intent Extra 获取的 URL 图像中。像您一样下载它LazyAdapter并设置为ImageView

于 2012-08-12T12:04:36.910 回答
0

在这一行

in.putExtra(KEY_THUMB_URL, R.id.list_image);

R.id.list_image 不是可绘制的。只有 R.drawable.[ ] 可用于 setImageResource。

那么你不应该使用String thumb_image = in.getStringExtra(KEY_THUMB_URL),因为 putextra 正在添加一个整数并且你正在尝试获取一个字符串

你应该使用

int thumb_image = in.getIntExtra(KEY_THUMB_URL)

thumb.setImageResource(thumb_image)
于 2012-08-12T08:22:25.500 回答