如何在listview上显示数据?

时间:2015-06-11 16:50:52

标签: java android

我有一个列表视图适配器,一个getter - setter类和一个显示ListView的类。我想获取数据但数据不显示。我正在使用json数组。

有人能指出我的错误吗?

这是我的班级

public class MyorderActivity extends Activity {
     private static final int SELECT_PICTURE_REQ = 1;
        private ObjectGetter mObjectGetter;
        private LoginSystem mLoginSystem;
     //   private ArrayAdapter<MyOrderItemData2> mItemsAdapter;
        private ProgressDialog mProgressDialog;
        private File mImageFile;
        private ListView mListView;
        private EmptyLayout mEmptyLayout;
        ArrayList<MyOrder> mCities;
        private boolean mUserImageChanged;
       ActionBarActivity activity;
       JSONObject jsonobject;
        JSONArray jsonarray;
        ListView listview;
        int ll;
        MyOrderAdapter adapter;
     String ss=null;
       MyOrder my;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myorder);
          getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));


          MainApplication mainApplication =  (MainApplication) getApplication();
            mLoginSystem = mainApplication.getLoginSystem();
            mProgressDialog = new ProgressDialog(this);
           // activity = this;
           // mCities = new ArrayList<MyOrderItemData2>();
            //mItemsAdapter = new MenuItemsAdapter(MyorderActivity.this, mCities);
           //mListView = (ListView)findViewById(R.id.listView156767577);

            ll=mLoginSystem.getUserId();
            ss=String.valueOf(ll);
            Log.i("llllllllllll",""+ll);

            Log.i("llllllllllll",ss);
            new DownloadJSON().execute();

    }

     // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void,Void,Void> {

        @Override

        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MyorderActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Downloading Restaurant....");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
            Toast.makeText(MyorderActivity.this, "Successs", Toast.LENGTH_LONG).show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            mCities = new ArrayList<MyOrder>();
            // Retrieve JSON Objects from the given URL address
            Log.i("123",""+ll);
            jsonobject = JSONfunctions.getJSONfromURL("http://firstchoicefood.in/fcfapiphpexpert/phpexpert_OrderDisplay.php?user_id"+ ll);
            Log.i("1234", "" +jsonobject);

                // Toast.makeText(MyorderActivity.this,"sharma",Toast.LENGTH_LONG).show();
            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("OrderViewResult");
                Log.i("1234", "" + jsonarray);
          //  Toast.makeText(MyorderActivity.this,"sunder",Toast.LENGTH_LONG).show();
                for (int i = 0; i < jsonarray.length(); i++) {

                    jsonobject = jsonarray.getJSONObject(i);
                   my=new MyOrder(jsonobject.getString("order_identifyno"),jsonobject.getString("restaurant_name"),jsonobject.getString("type"),jsonobject.getString("ordPrice"),jsonobject.getString("restaurant_id"),jsonobject.getString("user_date"));
                    mCities.add(my);
                    Log.i("my",""+my);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listView156767577);
            // Pass the results into ListViewAdapter.java
            adapter = new MyOrderAdapter(MyorderActivity.this,R.layout.myorderadapter,mCities);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();

        }


    }

我有另一个类。有customer.Name的用户ID是LoginSystem。

public void tryRegistration(String name,String email,String password,String phone,                             布尔订阅,                             final LoginSystemListener loginSystemListener){     ObjectGetter objectGetter = new ObjectGetter();     RequestParams requestParams = new RequestParams();

requestParams.put("user_name", name);
requestParams.put("user_email", email);
requestParams.put("user_pass", password);
requestParams.put("user_cellphone", phone);
requestParams.put("offerWant", subscription);

Log.d(TAG, "Attempting registration...");

objectGetter.postJsonObjectOrDialog(mContext, Urls.API_REGISTER, requestParams,
        GenericResponse.class, new ObjectGetter.OnFinishedListener<GenericResponse>() {
            @Override
            public void onFinishedLoadingObject(GenericResponse response) {
                if (response != null && response.isSucceded()) {
                    Log.d(TAG, "Registration succeded.");

                    loginSystemListener.onUserActionFinished(true);
                } else {
                    Log.d(TAG, "Registration error.");
                    loginSystemListener.onUserActionFinished(false);
                }
            }
        });

}

和适配器

public class MyOrderAdapter extends ArrayAdapter<MyOrder>{
    Context context;

    public MyOrderAdapter(Context context, int resourceId,
                                 ArrayList<MyOrder> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        TextView oderIN;
        TextView name;
        TextView type;
        TextView price;
        TextView showdetails;
        TextView date1;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        MyOrder rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView =mInflater.inflate(R.layout.myorderadapter,null);
            holder = new ViewHolder();
            holder.oderIN = (TextView) convertView.findViewById(R.id.textViewwerttt);
            holder.name = (TextView) convertView.findViewById(R.id.textView123455);
            holder.type = (TextView) convertView.findViewById(R.id.textView123);
            holder.price = (TextView) convertView.findViewById(R.id.textView1234354);
            holder.showdetails = (TextView) convertView.findViewById(R.id.textView1234354gfdfg);
            holder.date1 = (TextView) convertView.findViewById(R.id.textView123455sadsa);

            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.oderIN.setText(rowItem.getoderIN());
        holder.name.setText(rowItem.getName());
        holder.type.setText(rowItem.getType());
        holder.price.setText(rowItem.getPrice());
        holder.date1.setText(rowItem.getDate1());


 holder.showdetails.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
      //   context.startActivity(new Intent(context, ShowDetailsActivity.class));

     }
 });

        return convertView;

    }

错误是

06-11 13:48:16.365: E/AndroidRuntime(3062): FATAL EXCEPTION: main
06-11 13:48:16.365: E/AndroidRuntime(3062): Process: com.example.firstchoicefood, PID: 3062
06-11 13:48:16.365: E/AndroidRuntime(3062): java.lang.NullPointerException
06-11 13:48:16.365: E/AndroidRuntime(3062):     at 
06-11 13:48:16.365: E/AndroidRuntime(3062):     at com.example.firstchoicefood.MyorderActivity$DownloadJSON.onPostExecute(MyorderActivity.java:1)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at android.os.AsyncTask.finish(AsyncTask.java:632)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at android.os.Looper.loop(Looper.java:136)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at java.lang.reflect.Method.invoke(Method.java:515)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-11 13:48:16.365: E/AndroidRuntime(3062):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案