2012-04-05 136 views
0

我有複選框和「保存」按鈕的列表視圖。
無論複選框我檢查,然後點擊保存按鈕,我想保存與這些檢查行插入數據庫 這裏的數據是我的代碼Android-在列表視圖中保存複選框狀態

public class CardDetailsAdapter extends BaseAdapter {  
private static ArrayList<CardDetails> searchArrayList;   
private LayoutInflater mInflater;  

private boolean[] itemChecked = new boolean[100]; 

public CardDetailsAdapter(Context context, ArrayList<CardDetails> results) { 
    map = new HashMap<Integer, Boolean>(); 
    searchArrayList = results; 
    mInflater = LayoutInflater.from(context); 
} 

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

public Object getItem(int position) { 
    return searchArrayList.get(position); 
} 

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

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

    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.welcomelistview, null); 
     holder = new ViewHolder(); 
     holder.bankName = (TextView) convertView.findViewById(R.id.BankNameID); 
     holder.messages = (TextView) convertView.findViewById(R.id.messageID); 
     holder.chkbox = (CheckBox) convertView.findViewById(R.id.chkBoxID); 
     System.out.println("is checked==> " + holder.chkbox.isChecked()); 

     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    }   
    holder.bankName.setText(searchArrayList.get(position).getBankName()); 
    holder.messages.setText(searchArrayList.get(position).getMessages());  
    holder.chkbox.setChecked(false); 
    // save position and checking status into tag 
    holder.chkbox.setTag(position); 
    holder.chkbox.setOnClickListener(new OnClickListener() {    
     public void onClick(View v) { 
      HashMap<Integer, Boolean> mSimpleAdapter = new HashMap<Integer, Boolean>(); 
      int p = (Integer) (v.getTag()); 
       if (holder.chkbox.isChecked() == true) { 
        itemChecked[Integer.valueOf(position)] = holder.chkbox.isChecked(); 
       }else{ 
        itemChecked[Integer.valueOf(position)] = holder.chkbox.isChecked(); 
       } 

      mSimpleAdapter.put(p, itemChecked[position]); 
      holder.chkbox.setChecked(itemChecked[position]); 
     } 
    }); 
    return convertView; 
} 

static class ViewHolder { 
    TextView bankName; 
    TextView messages; 
    CheckBox chkbox; 
}} 

和活動類

import com.ispl.CFSMS.helper.CardDetailsAdapter; 
    public class WelcomePage extends Activity{ 
    CardDetailsAdapter mListAdapter; 
    ArrayList<Integer> arList = new ArrayList<Integer>(); 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welcomepage); 
     final ArrayList<CardDetails> cardResults = GetCardDetails(); 
     final ListView welcomelist = (ListView) findViewById(R.id.welcomelistID);   
     welcomelist.setAdapter(new CardDetailsAdapter(this, cardResults));      
     welcomelist.setOnItemLongClickListener(new OnItemLongClickListener() { 
      public boolean onItemLongClick(AdapterView<?> a, View v, int position, long id) { 
       Object o = welcomelist.getItemAtPosition(position); 
       CardDetails fullObject = (CardDetails)o; 
       Intent r=new Intent(WelcomePage.this , CardMenuActivity.class); 
       r.putExtra("BankName", fullObject.getBankName()); 
       r.putExtra("CardNumber", fullObject.getCardNumber()); 
       startActivityForResult(r, position); 
       return false; 
      } 
     }); 

     CheckBox checkbox = (CheckBox)findViewById(R.id.chkBoxID); 
     Button saveTODbBtn = (Button)findViewById(R.id.saveID); 
     saveTODbBtn.setOnClickListener(new OnClickListener() {    
      public void onClick(View v) {    
       //get the checkbox status and save related details into database 
       Intent intent=new Intent(WelcomePage.this, BankProjectActivity.class); 
       startActivity(intent); 
      } 
     }); 

    }  
private ArrayList<CardDetails> GetCardDetails(){ 
     ArrayList<CardDetails> carddetailresults = new ArrayList<CardDetails>();   
     CardDetails sr1 = new CardDetails(); 

     sr1.setBankName("Select All"); 
     sr1.setCardNumber(""); 
     sr1.setMessages(""); 
     carddetailresults.add(sr1); 

     sr1 = new CardDetails();  
     sr1.setBankName("HDFC Bank" +" "+ "xxxxx1234"); 
     sr1.setMessages("Messages: " + "10"); 
     carddetailresults.add(sr1); 

     sr1 = new CardDetails(); 
     sr1.setBankName("ICICI" + " "+ "xxxxx2134"); 
     sr1.setMessages("Messages: 5"); 
     carddetailresults.add(sr1); 

     sr1 = new CardDetails(); 
     sr1.setBankName("HSBC" + " "+ "xxxxx8796"); 
     sr1.setMessages("Messages: 2"); 
     carddetailresults.add(sr1); 

     return carddetailresults; 
}} 

在Adaptar類我我能夠獲得狀態。但我如何保存這些狀態並獲得活動類中的所有狀態,以便在單擊保存按鈕時保存與那些已檢查行相關的所有數據......?

+0

試試這個站點http://automateddeveloper.blogspot 。在/ 2010/09 /一切任您需要到專門about.html – Bhavin 2012-04-05 07:17:00

回答

3

喜在GetCardDetails()方法中添加適配器sr1.setSelected(true)sr1.setSelected(false)

添加以下

public static ArrayList<Boolean> itemChecked1 = null; 
    public CardDetailsAdapter(Context context, ArrayList<CardDetails> results) { 
    super(context, R.layout.welcomelistview, R.id.BankNameID, results); 
    searchArrayList = results; 
    getContext = context; 
    mInflater = LayoutInflater.from(context) ; 
    itemChecked1 = new ArrayList<Boolean>(); 
    for (int i = 0; i < results.size(); i++) { 
     itemChecked1.add(i, results.get(i).isSelected()); // initializes all items value with false   
    } 
} 

在getview

holder.chkbox.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      CheckBox cb = (CheckBox) v; 
      CardDetails planet = (CardDetails) cb.getTag(); 
      //planet.setSelected(cb.isChecked()); 
      if (holder.chkbox.isChecked()) { 
       itemChecked1.set(position, true); 
       holder.chkbox.setChecked(true); 
       planet.setSelected(true); 
      } else if (!holder.chkbox.isChecked()) { 
       itemChecked1.set(position, false); 
       holder.chkbox.setChecked(false); 
       planet.setSelected(false); 
      } 
     } 
    }); 
0
public View getView(final int position, View convertView,ViewGroup paramViewGroup) 
{ 
    final ViewHolder holder; 
    LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
    if (convertView == null) 
    { 
     convertView=inflater.inflate(R.layout.addressbook, null);holder = new ViewHolder(); 
     holder.adddress_name=(TextView)convertView.findViewById(R.id.addressbook_name); 
     holder.adddress_number=(TextView)convertView.findViewById(R.id.address_number); 
     holder.adddress_name.setText(adddressbook_name.get(position).toString().trim()) 
     holder.adddress_number.setText(addressbook_number.get(position).toString().trim()); 
     holder.addressbook_check=(CheckBox)convertView.findViewById(R.id.address_check);convertView.setTag(holder); 
    } 
    else 
    { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.addressbook_check.setId(position); 

    holder.addressbook_check.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       CheckBox cb = (CheckBox) v; 
       int id = cb.getId(); 
       if (thumbnailsselection[id]) 
       { 
        cb.setChecked(false); 
        thumbnailsselection[id] = false; 
       } 
       else 
       { 
        cb.setChecked(true); 
        thumbnailsselection[id] = true; 
       } 
      } 
     }); 

    holder.addressbook_check.setChecked(thumbnailsselection[position]); 
    return convertView; 
} 

class ViewHolder 
{ 
    TextView adddress_name; 
    TextView adddress_number; 
    CheckBox addressbook_check; 
} 
相關問題