如何在android中找到arraylist中所选数组的id

时间:2013-06-17 08:52:18

标签: android arrays arraylist

我有一个数组列表,包含国家列表,接下来我选择了字符串数组中的特定学生。那么如何找到相应数组列表ID的选定字符串数组。

代码:

public class MainActivity extends Activity {

    ArrayList<String> s = new ArrayList<String>();
    private String log_email, log_pass, selectedcon = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        s.add("Nepal");
        s.add("Korea");
        s.add("Sri Lanka");
        s.add("India");
        s.add("Pakistan");
        s.add("China");
        s.add("Australia");
        s.add("Bhutan");

        String s1 = "Australia, Nepal, India, Korea";

        String selectedHobbies[] = s1.split(",");
        Log.d("TAG", "ddddd--"  + "----"
                + selectedHobbies.length);
        if (selectedHobbies.length != 0) {

            for (int i = 0; i < s.size(); i++) {
                for (int j = 0; i < selectedHobbies.length; j++) {

                    String t = selectedHobbies[j];
                    if (Arrays.asList(s.get(i)).contains(t)) {

                        selectedcon = selectedcon + i + ",";
                        Log.d("TAG", "Testing--" + i + "----"
                                + selectedcon);
                    }
                    // int match=false;
                }
            }

        }
        Log.d("TAG", "Selected--ddddddddd---" + selectedcon);
    }


}

我想找到数组列表中所选国家/地区的ID。

1 个答案:

答案 0 :(得分:0)

ArrayList<String> s = new ArrayList<String>();
        s.add("Nepal");
        s.add("Korea");
        s.add("Sri Lanka");
        s.add("India");
        s.add("Pakistan");
        s.add("China");
        s.add("Australia");
        s.add("Bhutan");

        String s1 = "Australia, Nepal, India, Korea";
        String selectedHobbies[] = s1.split(",");

        for (int i=0;i<s.size();i++) {
            for (int j=0;j<selectedHobbies.length;j++) {
                if (s.get(i).contains(selectedHobbies[j])) {
                    Log.d("xxx", "found");
                    Log.d("xxx", "s index " + i);
                    Log.d("xxx", "hobbies index " + j);
                }
            }
        }

校正。