2

我有一个ListView. ListView 的行中有一个ImageView。我已将 onClick 属性附加到 ImageView 并且我的 onClick 函数是

public void alarmClick(View v)
{
    ImageView bell = (ImageView)v.findViewById(R.id.mEventImage);
    bell.setImageResource(R.drawable.bell_on);
    if(bell.getTag() == null)
    {
        bell.setImageResource(R.drawable.bell_on);
        bell.setTag(R.drawable.bell_on);
    }
    else if((Integer)bell.getTag() == R.drawable.bell_on)
    {
        bell.setImageResource(R.drawable.bell_off);
        bell.setTag(R.drawable.bell_off);
    }
    else
    {
        bell.setImageResource(R.drawable.bell_on);
        bell.setTag(R.drawable.bell_on);
    }
}

我正在尝试切换ImageView源,但是当我单击当时不在屏幕上的ImageView许多其他行时,它们也会被切换。ImageView这可能是因为视图在ListView. 任何建议如何为每个独特的行做。

适配器类

package com.example.drawer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class EventAdapter extends ArrayAdapter<EventList> {

    Context context;
    ArrayList<EventList> data;
    int layoutResourceId;

    public EventAdapter(Context context, int textViewResourceId, ArrayList<EventList> data) {
        super(context, textViewResourceId, data);
        this.context = context;
        this.data = data;
        this.layoutResourceId = textViewResourceId;
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        EventHolder holder =  null;
        if(row == null)
        {
            LayoutInflater inflator = ((Activity)context).getLayoutInflater();
            row = inflator.inflate(layoutResourceId, parent, false);
            holder = new EventHolder();
            holder.coord = (TextView)row.findViewById(R.id.eventCoor);
            holder.icon = (ImageView)row.findViewById(R.id.icon);
            holder.eventName = (TextView)row.findViewById(R.id.eventNam);
            holder.location = (TextView)row.findViewById(R.id.eventLoc);
            holder.time = (TextView)row.findViewById(R.id.eventTime);
            row.setTag(holder);

        }
        else
        {
            holder = (EventHolder)row.getTag();
        }
        EventList listItem = data.get(position);
        ImageHelper imageHelper = new ImageHelper();
        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ipl);
        Bitmap newIcon = imageHelper.getRoundedCornerBitmap(icon, 200);
        holder.coord.setText(listItem.coord);
        holder.eventName.setText(listItem.eventName);
        holder.location.setText(listItem.location);
        holder.time.setText(listItem.time);
        holder.icon.setImageBitmap(newIcon);
        holder.id = listItem.id;
        holder.day = listItem.day;
        //Log.d("ListView", "Executed");
        return row;

    }


    static class EventHolder
    {
        ImageView icon;
        TextView eventName;
        TextView location;
        TextView time;
        TextView coord;
        int id;
        int day;
    }
}
4

2 回答 2

0

是的,原因是视图被回收,并且相同的 ImageView 将出现在不同的行中。

您的列表项 - EventList,现在应该打开或关闭它的铃声,并在 getView 中适当地设置它。这意味着如果用户录制了第一个项目并打开了铃声,当其他项目重用该视图时,它会将其更改回关闭状态。

于 2013-08-15T07:05:53.427 回答
0

我建议您使用游标加载器并让您的 Activity/Fragment 实现 LoaderCallbacks,使用游标相当简单。使用 Sqlite 数据库或任何其他数据库管理您的 EventData,并使用 ContentProvider 检索您的数据

还让您的 EventAdapter 扩展 CursorAdapter 然后覆盖以下方法:-

public class EventAdapter extends CursorAdapter {

    int layoutResourceId;

    public EventAdapter(Context context, int textViewResourceId) 
    {
        super(context, textViewResourceId, data);
        this.layoutResourceId = textViewResourceId;
    }

    static class EventHolder
    {
       final ImageView icon;
       final ImageView bell;
       final TextView eventName;
       final TextView location;
       final TextView time;
       final TextView coord;
       final int id;
       final int day;

       EventHolder(View view)
        {
           coord = (TextView)view.findViewById(R.id.eventCoor);
           icon = (ImageView)view.findViewById(R.id.icon);
           bell = (ImageView) view.findViewById(R.id.mEventImage);
           eventName = (TextView)view.findViewById(R.id.eventNam);
           location = (TextView)view.findViewById(R.id.eventLoc);
           time = (TextView)view.findViewById(R.id.eventTime);
           id = -1;
           day = -1;
        }
    }
    //Re use the visible views
    @Override
    public void bindView(View view, Context context, Cursor cursor)
    {
        ViewHolder holder = (ViewHolder) view.getTag();
        EventList listItem = cursor.get(position);
        ImageHelper imageHelper = new ImageHelper();
        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ipl);
        Bitmap newIcon = imageHelper.getRoundedCornerBitmap(icon, 200);
        holder.coord.setText(listItem.coord);
        holder.eventName.setText(listItem.eventName);
        holder.location.setText(listItem.location);
        holder.time.setText(listItem.time);
        holder.icon.setImageBitmap(newIcon);
        holder.id = listItem.id;
        holder.day = listItem.day;

        //Checking Bell State Columnview if it is OFF or ON
        if(cursor.getString(YourActivity.COL_EVENTBELL_IMAGEVIEW_STATE).equals("on"))
        {
            holder.bell.setImageResource(R.drawable.bell_on);
        }
        else
        {
            holder.bell.setImageResource(R.drawable.bell_off);
        }

    }
    //Create new views, all to be visible at once
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    { 
        View view = LayoutInflater.from(context).inflate(layoutResourceId, parent, false);
        EventHolder holder = new EventHolder(view);

        view.setTag(holder);

        return view;
    }
}

在您的活动/片段中,您可以使用侦听器来更新铃声的状态,您的 AlarmClick() 将是这样的

public void alarmClick()
    {
    Cursor cursor = mEventAdapter.getCursor();
    ContentValues eventValues = new ContentValues();
    String event_bell_state = cursor.getString(YourActivity.COL_EVENT_BELL_IMAGEVIEW_STATE);
    String event_bell_id = cursor.getString(YourActivity.COL_EVENTBELL_ID);

    if(event_bell_state == "on")
    {
        eventValues.put(YourActivity.COL_EVENTBELL_IMAGEVIEW_STATE, "off");
    }
    else
    {
        eventValues.put(YourActivity.COL_EVENTBELL_IMAGEVIEW_STATE, "on");
    }

    String whereClause = YourActivity.COL_EVENTBELL_ID + " = ?";
    String[] selectionArgs = new String[]{String.valueOf(event_bell_id)};

    getActivity().getApplicationContext().getContentResolver().update(EventEntry.CONTENT_URI, eventValues, whereClause, selectionArgs);
    mEventAdapter.notifyDataSetChanged();
    getLoaderManager().restartLoader(EVENTLIST_LOADER, null, this);
    }
于 2015-06-08T09:04:21.127 回答