user6880336 user6880336 - 1 month ago 8
Android Question

How to fetch a data and set it to TextView?

I want to fetch a data from the mysql database I tried but not getting the output also tried tutorials.

Here my complete code:-

package com.example.sachin.splashlogin;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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 Home extends Fragment {
Activity activity;

public Home() {};

TextView visit;
SessionManager session;
CardView cardviewvisit, cardvieworder, cardviewpayment, cardviewdelivery;
JSONObject jsonobject;
private static String url_visitor = "http://10.0.2.2/portal/fetchvisit.php";
JSONParser jParser = new JSONParser();
JSONArray ownerObj, jsonarray;

ArrayList<HashMap<String, String>> arraylist;
ArrayList<String> v_username = new ArrayList<String>();
ArrayList<String> v_parties = new ArrayList<String>();
ArrayList<String> v_date = new ArrayList<String>();
String suid, uid, wt_wod_code1, wt_party1;
View view;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_home, container, false);
getActivity().setTitle("Home");
cardviewvisit = (CardView) view.findViewById(R.id.cardviewvisit);
cardvieworder = (CardView) view.findViewById(R.id.cardvieworder);
cardviewpayment = (CardView) view.findViewById(R.id.cardviewpayment);
cardviewdelivery = (CardView) view.findViewById(R.id.cardviewdelivery);
session = new SessionManager(getActivity());
HashMap<String, String> user = session.getUserDetails();
uid = user.get(SessionManager.KEY_ID);
visit = (TextView) view.findViewById(R.id.visit1);
cardviewvisit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Fragment fragment = new DeliveryTab();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.framelayout, fragment).commit();

}
});

new DownloadJSON().execute();
return view;
}


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

@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("v_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("visit");
for (int i = 0; i < ownerObj.length(); i++) {
jsonobject = ownerObj.getJSONObject(i);
v_username.add(jsonobject.getString("v_username"));
v_parties.add(jsonobject.getString("v_parties"));
}
}
} catch (Exception e) {
}
return null;
}

@Override
protected void onPostExecute(Void args) {

visit=(TextView)view.findViewById(R.id.visit1);
visit.setText(visit.getText());

}
}
}


Here is my logcat, the data is displaying in this.

10-12 19:40:58.222 31209-31277/com.example.sachin.splashlogin D/P: [v_username=JAYESHBHAI]
10-12 19:40:58.222 31209-31277/com.example.sachin.splashlogin E/Json Object:: {"visit":[{"v_parties":"WONDAR MOBILE"}],"success":1}


I wanted to print parties name in the TextView.

If needed I'll also post my API in this..

please help me out from this..

Answer

Example to access:

JSONObject json = new JSONObject(YourStringJSON_VARIABLE);

 String parties = json.getString("v_parties");

 Set:
 visit=(TextView)view.findViewById(R.id.visit1);
 visit.setText(parties);

To access a Array:

 JSONArray listArray = result.getJSONArray("your_list");
 for(int i = 0; i < listArray.length(); i++) {
                JSONObject jsonOb = listArray.getJSONObject(i);
                //store your variable
                String something = jsonOb.getString("something");


 }
visit=(TextView)view.findViewById(R.id.visit1);  
visit.setText(something);
Comments