从ArrayList中提取String,从listview中获取项目

时间:2016-11-15 12:39:24

标签: java android listview arraylist

我想从String的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);
    }

这就是Result 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 个答案:

答案 0 :(得分: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并相应地修改所有其他地方的代码:

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

然后,在任何时候,您都可以通过

轻松访问学生证
 selectedItems.get(index).studentID;