简体   繁体   English

从ArrayList提取字符串,该字符串从listview获取项目

[英]Extract String from ArrayList which get the item from listview

I want to extract string from the arraylist of String which is filled by the selected items from the listView. 我想从String的arraylist中提取字符串,该arraylist由listView中的选定项填充。
The result of the array list is: 数组列表的结果是:

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

I want only get the studentID which are SF10001 and SF10002. 我只想获取SF10001和SF10002的studentID。 Below is my code: 下面是我的代码:

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

And this the Result Activity.java 这就是结果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 So, I had created a StudentData Class: @SoulRayder因此,我创建了一个StudentData类:

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

public String getStudentNameStudentData() {
    return studentName;

}

public String getStudentID()
{
    return studentID;
}

And I change the code like tis: 我像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);
    }

But this code show me error: 但是这段代码显示了错误:

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

which is ArrayList cannot applied to (java.lang.String). ArrayList不能应用于(java.lang.String)。 How to solve the problem. 如何解决问题。 BTW, thanks your immediate respond :D 顺便说一句,谢谢您的立即答复:D

Two ways of solving your problem : 解决问题的两种方法:

1) Simpler way , but not efficient : (You can use this if all your strings are guaranteed to use that exact template, or add further validations to the below function) 1) 更简单的方法,但是效率 :(如果可以保证所有字符串都使用确切的模板,或者在下面的函数中添加进一步的验证,则可以使用此方法)

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

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

and use it in your code as follows 并在您的代码中使用它,如下所示

 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) Create a class for you student data 2)为您的学生数据创建一个班级

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

Use an arraylist of these objects, instead of an arraylist of strings and modify your code in all other places accordingly: 使用这些对象的数组列表,而不是字符串数组列表,并相应地在所有其他位置修改代码:

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

Then, at any point, you can access the student ID easily by 然后,您可以随时通过以下方式轻松访问学生证

 selectedItems.get(index).studentID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM