![](/img/trans.png)
[英]How do I get String values on listview from the ArrayList<String[]>?
[英]Extract String from ArrayList which get the item from listview
我想從String的arraylist中提取字符串,該arraylist由listView中的選定項填充。
數組列表的結果是:
{studentName=XXX,studentID=SF10001}
{studentName=YYYY,studentID=SF10002}
我只想獲取SF10001和SF10002的studentID。 下面是我的代碼:
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;
}
我像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)。 如何解決問題。 順便說一句,謝謝您的立即答復:D
解決問題的兩種方法:
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<StudentData> selectedItems = new ArrayList<StudentData>();
然后,您可以隨時通過以下方式輕松訪問學生證
selectedItems.get(index).studentID;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.