ListView问题。当我更改项目的颜色(按下它)并向下滚动时,会出现另一个项目,颜色已更改

时间:2011-08-04 11:43:42

标签: android android-listview

我的自定义列表视图出现了一个非常奇怪的问题。

此Listview显示了许多名称和更多数据,每个项目都有一个onClick监听器,当我按下一个项目时,该项目的背景颜色为RED。 (arg1.setBackgroundColor(Color.RED);)

但有些东西不能正常工作,因为如果我滚动列表视图,另一个项目会在列表底部附近显示为彩色。

这是我的代码:

public class StartingSquad extends Activity {

public static List<Player> Players = new ArrayList<Player>();
public int selectedPosition=-1;
ListView l1;
private TextView TeamPowerValue=null;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MyApplication.updateStartingSquadOnPlayers(MyApplication.getPlayerTeam(),this);//actualizo los titulares

    Players=MyApplication.getPlayerTeam().getPlayers();

    setContentView(R.layout.startingsquad);
    TeamPowerValue = (TextView) findViewById(R.id.TeamPowerValue);
    l1 = (ListView) findViewById(R.id.ListView01);
    l1.setAdapter(new EfficientAdapter(this));

    TeamPowerValue.setText(""+MyApplication.getPlayerTeam().getPower());


    l1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            //Toast.makeText(getBaseContext(), "You clciked "+Players.get(arg2).getName(), Toast.LENGTH_SHORT).show();
            if (selectedPosition==-1) //si no hay ningun item seleccionado ya
            {
                arg1.setSelected(true);
                //arg1.setBackgroundResource(R.drawable.bg3);
                arg1.setBackgroundColor(Color.RED);

                selectedPosition=arg2;
            }
            else //si ya habiamos seleccionado un jugador
            {
                MyApplication.getPlayerTeam().changePlayerPositions(selectedPosition, arg2);
                selectedPosition=-1;
                MyApplication.updateStartingSquadOnPlayers(MyApplication.getPlayerTeam(),StartingSquad.this);//actualizo los titulares
                MyApplication.getPlayerTeam().calculatePower();
                TeamPowerValue.setText(""+MyApplication.getPlayerTeam().getPower());
                l1.setAdapter(new EfficientAdapter(StartingSquad.this));
            }
        }
    });
}

private static class EfficientAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

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

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

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


    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.startingsquadlistview, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.PlayerPosition);
            holder.text2 = (TextView) convertView.findViewById(R.id.PlayerName);
            holder.text3 = (TextView) convertView.findViewById(R.id.PlayerPower);
            holder.text4 = (TextView) convertView.findViewById(R.id.PlayerStartingSquad);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(Players.get(position).getPosition());
        holder.text2.setText(Players.get(position).getName());
        holder.text2.setTextColor(Players.get(position).NameColor);
        holder.text3.setText(""+Players.get(position).getPower());
        holder.text4.setText("XX");

        return convertView;
    }

    static class ViewHolder {
        TextView text;
        TextView text2;
        TextView text3;
        TextView text4;
    }
}

}

2 个答案:

答案 0 :(得分:1)

背景更改不在onItemSelected中,而是在适配器getView()中。

if(position == selectedPosition) {
    convertView.setBackgroundColor(context.getResources().getColor(R.color.red));
} else {
    convertView.setBackgroundColor(context.getResources().getColor(R.color.default));
}

答案 1 :(得分:0)

尝试删除此行:

l1.setAdapter(new EfficientAdapter(StartingSquad.this));

如果你不能,请告诉我原因?


还有一件事:编辑此功能:

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        View convertViews;
        if (convertView == null) {
            convertViews = mInflater.inflate(R.layout.startingsquadlistview, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.PlayerPosition);
            holder.text2 = (TextView) convertView.findViewById(R.id.PlayerName);
            holder.text3 = (TextView) convertView.findViewById(R.id.PlayerPower);
            holder.text4 = (TextView) convertView.findViewById(R.id.PlayerStartingSquad);

            convertViews.setTag(holder);
        } else {
            convertViews = convertView;
            holder = (ViewHolder) convertViews.getTag();
        }

        holder.text.setText(Players.get(position).getPosition());
        holder.text2.setText(Players.get(position).getName());
        holder.text2.setTextColor(Players.get(position).NameColor);
        holder.text3.setText(""+Players.get(position).getPower());
        holder.text4.setText("XX");

        return convertViews;
    }