如何从JSON中为List视图创建条件

时间:2014-02-28 02:05:25

标签: java android json listview

我想列表视图,其中listview数据仅来自我的json文件中的“windy”名称。谁能告诉我哪里是我的错误? 我在我的json数组中创建一个条件以放入listview。 它应该显示来自json的所有数据并将其放在listview中,因为(x == y)是正确的,但没有任何反应,只有空列表视图。 当我制作System.out.println(x +“”+ y);为了确保没有空,它显示风和风的x和y的值,并且根据我所做的条件,它应该只显示windy的名称。但是当我做(x!= y)时,它会显示从json到listview的所有数据。 非常感谢你们。

这是我制作的条件的部分代码。

for(int i = 0; i < str_json.length(); i++){
JSONObject ar = str_json.getJSONObject(i);
String x = ar.getString("nama");
String y = "windy";
System.out.println(x +" "+ y);

//the conditon that i mean. 
//when i make if(x!=y), it show all the data from json to listview, not only "windy" 


if( x == y){

HashMap<String, String> map = new HashMap<String, String>();

String nama = ar.getString("nama");
String judul = ar.getString("judul");
String komen = ar.getString("komen");
String jawaban = ar.getString("jawaban");
String created_at = ar.getString("created_at");


map.put(tanya_nama, nama);
map.put(tanya_judul, judul);
map.put(tanya_komen, komen);
map.put(tanya_jawaban, jawaban);
map.put(tanya_waktu, created_at);

data_map.add(map);

这是完整的代码。

public class semuatempat extends ListActivity {

static String tanya_nama = "nama";
static String tanya_judul = "judul";
static String tanya_komen = "komen";
static String tanya_jawaban = "jawaban";
static String tanya_waktu = "created_at";

JSONArray str_json = null;
public String lo_Koneksi,isi ;

 // Search EditText
 EditText inputSearch;
 //Listview Adapter
 SimpleAdapter adapter;

 ListView lv;

private ProgressDialog pDialog;
ArrayList<HashMap<String, String>> data_map = new ArrayList<HashMap<String, String>>  ();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.utama);

    Koneksi lo_Koneksi = new Koneksi();
    isi = lo_Koneksi.isi_koneksi();
    new AmbilData().execute();

}



class AmbilData extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(semuatempat.this);
        pDialog.setMessage("loading ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... args) {


        String link_url = isi+"komentar.php";
        JSONParsena jParser = new JSONParsena();
        JSONObject json = jParser.AmbilJson(link_url);

        try {
            str_json = json.getJSONArray("komentar");
            DatabaseHandler db = new DatabaseHandler(getApplicationContext());

            /**
             * Hashmap to load data from the Sqlite database
             **/
             HashMap<String,String> user = new HashMap<String, String>();
             user = db.getUserDetails();



             for(int i = 0; i < str_json.length(); i++){
                JSONObject ar = str_json.getJSONObject(i);
                String x = ar.getString("nama");
                String y = "windy";
                System.out.println(x +" "+ y);

                if( x == y){

                HashMap<String, String> map = new HashMap<String, String>();

                String nama = ar.getString("nama");
                String judul = ar.getString("judul");
                String komen = ar.getString("komen");
                String jawaban = ar.getString("jawaban");
                String created_at = ar.getString("created_at");


                map.put(tanya_nama, nama);
                map.put(tanya_judul, judul);
                map.put(tanya_komen, komen);
                map.put(tanya_jawaban, jawaban);
                map.put(tanya_waktu, created_at);

                data_map.add(map);
                }
                else{
                    System.out.println(" cacad ");
                }

             }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
             public void run() {

                 adapter = new SimpleAdapter(
                        semuatempat.this, data_map,
                        R.layout.list_item, new String[] { tanya_nama, tanya_judul, tanya_komen,tanya_jawaban,tanya_waktu}, 
                        new int[] {R.id.nama, R.id.alamat, R.id.lat,R.id.lng,R.id.waktu});
                setListAdapter(adapter);

            }
        });

empty listview the result of System.out.println(x +" "+ y);

2 个答案:

答案 0 :(得分:0)

问题在于您编写的字符串比较函数:

x == y

将其替换为:

if (x.equals(y))

因为x和y是字符串。

答案 1 :(得分:0)

更改您的

if( x == y)

if( x.equals(y)),因为您正在比较字符串。

然后在你的AsyncTask中,也许你应该改变,

class AmbilData extends AsyncTask(String, String, String)

class AmbilData extends AsyncTask(String, String, HashMap<String, String>) {

方法doInBackground

 protected String doInBackground(String... args) {
    // rest of your method

    return map;
  }

将onPostExecute方法更改为:

protected void onPostExecute(HashMap<String,String> map) {
// adapting your hashmap to listview
    }

我还没有测试过它。但是,它应该有用。