在android中匹配两个ArrayList值

时间:2016-05-27 07:11:45

标签: android arraylist

我有两个ArrayList。首先包含类名,如Ab,BB,CC,第二个ArrayList包含类名和名称。我希望当我从第一个ArrayList中选择类名时,然后只显示名称与第一个ArrayList名称匹配的名称。这怎么可能?

以下是一些代码段:

JSONArray array3 = object.getJSONArray("class");
for(int x = 0; x<array3.length(); x++){
    JSONObject object1=array3.getJSONObject(x);
    c = object1.getString("class");
    classItem.add(c);
} 
JSONArray array4 = object.getJSONArray("subclass");
for(int z = 0; z<array4.length(); z++){
    JSONObject object1 = array4.getJSONObject(z);
    String name = object1.getString("Name");
    String cname = object1.getString("class");
    subclassitm.add(name);
} 

array3仅包含类名,array4包含名称和类名。我希望当我从array3选择课程时,只需在array4中显示姓名。

1 个答案:

答案 0 :(得分:0)

尝试这样

Spinner s1=(Spinner)findViewById(R.id.spinner);

  ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(),
                R.array.material_type, android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(adapter);
       final String materialType = s1.getSelectedItem().toString();
  b1.setOnClickListener(new View.OnClickListener() {
            InputStream is =null;
            String result1 =null;
            @Override
            public void onClick(View v) {

                String a = e1.getText().toString();
                String b = e2.getText().toString();
                String c = e3.getText().toString();

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("mat", a));
                nameValuePairs.add(new BasicNameValuePair("comp", b));
                nameValuePairs.add(new BasicNameValuePair("desc", c));
                nameValuePairs.add(new BasicNameValuePair("mattype", materialType));


                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("http://10.0.2.2/android/matmas/create.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpClient.execute(httpPost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                    android.util.Log.e("log_tag", "connection success ");

                } catch (Exception e) {
                    android.util.Log.e("log_tag", "Error in http connection " + e.toString());
                    Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
                }
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();

                    result1 = sb.toString();


                } catch (Exception e) {
                    android.util.Log.e("log_tag", "Error converting result " + e.toString());
                }
              try

              {
                  JSONObject object = new JSONObject(result1);
                  String w = object.getString("re");
                  if(w.equals("Material Details Created successfully"))
                  {
                      Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
                      Intent i = new Intent(getApplicationContext(),MenuScreen.class);
                      startActivity(i);

                  }
                  else
                  {
                      Toast.makeText(getApplicationContext(), "FILL REQUIRED FIELDS", Toast.LENGTH_SHORT).show();
                      Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
                  }
              }
              catch (Exception e)
              {
                  Log.e("log_tag", "Error parsing data "+e.toString());
                  Toast.makeText(getApplicationContext(), "JsonArray failed to load", Toast.LENGTH_SHORT).show();
              }

            }
        });
    }
}