2016-11-15 65 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 –