2013-09-26 7 views
6

ciao ho creato un listview con caselle di controllo in esso ... ma non so come ottenere il testo della casella di controllo che viene selezionato .. ecco il codice di activity_main.xmlAndroid ottiene il testo di tutte le checkbox spuntate in listView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="top" 
    android:orientation="vertical" 
    tools:context=".MygamesActivity" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginTop="20dp" /> 

</LinearLayout> 

altro layout che deve caselle da mostrare nella listview main.list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="CheckBox" 
     /> 
</LinearLayout> 

e questa è la classe che estende arrayadapter

package com.wasiff.listview; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckBox; 
import android.widget.ImageView; 
import android.widget.TextView; 


public class CheckboxAdapter extends ArrayAdapter<String> { 
    private LayoutInflater mInflater; 

    private String[] mStrings; 
    private TypedArray mIcons; 
    private int mViewResourceId; 

    public CheckboxAdapter(Context ctx,int viewResourceId,String[] strings){ 
     super(ctx,viewResourceId,strings); 

     mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     mStrings = strings; 

     mViewResourceId = viewResourceId; 
    } 

    public int getCount(){ 
     return mStrings.length; 
    } 

    public String getItem(int position){ 
     return mStrings[position]; 
    } 

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

    public View getView(int position,View convertView,ViewGroup parent){ 
     convertView = mInflater.inflate(mViewResourceId, null); 

     CheckBox tv = (CheckBox)convertView.findViewById(R.id.checkBox1); 
     tv.setText(mStrings[position]); 

     return convertView; 
    } 
} 

e questa è la mia classe mainActivity

package com.wasiff.listview; 

import android.app.ListActivity; 
import android.content.Context; 
import android.content.res.Resources; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Button; 
import android.widget.ListAdapter; 
import android.widget.ListView; 

public class MainActivity extends ListActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Context ctx = getApplicationContext(); 
     Resources res = ctx.getResources(); 

     String[] options = res.getStringArray(R.array.countrynames); 

     setListAdapter((ListAdapter) new CheckboxAdapter(ctx,R.layout.main_list_item,options)); 

    } 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

e, infine, ho tutti i paesi salvate in un file countries.xml sulla cartella valori

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string-array name="countrynames" translatable="false"> 
     <item>Bhutan</item> 
     <item>Colombia</item> 
     <item>India</item> 
     <item>Pakistan</item> 
     <item>Australia</item> 
     <item>Srilanka</item> 
     <item>England</item> 
    </string-array> 
</resources> 

mostra le caselle di controllo ListView ora quello che voglio è quello di ottenere il testo delle caselle di controllo che vengono controllati e mostrare un brindisi su un clic del pulsante (per testare) ho seguito il tutorial sul libro di cucina Android di oreilly ma ancora non so come impostare l'ascoltatore

risposta

18

Aggiungere all'interno CheckboxAdapter.java

ArrayList<String> selectedStrings = new ArrayList<String>(); 

Poi all'interno metodo GetView

tv.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (isChecked) { 
        selectedStrings.add(tv.getText().toString()); 
       }else{ 
        selectedStrings.remove(tv.getText().toString()); 
       } 

      } 
     }); 

Scrivi un getter che restituirà selectedStrings

ArrayList<String> getSelectedString(){ 
    return selectedStrings; 
} 
+1

Grazie per il suo funzionamento :) –

+0

Il problema con la soluzione è che non mantiene l'ordine delle caselle di controllo –

+0

all'interno di onCheckedChanged pulsante useView.getText invece di tv.getText – temirbek

1

Su approccio potrebbe essere quello di passare un proprio oggetto al vostro Arrayadapter:

class ArrayItem{ 
    private String text; 
    private boolean checked; 
    ... (getter/setter) 
} 

e solo ottenere l'Array utilizzato ritorno da Arrayadapter e leggerlo.

Arrayadaper...{ 

    public ArrayList<ArrayItem> getList(){ 
     return this.arrayList; 
    } 

    public View getView(int position,View convertView,ViewGroup parent){ 
     ArrayItem item = this.arrayList.get(position) 
     convertView = mInflater.inflate(mViewResourceId, null); 

     CheckBox tv = (CheckBox)convertView.findViewById(R.id.checkBox1); 
     tv.setCheckChangeListener... //item.setChecked(true:false) 
     tv.setText(mStrings[position]); 

     return convertView; 
    } 
} 


} 

È possibile scorrere la lista e basta manipolare le voci in cui controllati == true

Spero che questo aiuti.

13

Può essere questo testamento ti ha aiutato:

CheckBox cb; 
    ListView mainListView = getListView(); 
    for (int x = 0; x<mainListView.getChildCount();x++){ 
     cb = (CheckBox)mainListView.getChildAt(x).findViewById(R.id.myCheckBox); 
     if(cb.isChecked()){ 
      doSomething(); 
     } 
    } 
+0

Mi piace molto questo! –

+0

@Saif Hamed Funziona per me ... –

+0

Funzionerà solo per gli elementi visibili nell'elenco. – c0m4

0
SparseBooleanArray checked = listView1.getCheckedItemPositions(); 
       ArrayList<String> selectedItems = new ArrayList<String>(); 

       for (int i = 0; i < len; i++) { 
        if (checked.get(i)) 
         selectedItems.add((String) adapter.getItem(i)); 
       } 
       String[] outputStrArr = new String[selectedItems.size()]; 

       for (int i = 0; i < selectedItems.size(); i++) { 
        outputStrArr[i] = selectedItems.get(i); 
       } 
Problemi correlati