ListView适用于模拟器,但不适用于手机

时间:2014-08-18 01:01:33

标签: android android-listview android-json

我尝试制作一个简单的应用程序,它将从互联网上获取API,重新排列它们并显示为列表。

在模拟器中它可以工作。手机显示白屏。手机有Android 2.3.7。但是当我在平板电脑上试用它时,该应用程序停止了工作。平板电脑有Android 4.1。

MainActivity

public class MainActivity extends ListActivity implements OnItemClickListener{

    private ProgressDialog pDialog;

    private String url = "http://www.cscpro.org/secura/market/"; //food-29-5.json;
    private String res;
    private String ql;

    JSONArray market;
    final static String RESOURCES = "res";
    final static String QUALITY = "q";
    private static final String TAG_OFFER = "offer";
    private static final String TAG_PRICE = "price";
    private static final String TAG_SALLER = "seller";
    private static final String TAG_SALLER_NAME = "name";

    ArrayList<HashMap<String, String>> listSaller;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listSaller = new ArrayList<HashMap<String, String>>();
        Intent in = getIntent();
        res = in.getStringExtra(RESOURCES);
        ql = in.getStringExtra(QUALITY);
        ListView lv = getListView();
        lv.setOnItemClickListener(this);            

        // Calling async task to get json
        new GetContacts().execute();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        String rate = ((TextView) view.findViewById(R.id.rate)).getText().toString();
        String name = ((TextView) view.findViewById(R.id.name)).getText().toString();

        // Starting single contact activity
        Intent in = new Intent(getApplicationContext(),
                SingleContactActivity.class);
        in.putExtra(TAG_PRICE, rate);
        in.putExtra(TAG_SALLER_NAME, name);
        startActivity(in);
    }

    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String myURL = url+res+"-29-"+ql+".json";
            String jsonStr = sh.makeServiceCall(myURL, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    market = jsonObj.getJSONArray(TAG_OFFER);

                    // looping through All Contacts
                    for (int i = 0; i < market.length(); i++) {
                        JSONObject c = market.getJSONObject(i);

                        String rate = c.getString(TAG_PRICE);

                        // Phone node is JSON Object
                        JSONObject saller = c.getJSONObject(TAG_SALLER);
                        String name = saller.getString(TAG_SALLER_NAME);

                        // tmp hashmap for single contact
                        HashMap<String, String> sallers = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        sallers.put(TAG_PRICE, rate);
                        sallers.put(TAG_SALLER_NAME, name);

                        // adding contact to contact list
                        listSaller.add(sallers);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, listSaller,
                    R.layout.list_item, new String[] { TAG_PRICE, TAG_SALLER_NAME}, new int[] { R.id.rate,
                            R.id.name});

            setListAdapter(adapter);
        }
    }
}

SingleContactActivity

public class SingleContactActivity extends Activity {

    // JSON node keys
    private static final String TAG_PRICE = "price";
    private static final String TAG_SALLER_NAME = "name";

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

        // getting intent data
        Intent in = getIntent();

        // Get JSON values from previous intent
        String rate = in.getStringExtra(TAG_PRICE);
        String name = in.getStringExtra(TAG_SALLER_NAME);

        // Displaying all values on the screen
        TextView lblName = (TextView) findViewById(R.id.name_label);
        TextView lblRate = (TextView) findViewById(R.id.rate_label);

        lblName.setText(name);
        lblRate.setText(rate);
    }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <!-- Main ListView 
         Always give id value as list(@android:id/list)
    -->
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

activity_single_contact.xml 是一个简单的LinearLayout,其中包含两个TextView

0 个答案:

没有答案