只有最后一行改变了,当我点击listView中的imageView时,android

时间:2015-03-19 00:59:55

标签: android listview android-listview

我有一个listView,并且在listView中,每行都有一个imageView,我想在点击imageView时更改图像(例如白色开始 - >点击 - >黑色星星)。

我有一个适配器来显示每个视图。

问题: 1.当我单击任何行中的imageView时,只有最后一个视图发生了变化,我测试了最后一个视图中只有数据(我设置0表示白星,1表示黑星)。 2.当我有很多行时,当我向下滚动一点,然后单击中间部分的imageView时,listView将返回到列表的顶部。

这是我的适配器代码。

public View getView(final int position, View convertView, ViewGroup parent) {
    View rowView = convertView;

    expense = expenseList.get(position);

    if(rowView == null){
        viewHolder = new ViewHolder();
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        rowView = inflater.inflate(resource, parent, false);

        viewHolder.expenseCurrency = (TextView) rowView.findViewById(R.id.expenseAdaptor_currency);
        viewHolder.expenseCategory= (TextView) rowView.findViewById(R.id.expenseAdaptor_category);
        viewHolder.expenseAmount = (TextView) rowView.findViewById(R.id.expenseAdaptor_amount);
        viewHolder.expenseDate = (TextView) rowView.findViewById(R.id.expenseAdaptor_date);
        viewHolder.expenseName = (TextView) rowView.findViewById(R.id.expenseAdaptor_Name);
        viewHolder.expenseFlag = (ImageView) rowView.findViewById(R.id.expenseAdaptor_flag);
        rowView.setTag(viewHolder);

    }else{
        viewHolder = (ViewHolder) rowView.getTag();
    }

    viewHolder.expenseCurrency.setText(expense.getCurr());
    viewHolder.expenseCategory.setText(expense.getCat());
    viewHolder.expenseAmount.setText(expense.getCost().toString());
    viewHolder.expenseName.setText(expense.getName());

    if (expense.getFlag()==0){
        viewHolder.expenseFlag.setImageResource(R.drawable.ic_action_toggle_star_outline);
    }
    else{
        viewHolder.expenseFlag.setImageResource(R.drawable.ic_action_toggle_star);
    }


    SimpleDateFormat defaultExpenseDate = new SimpleDateFormat("MM/dd/yyyy",Locale.CANADA);
    if(expense.getDate() == null){
        viewHolder.expenseDate.setText("No Date");
    }else{
        viewHolder.expenseDate.setText(defaultExpenseDate.format(expense.getDate()));
    }

    viewHolder.expenseFlag.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v){
            //stars[position]=!stars[position];
            expense.setFlag(1-expense.getFlag());
            notifyDataSetChanged();
            notifyDataSetInvalidated();
        }
    });

======================这是我的ListViewActivity代码======

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    Intent intent=new Intent();
    claimName=this.getIntent().getExtras().getString("claimName");
    setContentView(R.layout.activity_expense_list);
    FileManager.initializeSaver(this);
    expenseListView = (ListView) findViewById(R.id.CurrentExpenseList);
    expenseList = ClaimListController.getClaimList().getClaim(claimName).getExpenseList();

    expenseAdaptor = new ExpenseListAdaptor(this,R.layout.expense_list_adaptor,expenseList.toArrayList());
    expenseAdaptor.notifyDataSetChanged();
    expenseListView.setAdapter(expenseAdaptor);

    registerForContextMenu(findViewById(R.id.CurrentExpenseList));
    ListView listView = (ListView) findViewById(R.id.CurrentExpenseList);
    //set_on_click();

}

3 个答案:

答案 0 :(得分:0)

我认为你的问题是这个

expense = expenseList.get(position);

用我的getview替换你的getview并进行任何其他需要的更改。

public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;

if(rowView == null){
    viewHolder = new ViewHolder();
    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
    rowView = inflater.inflate(resource, parent, false);

    viewHolder.expenseCurrency = (TextView) rowView.findViewById(R.id.expenseAdaptor_currency);
    viewHolder.expenseCategory= (TextView) rowView.findViewById(R.id.expenseAdaptor_category);
    viewHolder.expenseAmount = (TextView) rowView.findViewById(R.id.expenseAdaptor_amount);
    viewHolder.expenseDate = (TextView) rowView.findViewById(R.id.expenseAdaptor_date);
    viewHolder.expenseName = (TextView) rowView.findViewById(R.id.expenseAdaptor_Name);
    viewHolder.expenseFlag = (ImageView) rowView.findViewById(R.id.expenseAdaptor_flag);
    rowView.setTag(viewHolder);

}else{
    viewHolder = (ViewHolder) rowView.getTag();
}

EXPENSELISTITEM expense = expenseList.get(position);  ///<-- add I'm not sure what you named that class so change EXPENSELISTITEM to whatever you call it
viewHolder.expense = expense ;  ///<-- make sure the item is stored with the view holder too.

viewHolder.expenseCurrency.setText(expense.getCurr());
viewHolder.expenseCategory.setText(expense.getCat());
viewHolder.expenseAmount.setText(expense.getCost().toString());
viewHolder.expenseName.setText(expense.getName());

if (expense.getFlag()==0){
    viewHolder.expenseFlag.setImageResource(R.drawable.ic_action_toggle_star_outline);
}
else{
    viewHolder.expenseFlag.setImageResource(R.drawable.ic_action_toggle_star);
}


SimpleDateFormat defaultExpenseDate = new SimpleDateFormat("MM/dd/yyyy",Locale.CANADA);
if(expense.getDate() == null){
    viewHolder.expenseDate.setText("No Date");
}else{
    viewHolder.expenseDate.setText(defaultExpenseDate.format(expense.getDate()));
}

viewHolder.expenseFlag.setOnClickListener(new OnClickListener()
{
    public void onClick(View v){
        //stars[position]=!stars[position];
        expense.setFlag(1-expense.getFlag());
        notifyDataSetChanged();
    }
});
return rowView;
}

答案 1 :(得分:0)

更改onClick上的图像背景。您唯一需要确保您拥有所点击的项目位置及其各自的数据。

if(rowView == null){
....
viewHolder.expenseFlag = (ImageView) rowView.findViewById(R.id.expenseAdaptor_flag);
viewHolder.expenseFlag.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (int) v.getTag();
                    Expense expense = expenseList.get(position);
                    expense.setType(expense.getType() - 1);
                    notifyDataSetChanged();
                }
            });
....
} else{
viewHolder = (ViewHolder) rowView.getTag();
}

viewHolder.expenseFlag.setTag(position);
.....

if (expense.getFlag()==0){
        viewHolder.expenseFlag.setImageResource(R.drawable.ic_action_toggle_star_outline);
    }
    else{
        viewHolder.expenseFlag.setImageResource(R.drawable.ic_action_toggle_star);
    }

答案 2 :(得分:0)

感谢@bharat,你的想法是正确的,你的代码确实有效,我添加了一点来解决图像更改问题。

供其他人参考:

public void onClick(View v){
            ImageView i= (ImageView) v;
            int pos = (Integer) v.getTag();
            Expense expense1 = expenseList.get(pos);
            //stars[position]=!stars[position];
            expense1.setFlag(1-expense1.getFlag());
            if (expense1.getFlag()==0){
                i.setImageResource(R.drawable.ic_action_toggle_star_outline);
            }
            else{
                i.setImageResource(R.drawable.ic_action_toggle_star);
            }
            //notifyDataSetChanged();
            //notifyDataSetInvalidated();