不能在不同方法

时间:2016-08-09 17:42:01

标签: android json android-intent android-volley

也许我无法解释我的问题但是下面的代码我希望能帮助你回答我的问题。我试图使用Intent在两个活动之间传递一个String数据,同时,在我的StringRequest中,我使用jSonObject使用齐射从服务器恢复数据,所以我想知道如何从同一个变量恢复相同的变量服务器到第二个活动使用putExtra,因为我有修饰符的问题,如果我的变量的删除修饰符我在putExtra中有错误它应该是最终修饰符,如果我把它我的jsonobject有错误,那么,&# 39;解决方案好吗?提前谢谢。

String idMed;               
final String numTelMed;
final String communeMed;
final String nomMed;
final String emailMed;
final String codePostalMed;
final String prenomMed;
final String rueMed;
final String villeMed;
final String specialiteMed;
final String latitude;
final String longitude;
ListView listeView;

listeView = (ListView) findViewById(R.id.sampleListView);
List<String> listeMed = new ArrayList<String>();
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Main_Activity.this,android.R.layout.simple_list_item_1, listeMed);
listeView.setAdapter(adapter1);


JSONArray jObjectSearch = new JSONArray(response);
Log.i("OK","JSONObjectOK : "+jObjectSearch);
Log.i("OK","JSONObjectOK : "+jObjectSearch.getString(0));
for(int i=0; i<jObjectSearch.length();i++)
{
    JSONObject  j = new JSONObject(jObjectSearch.getString(0));
    idMed = j.getString("id");
    nomMed = j.getString("nomMed");
    prenomMed = j.getString("prenomMed");
    numTelMed = j.getString("numTelMed");
    emailMed = j.getString("emailMed");
    rueMed = j.getString("rueMed");
    communeMed = j.getString("communeMed");
    codePostalMed = j.getString("codePostalMed");
    villeMed = j.getString("villeMed");
    specialiteMed = j.getString("specialiteMed");
    latitude = j.getString("latitude");
    longitude = j.getString("longitude");

    listeMed.add("Nom : "+nomMed+"\n "+"Prénom : "+prenomMed+"\n "+"Numéro Téléphone : "+numTelMed+"\n "
              +"Email : "+emailMed+"\n "+"Adresse : "+rueMed+" "+codePostalMed+" "
              +villeMed+" "+communeMed+"\n "+"Spécialité : "+specialiteMed);

     listeView.setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            medecinSearched = new Intent(Main_Activity.this, MedecinSearched.class);
            medecinSearched.putExtra("nomMed", nomMed);
            medecinSearched.putExtra("prenomMed", prenomMed);
            medecinSearched.putExtra("numTelMed", numTelMed);
            medecinSearched.putExtra("emailMed", emailMed);
            medecinSearched.putExtra("rueMed", rueMed);
            medecinSearched.putExtra("communeMed", communeMed);
            medecinSearched.putExtra("codePostalMed", codePostalMed);
            medecinSearched.putExtra("villeMed", villeMed);
            medecinSearched.putExtra("specialiteMed", specialiteMed);
            medecinSearched.putExtra("latitude", latitude);
            medecinSearched.putExtra("longitude", longitude);

            startActivity(medecinSearched);
            finish();


        }

    });

}

2 个答案:

答案 0 :(得分:1)

如果要在匿名类中使用变量,则必须使用final关键字。

但是,在这种情况下,我会用另一种方法解决这个问题:

  • 创建一个Med类,其中包含您需要的所有属性id,nom,prenom等。

  • 创建MedAdapter展开BaseAdapter而不是使用ArrayAdapter<String>

onCreate方法:

listeView = (ListView) findViewById(R.id.sampleListView);
listeMed = new ArrayList<Med>();
MedAdapter adapter1 = new MedAdapter<String>(Main_Activity.this, listeMed);
listeView.setAdapter(adapter1);
listeView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Med med = listeMed.get(position);
            Intent medecinSearched = new Intent(Main_Activity.this, MedecinSearched.class);
            medecinSearched.putExtra("EXTRA_MED", med); // Med must implement Serializable or Parcelable
            startActivity(medecinSearched);
            finish();
        }
});

响应回调:

JSONArray jObjectSearch = new JSONArray(response);
for(int i=0; i<jObjectSearch.length(); i++) {
    JSONObject jsonObject = new JSONObject(jObjectSearch.getString(0));
    Med med = new Med(jsonObject); // Med must have a constructor which receives the JSONObject.
    listeMed.add(med);
}
adapter1.notifyDataSetChanged();

希望它有所帮助。

答案 1 :(得分:0)

感谢所有人,这对我有很大的帮助

我有解决方案,但我不知道它是否有不便之处,这是我的解决方案:
`

                            String idMed;             String numTelMed;  String communeMed;
                             String nomMed;          String emailMed;   String codePostalMed;
                             String prenomMed;       String rueMed;     String villeMed;
                             String specialiteMed;   String latitude;   String longitude;
                            ListView listeView;

                            listeView = (ListView) findViewById(R.id.sampleListView);
                            List<String> listeMed = new ArrayList<String>();
                            ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Main_Activity.this,android.R.layout.simple_list_item_1, listeMed);
                            listeView.setAdapter(adapter1);


                            JSONArray jObjectSearch = new JSONArray(response);
                            Log.i("OK","JSONObjectOK : "+jObjectSearch);
                            Log.i("OK","JSONObjectOK : "+jObjectSearch.getString(0));
                            for(int i=0; i<jObjectSearch.length();i++)
                            {
                             JSONObject  j = new JSONObject(jObjectSearch.getString(0));
                            final String idMedF = idMed = j.getString("id");
                            final String nomMedF = nomMed = j.getString("nomMed");
                            final String prenomMedF =prenomMed = j.getString("prenomMed");
                            final String numTelMedF =numTelMed = j.getString("numTelMed");
                            final String emailMedF =emailMed = j.getString("emailMed");
                            final String rueMedF= rueMed = j.getString("rueMed");
                            final String communeMedF =communeMed = j.getString("communeMed");
                            final String codePostalMedF = codePostalMed = j.getString("codePostalMed");
                            final String villeMedF =villeMed = j.getString("villeMed");
                            final String specialiteMedF =specialiteMed = j.getString("specialiteMed");
                            final double latitudeF =  j.getDouble("latitude");
                            final double longitudeF =  j.getDouble("longitude");

                              listeMed.add("Nom : "+nomMed+"\n "+"Prénom : "+prenomMed+"\n "+"Numéro Téléphone : "+numTelMed+"\n "
                                          +"Email : "+emailMed+"\n "+"Adresse : "+rueMed+" "+codePostalMed+" "
                                          +villeMed+" "+communeMed+"\n "+"Spécialité : "+specialiteMed);

                                 listeView.setOnItemClickListener(new OnItemClickListener() {


                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                                               putData(idMedF, nomMedF, prenomMedF, numTelMedF, emailMedF, rueMedF, communeMedF, codePostalMedF, villeMedF, specialiteMedF, latitudeF, longitudeF);


                                                }

                                      });


                            }

                        } catch (JSONException e) {

                            e.printStackTrace();
                        }



                }`