如何在我的BaseAdapter ListView中实现收藏夹按钮?

时间:2014-09-09 13:48:12

标签: java android android-layout android-listview baseadapter

我正在学习Android SDK,我需要一些建议 我有自定义ListView与BaseAdapter,我想实现一些新功能 - 收藏夹按钮。

我想要做的是,当我按下收藏夹按钮时,ListItem会转到列表的开头,收藏的图像会发生变化,所有这些内容都会保存在SharedPrefs中。

有人告诉我我需要做什么,让它有效吗?

我现有的代码:

row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
                android:id="@+id/layout_element_list"
    >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="150dp"
        android:padding="5dp"
        android:layout_height="150dp"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/radio" >
    </ImageView>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/label"
            android:paddingTop="20dp"
            android:layout_gravity="center_vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:textAlignment="center"
            android:text="RadioName"
            android:textColor="@color/color1"
            android:textSize="30dp" />
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <TextView
                android:id="@+id/label2"
                android:layout_gravity="center_vertical"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="fill_parent"
                android:textAlignment="center"
                android:text="Description.."
                android:textColor="@color/color1"
                android:textSize="15dp" />
            <ImageView
                android:id="@+id/favButton"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:padding="5dp"
                android:layout_height="fill_parent"
                android:layout_marginLeft="4px"
                android:layout_marginRight="10px"
                android:layout_marginTop="4px"
                android:src="@drawable/fav_off" >
            </ImageView>
        </LinearLayout>
    </LinearLayout>
</LinearLayout> 

BaseAdapter类:

public class RadioAdapter extends BaseAdapter
{

    ArrayList<RadioStation> myList = new ArrayList<RadioStation>();
    LayoutInflater inflater;
    Context context;

    public RadioAdapter(Context context, ArrayList<RadioStation> myList) {
        this.myList = myList;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }

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

    @Override
    public RadioStation getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        if(convertView == null) {
            convertView = inflater.inflate(R.layout.activity_menu_row, null);
            mViewHolder = new MyViewHolder();
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        mViewHolder.tvTitle = detail(convertView, R.id.label, myList.get(position).getTitle());
        mViewHolder.tvDesc  = detail(convertView, R.id.label2,  myList.get(position).getDescription());
        mViewHolder.ivIcon  = detail(convertView, R.id.icon,  myList.get(position).getImgResId());

        return convertView;
    }

    private TextView detail(View v, int resId, String text) {
        TextView tv = (TextView) v.findViewById(resId);
        tv.setText(text);
        return tv;
    }

    private ImageView detail(View v, int resId, int icon) {
        ImageView iv = (ImageView) v.findViewById(resId);
        iv.setImageResource(icon); //
        return iv;
    }

    private class MyViewHolder {
        TextView tvTitle, tvDesc;
        ImageView ivIcon;
    }
}

RadioStation课程:

public class RadioStation
{
    public String title;
    public String description;
    public int imgResId;

    //getters and setters  

    public static Comparator<RadioStation> comparatorByRadioName = new Comparator<RadioStation>()
    {
        @Override
        public int compare(RadioStation radioStation, RadioStation radioStation2)
        {
            String name1 = radioStation.getTitle().toLowerCase();
            String name2 = radioStation2.getTitle().toLowerCase();
            return name1.compareTo(name2);
        }
    };
}

ActivityListView:

public class ActivityMenuList extends Activity implements AdapterView.OnItemClickListener
{
    private ListView lvDetail;
    private Context context = ActivityMenuList.this;
    private ArrayList <RadioStation> myList = new ArrayList <RadioStation>();
    private String[] names = new String[] { "one", "two", "three" };
    private String[] descriptions = new String[] { "notset", "notset", "notset"};
    private int[] images = new int[] { R.drawable.one, R.drawable.two, R.drawable.three };

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        getWindow().setBackgroundDrawableResource(R.drawable.bg1);
        setContentView(R.layout.activity_menu_list);
        lvDetail = (ListView) findViewById(R.id.list);
        lvDetail.setOnItemClickListener(this);
        getDataInList();
        lvDetail.setAdapter(new RadioAdapter(context, myList));
    }
    private void getDataInList() {
        for(int i=0;i<3;i++) {
            RadioStation ld = new RadioStation();
            ld.setTitle(names[i]);
            ld.setDescription(descriptions[i]);
            ld.setImgResId(images[i]);
            myList.add(ld);
        }
        Collections.sort(myList, RadioStation.comparatorByRadioName);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
    {
        String item = names[i];
        Intent e = new Intent(ActivityMenuList.this, ActivityRadioStation.class);
        Bundle data = new Bundle();
        data.putString("radiostation",item);
        e.putExtras(data);
        startActivity(e);
    }
}

1 个答案:

答案 0 :(得分:2)

你需要做很多改变。让我们从基础开始吧。 在RadioStation中为喜欢的状态添加一个布尔值。

public boolean isFavorite;

接下来在你的getView上添加收藏夹按钮点击监听器(添加它对视图的引用,但这次让它保持简单)

public class RadioAdapter extends BaseAdapter
{
    ArrayList<RadioStation> myList = new ArrayList<RadioStation>();
    LayoutInflater inflater;
    Context context;
    ListView mListview;

    public RadioAdapter(Context context, ArrayList<RadioStation> myList, ListView list) {
        this.myList = myList;
        this.context = context;
        mListView = list;
        inflater = LayoutInflater.from(this.context);
    }

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

    @Override
    public RadioStation getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder mViewHolder;

        if(convertView == null) {
            convertView = inflater.inflate(R.layout.activity_menu_row, null);
            mViewHolder = new MyViewHolder();
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        mViewHolder.tvTitle = detail(convertView, R.id.label, myList.get(position).getTitle());
        mViewHolder.tvDesc  = detail(convertView, R.id.label2,  myList.get(position).getDescription());
        mViewHolder.ivIcon  = detail(convertView, R.id.icon,  myList.get(position).getImgResId());
        convertView.findViewById(R.id.favButton).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                myList.get(position).isFavorite=! myList.get(position).isFavorite;
                //reorder mlist
                notifyDataSetChanged();
                //mListView. smoothscroll here
            }
        });

        ((ImageView) convertView.findViewById(R.id.favButton)).setImageResource(myList.get(position).isFavorite?R.drawable.favoriteOn:R.drawable.favoriteOff);
        return convertView;
    }

    private TextView detail(View v, int resId, String text) {
        TextView tv = (TextView) v.findViewById(resId);
        tv.setText(text);
        return tv;
    }

    private ImageView detail(View v, int resId, int icon) {
        ImageView iv = (ImageView) v.findViewById(resId);
        iv.setImageResource(icon); //
        return iv;
    }

    private class MyViewHolder {
        TextView tvTitle, tvDesc;
        ImageView ivIcon;
    }
}

我留下评论你应该对听众做些什么。你应该可以从这里继续。

创建适配器时,将列表作为构造函数的最后一个参数传递。

已编辑:删除了界面。不需要在这里使用它。