从Android版的ListView的ImageView [英] Android ImageView from ListView

查看:147
本文介绍了从Android版的ListView的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图传递给KEY_THUMB_URL到SingleMenuItemActivity.class显示图像的onclick列表视图。我已标记的区域//不工作。请纠正我的编码,我是pretty新到Android。

CustomizedListView.class

 公共类CustomizedListView延伸活动{
    //所有静态变量
    静态最终字符串URL =htt​​p://api.androidhive.info/music/music.xml;
    // XML节点键
    静态最后弦乐KEY_SONG =歌; //父节点
    静态最后弦乐KEY_ID =ID;
    静态最后弦乐KEY_TITLE =称号;
    静态最后弦乐KEY_ARTIST =艺术家;
    静态最后弦乐KEY_DURATION =时间;
    静态最后弦乐KEY_THUMB_URL =thumb_url;    ListView控件列表;
    LazyAdapter适配器;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        ArrayList的<&HashMap的LT;字符串,字符串>> songsList =新的ArrayList<&HashMap的LT;字符串,字符串>>();        XMLParser的解析器=新XMLParser的();
        字符串XML = parser.getXmlFromUrl(URL); // URL从XML获得
        文档的文档= parser.getDomElement(XML); //获得DOM元素        节点列表NL = doc.getElementsByTagName(KEY_SONG);
        //遍历所有歌曲节点&LT循环;宋>
        的for(int i = 0; I< nl.getLength();我++){
            //创建新的HashMap
            HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
            元素e =(元)nl.item(I)
            //将每个子节点的HashMap键=>值
            map.put(KEY_ID,parser.getValue(即KEY_ID));
            map.put(KEY_TITLE,parser.getValue(即KEY_TITLE));
            map.put(KEY_ARTIST,parser.getValue(即KEY_ARTIST));
            map.put(KEY_DURATION,parser.getValue(即KEY_DURATION));
            map.put(KEY_THUMB_URL,parser.getValue(即KEY_THUMB_URL));            //添加HashList到ArrayList的
            songsList.add(地图);
        }
        名单=(ListView控件)findViewById(R.id.list);        //通过传递XML数据的ArrayList获取适配器
        适配器=新LazyAdapter(这一点,songsList);
        list.setAdapter(适配器);
        // Click事件的单排列表
        list.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图<>母公司,观景,
                    INT位置,长的id){
                字符串title =((的TextView)view.findViewById(R.id.title))的getText()的toString()。
                字符串描述=((的TextView)view.findViewById(R.id.artist))的getText()的toString()。
                //开始新意图
                在意向=新意图(getApplicationContext(),SingleMenuItemActivity.class);                in.putExtra(KEY_TITLE,职称);
                in.putExtra(KEY_THUMB_URL,R.id.list_image); //修改由Nandeesh如告诉
                in.putExtra(KEY_ARTIST,说明);
                startActivity(在); }
        });
    }
}

SingleMenuItemActivity.class

 公共类SingleMenuItemActivity延伸活动{
    静态最后弦乐KEY_TITLE =称号;
    静态最后弦乐KEY_ARTIST =艺术家;
    静态最后弦乐KEY_THUMB_URL =thumb_url;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.single_list_item);        在意向= getIntent();        字符串title = in.getStringExtra(KEY_TITLE);
        字符串描述= in.getStringExtra(KEY_ARTIST);       // INT thumb_image = in.getIntExtra(KEY_THUMB_URL); //错误:在类型意图的方法getIntExtra(字符串,INT)不适用的参数(字符串)        TextView的lblName =(的TextView)findViewById(R.id.name_label);
        TextView的lblDesc =(的TextView)findViewById(R.id.description_label);       // ImageView的拇指=(ImageView的)findViewById(R.id.imageView1);        lblName.setText(职称);
       // thumb.setImageResource(thumb_image); //修改由Nandeesh如告诉
        lblDesc.setText(介绍);    }
}

LazyAdapter.class

 公共类LazyAdapter延伸BaseAdapter {    私人活动活动;
    私人的ArrayList<&HashMap的LT;字符串,字符串>>数据;
    私有静态LayoutInflater吹气= NULL;
    公共ImageLoader的ImageLoader的;    公共LazyAdapter(活动一,ArrayList的<&HashMap的LT;字符串,字符串> D 1和D){
        活性=一个;
        数据= D;
        吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ImageLoader的=新ImageLoader的(activity.getApplicationContext());
    }    公众诠释的getCount(){
        返回data.size();
    }    公共对象的getItem(INT位置){
        返回的位置;
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看VI = convertView;
        如果(convertView == NULL)
            VI = inflater.inflate(R.layout.list_row,NULL);        TextView的标题=(TextView中)vi.findViewById(R.id.title); //称号
        TextView的艺术家=(TextView中)vi.findViewById(R.id.artist); //歌手名
        TextView的持续时间=(TextView中)vi.findViewById(R.id.duration); //持续时间
        ImageView的thumb_image =(ImageView的)vi.findViewById(R.id.list_image); //拇指图像        HashMap的<字符串,字符串>歌=新的HashMap<字符串,字符串>();
        曲= data.get(位置);        //设置列表视图中的所有值
        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);
        返回VI;
    }
}


解决方案

在此行

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

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

那么你不应该使用字符串thumb_image = in.getStringExtra(KEY_THUMB_URL)因为putextra正在添加整数,你试图得到一个字符串

您应该使用


  

INT thumb_image = in.getIntExtra(KEY_THUMB_URL)

  thumb.setImageResource(thumb_image)


I'm trying to pass KEY_THUMB_URL to SingleMenuItemActivity.class to show Image onclick listview. I have marked those area //Not Working . Please correct my codings, I'm pretty new to 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;
    }
}

解决方案

In this line

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

R.id.list_image is not a drawable. Only R.drawable.[ ] can be used to setImageResource.

Then you should not use String thumb_image = in.getStringExtra(KEY_THUMB_URL) since putextra is adding a integer and you are trying to get a string

You should use

int thumb_image = in.getIntExtra(KEY_THUMB_URL)

thumb.setImageResource(thumb_image)

这篇关于从Android版的ListView的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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