开发者

Issue With android ListView

I have added a listadapter which contains a checkbox, imageview, two textviews and a button to a listview. The problem is when I click on single checkbox for selecting more than one is getting selected. Below is my code for the Adapter. Please tell me any thing wrong in the code.

package com.mgm.schannel.lazylist;

import java.util.ArrayList;
import java.util.List;
import com.mgm.schannel.R;
import com.mgm.schannel.fonts.FontUtil;
import com.mgm.schannel.parser.inbox.Child;
import com.mgm.schannel.parser.inbox.Messages;
import com.mgm.schannel.ui.inboxActivity;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class InboxAdapter extends BaseAdapter {
  public ArrayList<Object> getSelectedItems() {
    return selectedItems;
  }

  private ArrayList<Object> selectedItems;
  private inboxActivity activity;
  private List<Object>  data;
  private static LayoutInflater inflater = null;
  private TextView MessageCount;
  FontUtil fontsList;
  private boolean showM开发者_运维问答oveButton;
  int positionItem;
  public TextView getMessageCount() {
    return MessageCount;
  }

  public InboxAdapter(inboxActivity a, List<Object> results, FontUtil fontslist) {
    activity = a;
    data = results;
    fontsList = fontslist;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    showMoveButton = false;
    selectedItems = new ArrayList<Object>();
  }

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

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

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

  public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    Object inboxObject = data.get(position);
    positionItem = position;

    if(convertView == null)
      vi = inflater.inflate(R.layout.inboxlist, null);




    Button btnmove = (Button)vi.findViewById(R.id.btnmovehere);

    if(position % 2 == 0)
    {
      vi.setBackgroundColor(Color.parseColor("#ebebeb"));
    }
    else
    {
      vi.setBackgroundColor(Color.parseColor("#f4f4f4"));
    }

    if(inboxObject instanceof Messages)
    {
      ImageView imgicon = (ImageView)vi.findViewById(R.id.inboxicon);
      imgicon.setImageResource(R.drawable.message);

      TextView text = (TextView)vi.findViewById(R.id.txtCaption);
      text.setTextSize(16.f);
      text.setTextColor(Color.BLACK);
      text.setTypeface(fontsList.getTitilliumMaps29L002());
      text.setText(((Messages)inboxObject).getSender());

      text = (TextView)vi.findViewById(R.id.txtMessage);
      text.setTextSize(10.f);
      text.setTextColor(Color.BLACK);
      text.setTypeface(fontsList.getTitilliumMaps29L002());
      text.setText(((Messages)inboxObject).getMessage());
      btnmove.setVisibility(View.INVISIBLE);

      CheckBox cbSelected = (CheckBox)vi.findViewById(R.id.chkSelection);
      cbSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          // TODO Auto-generated method stub
          if(buttonView.isChecked())
          {
            getSelectedItems().add(data.get(positionItem));
          }

        }
      });


    }
    else if(inboxObject instanceof Child)
    {
      TextView text = (TextView)vi.findViewById(R.id.txtCaption);
      text.setTextSize(14.f);
      text.setTextColor(Color.BLACK);
      text.setTypeface(fontsList.getTitilliumMaps29L002());
      Log.e("Folder Name", ((Child)inboxObject).getFolderName());
      text.setText(((Child)inboxObject).getFolderName());
      ImageView imgicon = (ImageView)vi.findViewById(R.id.inboxicon);
      imgicon.setImageResource(R.drawable.folder);
      text = (TextView)vi.findViewById(R.id.txtMessage);
      text.setVisibility(View.GONE);
      if(showMoveButton)
      {
        btnmove.setVisibility(View.VISIBLE);
      }
      else
      {
        btnmove.setVisibility(View.INVISIBLE);
      }
      CheckBox cbSelected = (CheckBox)vi.findViewById(R.id.chkSelection);
      cbSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          // TODO Auto-generated method stub
          if(buttonView.isChecked())
          {
            getSelectedItems().add(data.get(positionItem));
          }

        }
      });
    }

    return vi;
  }

  public void showMove(boolean value) {
    showMoveButton = value;
  }


}


You are always referencing the same exact checkbox object. You need to reference a different one each time, or create a new checkbox each time.

CheckBox cbSelected=(CheckBox)vi.findViewById(R.id.chkSelection);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜