2011-03-18 28 views
0

我使用的是一個ListActivity,我得到了WIFI範圍內的接入點,並將它們列在一個列表中。 我一直在成功做到這一點,但我希望在點擊頁腳按鈕時獲得cheked項目。我如何讓他們到一個字符串數組?Android,在ListActivity上獲取cheked項目

這是代碼:

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

回答

0

我不認爲有一種方法可以直接將檢查項目從ListView的一個String數組,你必須要經過中間步驟:

您可以使用ListView.getCheckedItemIds獲取檢查項目的ID的數組。這些ID由您的列表適配器分配。由於您使用的是ArrayAdapter,position = id,所以您可以使用ArrayAdapter.getItem()獲取與每個檢查的項目標識相關聯的字符串。

這看起來像:

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 
    } 
} 

注意,這需要訪問適配器,所以你有你的ArrayAdapter分配給一個變量。

1

有幾種方法可以做到這一點。最簡單的可能是實現ListView的OnClickListener。當用戶單擊列表項目時,從單擊的項目中提取字符串。將項目存儲在ArrayList中。當用戶點擊時,如果列表包含字符串,請將其刪除,否則將其添加。巴姆。所有選定項目的列表。