vinusvini
vinusvini

Reputation: 23

Empty List Android Studio

Why do i get empty list on my phone. Can some 1 help ? It shows correct 7 positions but they are empty. There is my php file its probbably ok. I use json encode.

public class ClientActivity extends UserAreaActivity {

private String jsonResult;
private String url = "http://vinusek.000webhostapp.com/Client2.php";
private ListView listView;
private TextView nazwa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client);
    listView = (ListView) findViewById(R.id.listView1);
    nazwa = (TextView) findViewById(R.id.textView2) ;
    accessWebService();
}

// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0]);
        try {
            HttpResponse response = httpclient.execute(httppost);
            jsonResult = inputStreamToString(
                    response.getEntity().getContent()).toString();
        }

        catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        }

        catch (IOException e) {
            // e.printStackTrace();
            Toast.makeText(getApplicationContext(),
                    "Error..." + e.toString(), Toast.LENGTH_LONG).show();
        }
        return answer;
    }

    @Override
    protected void onPostExecute(String result) {
        ListDrwaer();
    }
}// end async task

public void accessWebService() {
    JsonReadTask task = new JsonReadTask();
    // passes values for the urls string array
    task.execute(new String[] { url });
}

// build hash set for list view
public void ListDrwaer() {
    List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();

    try {

        JSONObject jsonResponse = new JSONObject(jsonResult);
        JSONArray jsonMainNode = jsonResponse.optJSONArray("result");

        for (int i = 0; i < jsonMainNode.length(); i++) {
            JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
            String number = jsonChildNode.getString("id");
            String name = jsonChildNode.getString("login");
            String pass = jsonChildNode.getString("haslo");
            String outPut = number + "-" +  name + "-" + pass;
            employeeList.add(createEmployee("list", outPut));
            nazwa.setText(outPut);
        }


    } catch (JSONException e) {
        Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                Toast.LENGTH_SHORT).show();
    }


    SimpleAdapter simpleAdapter = new SimpleAdapter(ClientActivity.this, employeeList,
            android.R.layout.simple_list_item_1,
            new String[] {"list"}, new int[] { android.R.id.text1 });
    listView.setAdapter(simpleAdapter);
}

private HashMap<String, String> createEmployee(String name, String number) {
    HashMap<String, String> employeeNameNo = new HashMap<String, String>();
    employeeNameNo.put(number, name);
    return employeeNameNo;
}}

What is wrong here, i got the code from here http://codeoncloud.blogspot.in/2013/07/android-mysql-php-json-tutorial.html and it works for his php file.

Upvotes: 2

Views: 1648

Answers (1)

Eran
Eran

Reputation: 393946

Check the SimpleAdapter class reference:

As you can see, the constructor takes the following arguments :

SimpleAdapter (Context context, 
               List<? extends Map<String, ?>> data, 
               int resource, 
               String[] from, 
               int[] to)
  1. context Context: The context where the View associated with this SimpleAdapter is running
  2. data List: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
  3. resource int: Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
  4. from String: A list of column names that will be added to the Map associated with each item.
  5. to int: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

You instantiate the SimpleAdapter with :

SimpleAdapter simpleAdapter = new SimpleAdapter(ClientActivity.this, employeeList,
            android.R.layout.simple_list_item_1,
            new String[] {"list"}, new int[] { android.R.id.text1 });

This means that each element of your employeeList List should be a Map that contains the key "list".

Therefore

employeeList.add(createEmployee("list", outPut));

should add to the List a Map having an entry with the key "list" and the value outPut.

However, your createEmployee() method creates a HashMap with a single entry where the key and the value are swapped.

You should change

private HashMap<String, String> createEmployee(String name, String number) {
    HashMap<String, String> employeeNameNo = new HashMap<String, String>();
    employeeNameNo.put(number, name);
    return employeeNameNo;
}

to

private HashMap<String, String> createEmployee(String name, String number) {
    HashMap<String, String> employeeNameNo = new HashMap<String, String>();
    employeeNameNo.put(name, number);
    return employeeNameNo;
}

Upvotes: 1

Related Questions