单击按钮上的ListView位置

时间:2014-02-21 09:04:20

标签: android

我是Android开发人员,我有一个带有复选框的自定义列表视图。此layput还包含一个删除按钮。我想当我点击cheakbox时,选择了特定行中的所有项目,并且点击删除它将被删除。 问题是,当我点击删除按钮时,我得到一个+1行值列表。

最初我已定义:

int position=0;

btmsgdelete.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        System.out.println("request send for message delete");
        for(Message msg:almsg) {
            if(msg.isSelected()) {
            CheckBox chk = (CheckBox)findViewById(R.id.checkBox1);
            System.out.println("msg is selected");
            msgid=almsg.get(position).getEmpid();
            System.out.println(msgid);
            empname=almsg.get(position).getEmpname();
            System.out.println(empname);
            msgheader=almsg.get(position).getHeader();
           System.out.println(msgheader);
            }
        }

1 个答案:

答案 0 :(得分:0)

试试此示例代码

public class MainActivity extends Activity {

private int textViewResourceId;
private ArrayList<CompareListData> searchResults;
private ListView lst;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    searchResults = GetSearchResults();

    lst = (ListView) findViewById(R.id.list);
    findViewById(R.id.delete).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            for (int i = 0; i < searchResults.size(); i++) {

                if (searchResults.get(i).getSelected()) {

                    searchResults.remove(i);

                }
            }
            lst.setAdapter(new Adapter(MainActivity.this, textViewResourceId,
                    searchResults));

        }
    });
    System.out.println("size  " + searchResults.size());

    lst.setAdapter(new Adapter(MainActivity.this, textViewResourceId,
            searchResults));
}

private ArrayList<CompareListData> GetSearchResults() {
    ArrayList<CompareListData> results = new ArrayList<CompareListData>();

    CompareListData sr1 = new CompareListData();
    sr1.setName("John Smith");

    results.add(sr1);

    sr1 = new CompareListData();
    sr1.setName("Jane Doe");

    results.add(sr1);

    sr1 = new CompareListData();
    sr1.setName("Steve Young");

    results.add(sr1);

    sr1 = new CompareListData();
    sr1.setName("Fred Jones");
    results.add(sr1);

    return results;

}

 } 

CompareListData.java

public class CompareListData {

String name;
boolean selected;

public String getName() {
    return name;
}

public void setName(String Name) {
    name = Name;
}

public boolean getSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    this.selected = selected;
}

}

Adapter.java

public class Adapter extends BaseAdapter{

public static int count = 0;
public LayoutInflater inflater;
public static ArrayList<CompareListData> selectedId;
public ArrayList<CompareListData> listObjects;
Context contex;

public Adapter(Context context, int textViewResourceId,
        ArrayList<CompareListData> objects) {

     super();
      this.inflater =    (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.listObjects = objects;
    this.contex = context;

}
public static class ViewHolder
{   
    TextView txtViewLoanName;
    CheckBox chkSelected;
}


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

    View view = null;
    if(convertView==null)
    {
         final ViewHolder holder  = new ViewHolder();
         view = inflater.inflate(R.layout.row_comparelist, null);

        holder.txtViewLoanName= (TextView) view.findViewById(R.id.rowcomparelist_tv_loanname);

        holder.chkSelected= (CheckBox) view.findViewById(R.id.rowcomparelist_chk_selected);
        holder.chkSelected.setId(position);

        holder.chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {


            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                CompareListData element = (CompareListData) holder.chkSelected.getTag();
                element.setSelected(buttonView.isChecked());

            }
        });
        view.setTag(holder);
        holder.chkSelected.setTag(listObjects.get(position));


    }
    else{
        view = convertView;
        ((ViewHolder) view.getTag()).chkSelected.setTag(listObjects.get(position));

    }


    ViewHolder holder = (ViewHolder) view.getTag();
    holder.txtViewLoanName.setText(listObjects.get(position).getName());
    holder.chkSelected.setChecked(listObjects.get(position).getSelected());
    return view;
}


public int getCount() {

    return listObjects.size();
}


public Object getItem(int position) {

    return listObjects.get(position);
}


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

}