开发者

Android, get cheked items on ListActivity

I'm using a ListActivity on wich I get the Access Points in the WIFI range, and list them in a cheking list. I've been successfull doing this, but I would like to get the cheked items when I click in the footer button. How do I get them to a String array??

This is the code:

public class APselection extends ListActivity {
    protected static开发者_运维知识库 final String TAG = "teste";
    /** Called when the activity is first created. */
    private TextView mScanList;
    public List<ScanResult> listaAPs; 
    protected WifiManager wifiManager;
    private IntentFilter mWifiStateFilter;
    public String SCAN;
    public String[] SCANed;
    public String scanAP;
    public ListView lv;
    public ListView lv1;
    public List<Long> list = new ArrayList(); 
    public String[] checked;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);             

        View footer = View.inflate(this, R.layout.footer, null);           

        wifiManager= (WifiManager)getSystemService(Context.WIFI_SERVICE);

        int i;

        //function to get the APs
        SCANed=handleScanResultsAvailable().split("SPl");

        lv=getListView();
        lv.addFooterView(footer);            

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, SCANed));            

        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lv.setTextFilterEnabled(false); 
    }
}


There are several ways to do this. The easiest is probably to implement the OnClickListener for the ListView. When the user clicks a list item, extract the String from the clicked item. Store the items in an ArrayList. When the user clicks, if the list contains the String, remove it, otherwise add it. Bam. List of all selected items.


I don't think there is a way to directly get the checked item to a String array from the ListView, you have to go through intermediate steps :

You can use ListView.getCheckedItemIds to get an array of the id of the checked items. These id are assigned by your list adapter. Since you're using an ArrayAdapter, position=id, so you can just use ArrayAdapter.getItem() to get the String associated with each checked item id.

This would look like :

btn.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    long ids[] = lv.getCheckedItemIds();
    String checkedItems[] = new String[ids.length];
    for (int i=0; i<ids.length; i++)
      checkedItems[i] = adapter.getItem(i);
    //You got your array of checked strings
  }
}

Note that this require access to the adapter, so you would have to assign your ArrayAdapter to a variable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜