2016-11-15 71 views
0

我想從列表視圖中選擇的項目填充的字符串ArrayList中提取字符串。
數組列表的結果是:從ArrayList中提取字符串,它從列表視圖中獲取項目

 {studentName=XXX,studentID=SF10001} 
    {studentName=YYYY,studentID=SF10002} 

我只想得到studentID這是SF10001和SF10002。下面是我的代碼:

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

    for (int i = 0; i < checked.size(); i++) { 
     // Item position in adapter 
     int position = checked.keyAt(i); 
     // Add sport if it is checked i.e.) == TRUE! 
     if (checked.valueAt(i)) 
      selectedItems.add(adapter.getItem(position)); 
    } 

    String[] outputStrArr = new String[selectedItems.size()]; 

    for (int i = 0; i < selectedItems.size(); i++) { 

      outputStrArr[i] = selectedItems.get(i); 

    } 

    Intent intent = new Intent(getApplicationContext(), 
      ResultActivity.class); 

    // Create a bundle object 
    Bundle b = new Bundle(); 
    b.putStringArray("selectedItems", outputStrArr); 

    // Add the bundle to the intent. 
    intent.putExtras(b); 

    // start the ResultActivity 
    startActivity(intent); 
    } 

這Activity.java

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_result); 
    Bundle b = getIntent().getExtras(); 
    String[] resultArr = b.getStringArray("selectedItems"); 

    ListView lv = (ListView) findViewById(R.id.outputList); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, resultArr); 
    lv.setAdapter(adapter); 


} 

@SoulRayder 所以,我創造了一個StudentData類的結果:

public class StudentData implements Serializable { 
String studentName; 
String studentID; 

public String getStudentNameStudentData() { 
    return studentName; 

} 

public String getStudentID() 
{ 
    return studentID; 
} 

我更改代碼like tis:

SparseBooleanArray checked = list.getCheckedItemPositions(); 

    ArrayList<StudentData> selectedItems = new ArrayList<StudentData>(); 

    for (int i = 0; i < checked.size(); i++) { 
     // Item position in adapter 
     int position = checked.keyAt(i); 
     // Add sport if it is checked i.e.) == TRUE! 
     if (checked.valueAt(i)) 

      selectedItems.add(adapter.getItem(position)); 
    } 

    String[] outputStrArr = new String[selectedItems.size()]; 

    for (int i = 0; i < selectedItems.size(); i++) { 

      outputStrArr[i] = selectedItems.get(i).getStudentID(); 

    } 

    Intent intent = new Intent(getApplicationContext(), 
      ResultActivity.class); 

    // Create a bundle object 
    Bundle b = new Bundle(); 
    b.putStringArray("selectedItems", outputStrArr); 

    // Add the bundle to the intent. 
    intent.putExtras(b); 

    // start the ResultActivity 
    startActivity(intent); 
    } 

但這個代碼告訴我錯誤:

selectedItems.add(adapter.getItem(position)); 

這是ArrayList中不能應用到(java.lang.String中)。如何解決問題。 BTW,感謝您的即時迴應:d

+2

您有一個字符串的Arraylist ...使它比需要的更難。製作學生課程。將其存儲在可以過濾的列表中 –

回答

0

兩種方式解決你的問題:

1)簡單方式,但不是高效:(您可以使用此,如果您的所有字符串都保證使用該確切模板,或進一步增加驗證到下面的函數)

public string extractStudentID(string input) 
{ 
     string [] parts = input.substring(1,input.length-1).split(","); 

     return parts[1].split("=")[1]; 
} 

,並在代碼中使用它,如下所示

for (int i = 0; i < checked.size(); i++) { 
    // Item position in adapter 
    int position = checked.keyAt(i); 
    // Add sport if it is checked i.e.) == TRUE! 
    if (checked.valueAt(i)) 
     selectedItems.add(extractStudentID(adapter.getItem(position))); 
} 

2)爲您的學生數據

class StudentData 
{ 
     public string studentName; 
     public string studentID; 
} 

使用這些對象的ArrayList創建一個類,而不是字符串數組列表,並相應地修改代碼在所有其他地方:

ArrayList<StudentData> selectedItems = new ArrayList<StudentData>(); 

然後,在任何時候,您都可以輕鬆訪問學生ID

selectedItems.get(index).studentID; 
+0

我更新了代碼也存在一些問題。請hlp解決:D –

+0

@Lai:您還需要確保您的適配器將學生數據存儲在您班級的相同簽名中(即它應該處理StudentData)。如果你確保這一點,那麼你可以使用那條錯誤的代碼行。目前它正在抱怨,因爲adapter.getItem(position)返回一個字符串而不是一個StudentData對象,因爲你的列表是一個StudentData對象列表。如果你讓它返回一個StudentData對象,那麼代碼應該工作。 – SoulRayder

+0

感謝您的回覆。我努力解決這個問題,但又出現了另一個問題。如果你能幫忙,你能幫我看一下嗎? http://stackoverflow.com/questions/40660410/change-the-listview-in-arrayadapter-become-multiple-choice-listview-in-arrayadap。謝謝:: D –