如何从MYSQL数据库中获取数据并将其显示在应用程序上?

时间:2016-09-21 07:46:12

标签: java android mysql json listview

我想使用compile 'com.squareup.okhttp3:okhttp:3.4.1' 从数据库中获取数据我尝试了很多,但我面临的问题是数据没有显示在屏幕上。

数据完美呈现,但未显示在屏幕上。 陷入困境请帮帮我..

这里,完整代码:

Order.java

Json

下面,的 ListviewAdapter3.java

    package com.example.sachin.splashlogin;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

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 {

    public Order(){};

    JSONObject jsonobject;
    JSONArray ownerObj;
    ListView listview;


    ListViewAdapter3 listadapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    String uid = "0";
    SessionManager session;

    private static String url_visitor = "http://10.0.2.2/portal/fetchinforder.php";
    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> o_username= new ArrayList<String>();
    View view;


    @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("Visit");
       // listview = (ListView) view.findViewById(R.id.lvvisit);

        session = new SessionManager(getActivity());
        HashMap<String, String> user = session.getUserDetails();
        uid = user.get(SessionManager.KEY_ID);

        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("o_username", uid));
                JSONObject json = jParser.makeHttpRequest(url_visitor, "GET", params);

                int success1 = Integer.parseInt(json.getString("success4"));
                Log.d("success4", json.toString());

                if (success1 == 0) {
                    Snackbar.make(view, "Not Data Found", Snackbar.LENGTH_LONG).show();
                }
                if (success1 == 1) {
                    ownerObj = json.getJSONArray("Ordera");
                    // arraylist = new ArrayList<HashMap<String, String>>();
                    // Retrieve JSON Objects from the given URL address
                    // Locate the array name in JSON
                    // jsonarray = jsonobject.getJSONArray("images");
                    // JSONObject own = ownerObj.getJSONObject(0);
                    // display product data in EditText
                    // uid = own.getString("user_id");

                    for (int i = 0; i < ownerObj.length(); i++) {
                        //   HashMap<String, String> map = new HashMap<String, String>();
                        jsonobject = ownerObj.getJSONObject(i);
                        // Retrive JSON Objects
                        if (jsonobject.getString("o_username").equalsIgnoreCase(uid)) {


                                itemwod.add(jsonobject.getString("itm_wod"));
                                itemorder.add(jsonobject.getString("itm_order"));
                                item_remark.add(jsonobject.getString("itm_remark"));
                                //   o_username.add(jsonobject.getString("o_username"));
                            }
                        }
                    }
                }
                catch(Exception e)
                {
                }
                return null;
            }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml

            listview = (ListView)getActivity().findViewById(R.id.list123);
            listadapter = new ListViewAdapter3(getActivity(), itemwod,item_remark,itemorder);
            // Set the adapter to the ListView
            listview.setAdapter(listadapter);
            // Close the progressdialog

            mProgressDialog.dismiss();
        }
    }


}

我创建了1 raw_order.xml activity_order.xml

这里, Logcat 在这个数据正在取得正确。

    package com.example.sachin.splashlogin;

import android.app.AlertDialog;
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;
    AlertDialog.Builder alertDialogBuilder;
    int loader = R.drawable.ic_menu_gallery;


    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;
    HashMap<String, String> resultp = new HashMap<String, String>();



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


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

        alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setMessage("Do You Want To Call....");


    }

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

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

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

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

        TextView lvtaskname,lvtaskdetails,lvtaskremark,lvtaskdate;

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

        LayoutInflater inflater = LayoutInflater.from(cntx);

        convertView = inflater.inflate(R.layout.raw_visit, parent,
                false);


        lvtaskname= (TextView) convertView.findViewById(R.id.lvtaskname);
        lvtaskdetails = (TextView) convertView.findViewById(R.id.lvtaskdetails);
        lvtaskremark = (TextView) convertView.findViewById(R.id.lvtaskremark);
        //lvtaskdate = (TextView) convertView.findViewById(R.id.lvtaskdate);

        lvtaskname.setText(itemwod.get(position));
        lvtaskdetails.setText(itemorder.get(position));
        lvtaskremark.setText(item_remark.get(position));
       //lvtaskdate.setText(item_date.get(position));


        return convertView;
    }

}

我不明白这有什么不对?请帮帮我..

2 个答案:

答案 0 :(得分:0)

我认为你的问题就在这一行:

    if (jsonobject.getString("o_username").equalsIgnoreCase(uid)) {

你的&#39; uid&#39;是&#34; 0&#34;

    String uid = "0";

所以你的if语句返回false。

答案 1 :(得分:0)

我想你忘记了setText - &gt;

parenthesis