如何通过Json从数据库中获取数据?

时间:2022-09-25 15:51:40

i want to fetch a data from the database while i select the spinner item. here i pasted the complete code of my project..

我想在选择微调项目时从数据库中获取数据。在这里,我粘贴了我的项目的完整代码..

Order.java

package com.example.sachin.omcommunication;

import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Order extends Fragment {
    private Spinner today;
    private String[] strtoday;
    private TextView orderWOD,order,order_remarks,order_date;
    private ListView lv;
    private Button btnorder;
    Activity activity;


    private static final String TAG_SUCCESS = "success";
    static String TAG_RESULTS = "Order";
    //static String TAG_VISIT = "VISIT";
    static String TAG_O_PARTIES= "o_parties";
    static String TAG_O_PRODUCT= "o_product";
    static String TAG_O_REMARK = "o_remark";
    static String TAG_O_DATE= "o_date";
    String cat = "21";

    String strtodaystr;
    JSONObject jsonobject;
    //JSONArray jsonarray;
    //  String img;
    ListViewAdapter3 listadapter3;
    ProgressDialog mProgressDialog;

    ArrayList<HashMap<String, String>> arraylist;
    // private static String url_profile ="http://websofthub.co.in/fetchmember.php";
    private static String url_profile = "http://10.0.2.2/portal/fetchorder.php";
    //  String g;

    JSONParser jParser = new JSONParser();

    ArrayList<String> itemwod = new ArrayList<String>();
    ArrayList<String> itemorder = new ArrayList<String>();
    ArrayList<String> item_remark= new ArrayList<String>();
    ArrayList<String> item_date = new ArrayList<String>();


    View view;
    public Order(){};


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // don't look at this layout it's just a listView to show how to handle the keyboard
        View view = inflater.inflate(R.layout.activity_order, container, false);
        getActivity().setTitle("Order");
        lv = (ListView) view.findViewById(R.id.listvieworder);
        today = (Spinner) view.findViewById(R.id.today);
        btnorder = (Button) view.findViewById(R.id.btnorder);

        orderWOD = (TextView) view.findViewById(R.id.orderWOD);
        order = (TextView) view.findViewById(R.id.order);
        order_remarks = (TextView) view.findViewById(R.id.order_remarks);
        order_date = (TextView) view.findViewById(R.id.order_date);

        strtoday = getResources().getStringArray(R.array.visit);
        ArrayAdapter<String> adaptertoday= new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, strtoday);
        adaptertoday.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        today.setAdapter(adaptertoday);


        btnorder.setOnClickListener(new View.OnClickListener() {
            @Override
           public void onClick(View v) {
                strtodaystr = today.getSelectedItem().toString();

                //listvieworder.clearChoices();
                itemwod.clear();
                itemorder.clear();
                item_remark.clear();
                item_date.clear();

                new DownloadJSON().execute();

            }
        });
        return  view;
    }

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(getActivity());

            mProgressDialog.setTitle("Please Wait");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();

        }
        @Override
        protected Void doInBackground(Void... args) {
            // Create an array
            try {
                arraylist = new ArrayList<HashMap<String, String>>();

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair(TAG_O_DATE, strtodaystr));
                JSONObject json = jParser.makeHttpRequest(url_profile, "GET", params);
                int success1 = Integer.parseInt(json.getString(TAG_SUCCESS));
                Log.d("success", json.toString());
                if (success1 == 0) {
                    Snackbar.make(view, "Not Data Found", Snackbar.LENGTH_LONG).show();
                }
                if (success1 == 1) {
                    JSONArray ownerObj = json.getJSONArray("Ordera");

                    for (int i = 0; i < ownerObj.length(); i++) {

                        jsonobject = ownerObj.getJSONObject(i);

                        //if (jsonobject.getString(TAG_M_YEAR).equalsIgnoreCase(stryearstr) || jsonobject.getString(TAG_M_MONTH).equalsIgnoreCase(strmonthstr)) {
                        itemwod.add(jsonobject.getString(TAG_O_PARTIES));
                        itemorder.add(jsonobject.getString(TAG_O_PRODUCT));
                        item_remark.add(jsonobject.getString(TAG_O_REMARK));
                        item_date.add(jsonobject.getString(TAG_O_DATE));


                        Log.d("today", itemwod.get(0));
                        //}
                    }
                }
            } catch (Exception e) {

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listadapter3 = new ListViewAdapter3(getActivity(), itemwod, itemorder, item_remark, item_date);

            // Set the adapter to the ListView
            lv.setAdapter(listadapter3);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }
}

now,Listviewadapter3.java

package com.example.sachin.omcommunication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class ListViewAdapter3 extends BaseAdapter {
    Context cntx;

    ArrayList<String> itemwod = new ArrayList<String>();
    ArrayList<String> itemorder = new ArrayList<String>();
    ArrayList<String> item_remark= new ArrayList<String>();
    ArrayList<String> item_date = new ArrayList<String>();

    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;


    public ListViewAdapter3(Context context,
                            ArrayList<String> itm_wod,
                            ArrayList<String> itm_order,
                            ArrayList<String> itm_remark,
                            ArrayList<String> itm_date
    ) {
        // TODO Auto-generated constructor stub
        cntx = context;


        itemwod = itm_wod;
        itemorder = itm_order;
        item_remark = itm_remark;
        item_date = itm_date;


    }

    @Override
    public int getCount() {
        return itemorder.size();
    }

    @Override
    public Object getItem(int position) {
        return itemorder.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("deprecation")
    public View getView(final int position, View convertView, ViewGroup parent) {

        TextView orderWOD, order, order_remarks, order_date;

        inflater = (LayoutInflater) cntx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LayoutInflater inflater = LayoutInflater.from(cntx);
        convertView = inflater.inflate(R.layout.raw_order, parent,
                false);


        orderWOD = (TextView) convertView.findViewById(R.id.orderWOD);
        order = (TextView) convertView.findViewById(R.id.order);
        order_remarks = (TextView) convertView.findViewById(R.id.order_remarks);
        order_date = (TextView) convertView.findViewById(R.id.order_date);

        orderWOD.setText(itemwod.get(position));
        order.setText(itemorder.get(position));
        order_remarks.setText(item_remark.get(position));
        order_date.setText(item_date.get(position));


        return convertView;
    }

}

my both java file are pasted..

我的两个java文件都被粘贴了..

and now, .xml file

现在,.xml文件

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.sachin.omcommunication.Order">
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <Spinner
            android:id="@+id/today"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center"
            android:entries="@array/visit"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit"
            android:id="@+id/btnorder"
            android:layout_weight="1"
            android:textSize="20dp"/>

    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="10dp"
        android:layout_weight="10"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3">

            <View
                android:layout_width="match_parent"
                android:layout_height="4dp"
                android:background="#a35870"></View>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="98"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="17"
                android:background="#FFAE5E5E"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="98"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/orderWOD"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:text="WOD"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="#000"
                        android:textSize="13dp"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:orientation="horizontal">

                    <View
                        android:layout_width="2dp"
                        android:layout_height="match_parent"
                        android:background="#fff"></View>
                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="18"
                android:background="#FFAE5E5E"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="98"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/order"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:text="Order"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="#000"
                        android:textSize="13dp"
                        android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:orientation="horizontal">

                    <View
                        android:layout_width="2dp"
                        android:layout_height="match_parent"
                        android:background="#fff"></View>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="18"
                android:background="#FFAE5E5E"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="98"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/order_remarks"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:text="Remarks"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textColor="#000"
                        android:textSize="13dp"
                        android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:orientation="horizontal">

                    <View
                        android:layout_width="2dp"
                        android:layout_height="match_parent"
                        android:background="#fff"></View>
                </LinearLayout>
            </LinearLayout>


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="14"
                android:background="#FFAE5E5E"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/order_date"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:text="Date"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#000"
                    android:textSize="13dp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="vertical">

            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="#a35870"></View>

        </LinearLayout>
    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="10dp"
        android:layout_weight="73"
        android:orientation="vertical">

        <ListView
            android:id="@+id/listvieworder"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

i created a raw.xml for displaying the listview data. nd lastly i get a error like.

我创建了一个raw.xml来显示listview数据。最后我得到一个错误。

 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.sachin.omcommunication, PID: 23880
 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
 at android.view.LayoutInflater.from(LayoutInflater.java:229)
 at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:178)
 at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137)
 at com.example.sachin.omcommunication.Order.onCreateView(Order.java:86)
 at android.app.Fragment.performCreateView(Fragment.java:2220)
 at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
 at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
 at android.app.BackStackRecord.run(BackStackRecord.java:793)
 at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
 at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-31 13:18:35.120 23880-23880/com.example.sachin.omcommunication I/Process: Sending signal. PID: 23880 SIG: 9

1 个解决方案

#1


1  

you got NullPointerException at com.example.sachin.omcommunication.Order.onCreateView(Order.java:86) because you was not init activity in your fragment init your activity before use.

你在com.example.sachin.omcommunication.Order.onCreateView(Order.java:86)得到了NullPointerException,因为你在使用之前没有在你的活动片段中初始化你的活动。

activity = getActivity();

#1


1  

you got NullPointerException at com.example.sachin.omcommunication.Order.onCreateView(Order.java:86) because you was not init activity in your fragment init your activity before use.

你在com.example.sachin.omcommunication.Order.onCreateView(Order.java:86)得到了NullPointerException,因为你在使用之前没有在你的活动片段中初始化你的活动。

activity = getActivity();