使用ViewHolder设置图像和文本

时间:2014-02-14 16:56:30

标签: android performance android-layout android-listview android-fragments

我无法从getView()函数访问图像和音频数组。我已经评论过我尝试过的陈述。这些注释位于ViewHolder类之前的代码底部。

我尝试过使用以下语句:

holder.imageView.setImageResource(images[position]);
holder.textView.setText(audio[position]);

但是图像和音频阵列无法从该位置访问。如何从getView()访问它们?

    import java.util.ArrayList;

    import android.content.Context;
    import android.content.res.Resources;
    import android.graphics.Color;
    import android.graphics.Typeface;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.provider.MediaStore.Images;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;

    public class MyList extends Fragment {

        ListView list;
        MediaPlayer mp;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState)
        {
            mp=MediaPlayer.create(getActivity(), R.raw.wave1);

            return inflater.inflate(R.layout.mylistview, container, false);
        }

        @Override
        public void onStart() {

            super.onStart();
            list=(ListView)getView().findViewById(R.id.listView1);
            list.setAdapter(new MyAdapter(getActivity()));
            list.setCacheColorHint(Color.TRANSPARENT);

            list.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                        long arg3) 
                {
                    switch(position)
                        {
                        case 0:
                            mp.release();
                            mp=MediaPlayer.create(getActivity(), R.raw.wave1);
                            mp.start();                         
                        }

                 }
                });
        }
    }

        class MyAdapter extends BaseAdapter
        {
            ArrayList<SingleRow> list;
            Context context;
            public MyAdapter(Context c)
            {
                context=c;
                list=new ArrayList<SingleRow>();

            Resources res=c.getResources();
            String[] audio=res.getStringArray(R.array.audio);

                int[] images={ 
                        R.drawable.image,
                };
                for(int i=0;i<2;i++)
                {
                    list.add(new SingleRow(audio[i],images[i]));
                } 
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return list.size();
            }

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

                return list.get(position);
            }

            @Override
            public long getItemId(int position) {

                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                View row=convertView;
                MyViewHolder holder=null;

                if(row==null)
                {
                    LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    row=inflater.inflate(R.layout.list_rows, parent, false);
                    holder=new MyViewHolder(row);

                    row.setTag(holder);
                }

                else 
                {
                    holder=(MyViewHolder)row.getTag();
                }


/* The following two statements don't work, because images and the audio array are not accessible.

            holder.imageView.setImageResource(images[position]);
            holder.textView.setText(audio[position]);
*/
                return row;
            }

        class MyViewHolder
        {
            ImageView imageView;
            TextView textView;

            MyViewHolder(View v)
            {
                textView=(TextView)v.findViewById(R.id.textView1);
                imageView=(ImageView)v.findViewById(R.id.imageView1);
            }
        }

        class SingleRow
        {
            String audio;
            int image;
            SingleRow(String audio, int image)
            {
                this.audio=audio;
                this.image=image;
            }
        }

        }

修改

MyAdapter类现在如下所示。现在应用程序启动了,但片段是空的,根本没有视图。

class MyAdapter extends BaseAdapter
{
    ArrayList<SingleRow> list;
    Context context;

    int[] images1 = null;
    String[] audio = null;
    public MyAdapter(Context c) {

        context=c;
        list=new ArrayList<SingleRow>();
        //Initialize them...
        Resources res=c.getResources();
        audio = res.getStringArray(R.array.audio);
        int[] images1={ 

                R.drawable.image,
                };

        for(int i=0;i<2;i++)
        {
            list.add(new SingleRow(audio[i],images1[i]));
        } 

    }



    Resources res=context.getResources();
    String[] audio=res.getStringArray(R.array.audio);



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

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

        return list.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row=convertView;
        MyViewHolder holder=null;

        if(row==null)
        {
            LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=inflater.inflate(R.layout.list_rows, parent, false);
            holder=new MyViewHolder(row);

            row.setTag(holder);
        }

        else 
        {
            holder=(MyViewHolder)row.getTag();
        }

    holder.imageView.setImageResource(images1[position]);
    holder.textView.setText(audio[position]);

            return row;
    }

class MyViewHolder
{
    ImageView imageView;
    TextView textView;

    MyViewHolder(View v)
    {
        textView=(TextView)v.findViewById(R.id.textView1);
        imageView=(ImageView)v.findViewById(R.id.imageView1);
    }
}

class SingleRow
{
    String audio;
    int image;
    SingleRow(String audio, int image)
    {
        this.audio=audio;
        this.image=image;
    }
}

}

1 个答案:

答案 0 :(得分:2)

    class MyAdapter extends BaseAdapter
    {
        ArrayList<SingleRow> list;
        Context context;
        //Just declare them as class members
        int[] images = null;
        String[] audio = null;
        public MyAdapter(Context c)
        {
            context=c;
            list=new ArrayList<SingleRow>();
            //Initialize them...
            Resources res=c.getResources();
            audio = res.getStringArray(R.array.audio);
            images1= new int[] { R.drawable.image };
//Noticed you only have an element, 
//while I do not recommend to hardcode the positions, 
//I added it for compilation's sake
                list.add(new SingleRow(audio[0],images[0]));
            //... And the rest
    }