2014-02-21 172 views
0

我是android開發人員,我有一個複選框的自定義列表視圖。這個layput還包含一個刪除按鈕。我想當我點擊一個特定行的所有項目被選中時,點擊刪除它被刪除。 問題是當我點擊刪除按鈕,我得到一個+1行值的列表。ListView按鈕點擊位置

最初我已經定義:

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); 
      } 
     } 
+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; 
} 

} 

適配器.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; 
} 

}