I got the values from the Internet and put the data from the GRID_DATA array into the grid, working separately, and both worked fine. I combined them together when the data from JSONObject adds the values to the JSON array, so from GRID_DATA we get the rows needed to represent GRID. But I get some errors.
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class QGFragment extends Fragment {
GridView grdView;
String strurl;
private static String url = "http://localhost/app/data.php";
private static final String TAG_QP = "qp";
private static final String TAG_NAME = "name";
JSONArray qp = null;
static String[] GRID_DATA;
public QGFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_qg, container, false);
grdView = (GridView)rootView.findViewById(R.id.gridview);
new JSONParse().execute();
grdView.setAdapter( new GridAdapter( getActivity(), GRID_DATA ) );
return rootView;
}
class GridAdapter extends BaseAdapter {
private Context context;
private final String[] gridValues;
public GridAdapter(Context context, String[ ] gridValues) {
this.context = context;
this.gridValues = gridValues;
}
@Override
public int getCount() {
return gridValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate( R.layout.grid_list , null);
TextView textView = (TextView) gridView
.findViewById(R.id.icon_text);
textView.setText(gridValues[position]);
ImageView imageView = (ImageView) gridView
.findViewById(R.id.icon_image);
imageView.setImageResource(R.drawable.ic_launcher);
} else {
gridView = (View) convertView;
}
return gridView;
}
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
qp = json.getJSONArray(TAG_QP);
for(int i = 0 ; i < qp.length(); i++){
JSONObject c = qp.getJSONObject(i);
String name = c.getString(TAG_NAME);
GRID_DATA[i] = name;
};
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
Log Code:
12-24 20:53:06.702: W/dalvikvm(4304): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-24 20:53:06.743: E/AndroidRuntime(4304): FATAL EXCEPTION: main
12-24 20:53:06.743: E/AndroidRuntime(4304): java.lang.NullPointerException
12-24 20:53:06.743: E/AndroidRuntime(4304): at com.qrodsintegrated.QGFragment$GridAdapter.getCount(QGFragment.java:74)
12-24 20:53:06.743: E/AndroidRuntime(4304): at android.widget.GridView.setAdapter(GridView.java:180)
12-24 20:53:06.743: E/AndroidRuntime(4304): at com.qrodsintegrated.QGFragment.onCreateView(QGFragment.java:51)
12-24 20:53:06.743: E/AndroidRuntime(4304): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
12-24 20:53:06.743: E/AndroidRuntime(4304): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
12-24 20:53:06.743: E/AndroidRuntime(4304): at android.app.BackStackRecord.run(BackStackRecord.java:622)
12-24 20:53:06.743: E/AndroidRuntime(4304): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
12-24 20:53:06.743: E/AndroidRuntime(4304): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:417)
12-24 20:53:06.743: E/AndroidRuntime(4304): at android.os.Handler.handleCallback(Handler.java:605)
12-24 20:53:06.743: E/AndroidRuntime(4304): at android.os.Handler.dispatchMessage(Handler.java:92)
12-24 20:53:06.743: E/AndroidRuntime(4304): at android.os.Looper.loop(Looper.java:137)
12-24 20:53:06.743: E/AndroidRuntime(4304): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-24 20:53:06.743: E/AndroidRuntime(4304): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 20:53:06.743: E/AndroidRuntime(4304): at java.lang.reflect.Method.invoke(Method.java:511)
12-24 20:53:06.743: E/AndroidRuntime(4304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-24 20:53:06.743: E/AndroidRuntime(4304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-24 20:53:06.743: E/AndroidRuntime(4304): at dalvik.system.NativeStart.main(Native Method)
12-24 20:53:08.722: I/Process(4304): Sending signal. PID: 4304 SIG: 9
source
share