0

我在这里需要帮助。我想用来自 2 个 url 服务器的 json 进行解析,然后在 1 个列表视图中显示该数组。这是我的代码。

public class MainActivity extends Activity {
private JSONObject jObject;
private String jsonResult ="";
private JSONObject jObject2;
private String jsonResult2 ="";
private String url = "http://10.0.2.2/kota/daftarkota.php";
private String url2 = "http://10.0.2.2/kota/delkota.php";
private String url3 = "http://www.hatsa.byethost33.com/kota_daftarkota.php";
String[] daftarid;
String[] daftarnama;
String[] daftarlatitude;
String[] daftarlongitude;
Menu menu;
public static MainActivity ma;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ma=this;
    RefreshList();
}

public void RefreshList() {
    try {
        jsonResult = getRequest(url); 

        jObject = new JSONObject(jsonResult);
        JSONArray menuitemArray = jObject.getJSONArray("kota");

        daftarid = new String[menuitemArray.length()];
        daftarnama = new String[menuitemArray.length()];
        daftarlatitude = new String[menuitemArray.length()];
        daftarlongitude = new String[menuitemArray.length()];

        for (int i = 0; i < menuitemArray.length(); i++)
        {
            daftarid[i] = menuitemArray.getJSONObject(i).getString("id").toString();
            daftarnama[i] = menuitemArray.getJSONObject(i).getString("nama").toString();
            daftarlatitude[i] = menuitemArray.getJSONObject(i).getString("latitude").toString();
            daftarlongitude[i] = menuitemArray.getJSONObject(i).getString("longitude").toString();
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        jsonResult2 = getRequest(url3); 

        jObject2 = new JSONObject(jsonResult2);
        JSONArray menuitemArray = jObject2.getJSONArray("kota");

        daftarid = new String[menuitemArray.length()];
        daftarnama = new String[menuitemArray.length()];
        daftarlatitude = new String[menuitemArray.length()];
        daftarlongitude = new String[menuitemArray.length()];

        for (int i = 0; i < menuitemArray.length(); i++)
        {
            daftarid[i] = menuitemArray.getJSONObject(i).getString("id").toString();
            daftarnama[i] = menuitemArray.getJSONObject(i).getString("nama").toString();
            daftarlatitude[i] = menuitemArray.getJSONObject(i).getString("latitude").toString();
            daftarlongitude[i] = menuitemArray.getJSONObject(i).getString("longitude").toString();
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ListView ListView01 = (ListView)findViewById(R.id.ListView01);
    ListView01.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, daftarnama));

    ListView01.setSelected(true);
    ListView01.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            final String selectionid = daftarid[arg2]; 
            final String selectionnama = daftarnama[arg2]; 
            final String selectionlatitude = daftarlatitude[arg2]; 
            final String selectionlongitude = daftarlongitude[arg2]; 
            final CharSequence[] dialogitem = {"Edit", "Delete"};
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Pilih ?");
            builder.setItems(dialogitem, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    switch(item){
                    case 0 :
                        Intent i = new Intent(getApplicationContext(), EditActivity.class);  
                        i.putExtra("id", selectionid);
                        i.putExtra("nama", selectionnama);
                        i.putExtra("latitude", selectionlatitude);
                        i.putExtra("longitude", selectionlongitude);
                        startActivity(i);

                        break;
                    case 1 :
                        getRequest(url2 + "?id=" + selectionid);
                        RefreshList();

                        break;
                    }
                }
            });
            builder.create().show();
        }});

    ((ArrayAdapter)ListView01.getAdapter()).notifyDataSetInvalidated();

}

/**
 * Method untuk Mengirimkan data ke server
 */
public String getRequest(String Url){

    String sret="";
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(Url);
    try{
        HttpResponse response = client.execute(request);
        sret =request(response);

    }catch(Exception ex){
        Toast.makeText(this,"Gagal "+sret, Toast.LENGTH_SHORT).show();
    }
    return sret;

}
/**
 * Method untuk Menerima data dari server
 */
public static String request(HttpResponse response){
    String result = "";
    try{
        InputStream in = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) != null){
            str.append(line + "\n");
        }
        in.close();
        result = str.toString();
    }catch(Exception ex){
        result = "Error";
    }
    return result;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;

    menu.add(0, 1, 0, "Tambah").setIcon(android.R.drawable.btn_plus);
    menu.add(0, 2, 0, "Refresh").setIcon(android.R.drawable.ic_menu_rotate);
    menu.add(0, 3, 0, "Exit").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case 1:
        Intent i = new Intent(MainActivity.this, AddActivity.class);
        startActivity(i);
        return true;
    case 2:            
        RefreshList();         
        return true;
    case 3:
        finish();
        return true;
    }
    return false;
}

}

如果我运行它,列表视图只会显示来自“url”的数组,而“url3”中什么也没有。我不知道我的代码应该如何,如果我想让列表视图显示来自“url”和“url3”的数组。谢谢。:)

4

1 回答 1

0

它实际上看起来相反。我猜您会看到来自 的数据url3,因为您创建了两次数组daftarnama- 第二次使用来自 的数据url3。使用 aList<String>而不是数组。只需创建一次列表,然后添加两个 url 中的所有项目。此外,您应该重新考虑您的代码。您在这里有很多重复的代码(DRY - 不要重复自己)。

于 2013-03-22T00:39:01.330 回答