从数据库获取数据到android列表视图

时间:2015-11-19 23:17:34

标签: android mysql

我正在创建一个Android应用程序,我将从mysql数据库中获取图像和文本等数据。我有一个问题。当我运行程序时,我的列表视图中没有显示任何内容。

这是我的City.java

public class City extends ActionBarActivity {

String myJSON;
 ImageView image;
TextView name,id,description;
private static final String TAG_RESULTS="result";
public static final String TAG_ID = "place_id";
public static final String TAG_NAME = "place_name";
public static final String TAG_ADD ="description";
//public static final String TAG_PIC ="place_icture";
JSONArray cities = null;

ArrayList<HashMap<String, String>> CitiesList;
UserLocalStore userLocalStore;
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.citylayout);
    userLocalStore = new UserLocalStore(this);
    name=(TextView)findViewById(R.id.name);
    id=(TextView)findViewById(R.id.id);
    description=(TextView)findViewById(R.id.description);
    list = (ListView) findViewById(R.id.listView1);
    CitiesList = new ArrayList<HashMap<String, String>>();
    image = (ImageView)findViewById(R.id.image);
    getData();
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(City.this, "Opening", Toast.LENGTH_LONG).show();
            Map<String, Object> map = (Map<String, Object>)parent.getItemAtPosition(position);
            String info1 = (String) map.get("place_id");
            String info2 = (String) map.get("place_name");
            String info3 = (String) map.get("description");
           //String info4 = (String) map.get("place_picture");
            Intent myIntent = new Intent(view.getContext(), Login.class);
            myIntent.putExtra("info1", info1);
            myIntent.putExtra("info2", info2);
            myIntent.putExtra("info3", info3);
           //myIntent.putExtra("info4", info4);


            startActivity(myIntent);

        }
    });


}

protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        cities = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<cities.length();i++){
            JSONObject c = cities.getJSONObject(i);
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            String address = c.getString(TAG_ADD);
            //String picture = c.getString(TAG_PIC);
            HashMap<String,String> persons = new HashMap<String, String>();

            persons.put(TAG_ID,id);
            persons.put(TAG_NAME,name);
            persons.put(TAG_ADD,address);
            //persons.put(TAG_PIC,picture);
            CitiesList.add(persons);
        }

        ListAdapter adapter = new SimpleAdapter(
                City.this, CitiesList, R.layout.city,
                new String[]{TAG_ID,TAG_NAME,TAG_ADD},
                new int[]{R.id.id,R.id.name, R.id.description}
        );

        list.setAdapter(adapter);

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

}

public void getData(){
    class GetDataJSON extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://iguideph-001-site1.btempurl.com/city_info.php");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result){
            myJSON=result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

请更改:

public static final String TAG_PIC ="place_icture";

public static final String TAG_PIC ="place_picture";
声明中的

persons.put(TAG_PIC,picture); 

persons.put(TAG_PIC,String.valueOf(d));
showList()

中的

希望这有帮助!