ARIJIT DAS
Lurker
<p dir='ltr' style='margin-top:0; margin-bottom:0; text-align:start;'>Hello,</p>
I am new to Android Development. Now I am wanting to display data to a gridview from a php made web api. It was working nicely But got stopped and I am unable to find out the issue.
My java file code, as follows:- -----------------------------------------------
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import java.util.ArrayList;
public class ViewRecord extends AppCompatActivity {
private final ArrayList<String> val = new ArrayList<String>();
private ArrayAdapter<String> adapter;// = new ArrayAdapter(this, android.R.layout.simple_list_item_1,val2);
private GridView gv;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_record);
//String val3='';
//ArrayList<String> val2 = new ArrayList<String>();
//TextView tv1 = (TextView) findViewById(R.id.editText2 hi);
//val2.add(tv1.getText().toString());
//ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,val2);
gv= (GridView)findViewById(R.id.gridView1);
adapter= new ArrayAdapter(this, android.R.layout.simple_list_item_1,val);
//adapter= new ArrayAdapter(this, R.layout.gridview_dashboard);
gv.setAdapter(adapter);
// we will using AsyncTask during parsing
new AsyncTaskParseJson().execute();
}
// you can make this class as another java file so it will be separated from your main activity.
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = 'AsyncTaskParseJson.java';
// set your json string url here
String yourJsonStringUrl = 'http://... ';
// contacts JSONArray
JSONArray dataJsonArr = null;
//public String val='';
//ArrayList<String> val = new ArrayList<String>();
@override
protected void onPreExecute() {}
@override
protected String doInBackground(String... arg0) {
try {
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray('DocumentSequence');
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing each json item in variable
String ID_TXN = c.getString('idtransactions');
String FROM = c.getString('from');
String TO = c.getString('to');
String OTH_EXT_NAME = c.getString('other_external_name');
String PAYMDODE = c.getString('paymentMode');
String FLDN1 = c.getString('pmd_fieldname1');
String FLDV1 = c.getString('pmd_fieldvalue1');
String FLDN2 = c.getString('pmd_fieldname2');
String FLDV2 = c.getString('pmd_fieldvalue2');
String FLDN3 = c.getString('pmd_fieldname3');
String FLDV3 = c.getString('pmd_fieldvalue3');
String FLDN4 = c.getString('pmd_fieldname4');
String FLDV4 = c.getString('pmd_fieldvalue4');
String FLDN5 = c.getString('pmd_fieldname5');
String FLDV5 = c.getString('pmd_fieldvalue5');
String FLDN6 = c.getString('pmd_fieldname6');
String FLDV6 = c.getString('pmd_fieldvalue6');
String FLDN7 = c.getString('pmd_fieldname7');
String FLDV7 = c.getString('pmd_fieldvalue7');
String FLDN8 = c.getString('pmd_fieldname8');
String FLDV8 = c.getString('pmd_fieldvalue8');
String FLDN9 = c.getString('pmd_fieldname9');
String FLDV9 = c.getString('pmd_fieldvalue9');
String FLDN10 = c.getString('pmd_fieldname10');
String FLDV10 = c.getString('pmd_fieldvalue10');
String NAME_OF_TXN_PERSON = c.getString('transactionMadeByThePerson');
String AMT = c.getString('amount');
String PURPOSE = c.getString('purpose');
String INITIATED_BY = c.getString('initiatedby');
String DATE = c.getString('date');
String TIME = c.getString('time');
String NOTE = c.getString('note');
String ENTRY_BY = c.getString('dataEnteredBy');
//String username = c.getString('city');
// show the values in our logcat
Log.e(TAG, 'From: ' + FROM + ', To: ' + TO +', Other External Name: ' + OTH_EXT_NAME + ', Payment Mode: ' + PAYMDODE + ', FLDN1: ' + FLDN1 + ', FLDV1:' +FLDV1+ ', FLDN2: ' + FLDN2 + ', FLDV2:' +FLDV2+ ', FLDN3: ' + FLDN3 + ', FLDV3:' +FLDV3+ ', FLDN4: ' + FLDN4 + ', FLDV4:' +FLDV4+ ', FLDN5: ' + FLDN5 + ', FLDV5:' +FLDV5 +', FLDN6: ' + FLDN6 + ', FLDV6:' +FLDV6+ ', FLDN7: ' + FLDN7 + ', FLDV7:' +FLDV7+ ', FLDN8: ' + FLDN8 + ', FLDV8:' +FLDV8+ ', FLDN9: ' + FLDN9 + ', FLDV9:' +FLDV9+ ', FLDN10: ' + FLDN10 + ', FLDV10:' + FLDV10 + ', TXN MADE BY:' + NAME_OF_TXN_PERSON + ', AMOUNT:' + AMT + ', PURPOSE:' + PURPOSE + ', INTIATED BY:' + INITIATED_BY + ', DATE:' + DATE + ', TIME:' + TIME + ', NOTE:' + NOTE + ', ENTRY BY:' + ENTRY_BY);
val.add('From: ' + FROM + ', To: ' + TO +', Other External Name: ' + OTH_EXT_NAME + ', Payment Mode: ' + PAYMDODE + ', FLDN1: ' + FLDN1 + ', FLDV1:' + FLDV1 + ', FLDN2: ' + FLDN2 + ', FLDV2:' + FLDV2 + ', FLDN3: ' + FLDN3 + ', FLDV3:' + FLDV3 + ', FLDN4: ' + FLDN4 + ', FLDV4:' + FLDV4 + ', FLDN5: ' + FLDN5 + ', FLDV5:' + FLDV5 + ', FLDN6: ' + FLDN6 + ', FLDV6:' + FLDV6 + ', FLDN7: ' + FLDN7 + ', FLDV7:' + FLDV7 + ', FLDN8: ' + FLDN8 + ', FLDV8:' + FLDV8 + ', FLDN9: ' + FLDN9 + ', FLDV9:' + FLDV9 + ', FLDN10: ' + FLDN10 + ', FLDV10:' + FLDV10 + ', TXN MADE BY:' + NAME_OF_TXN_PERSON + ', AMOUNT:' + AMT + ', PURPOSE:' + PURPOSE + ', INTIATED BY:' + INITIATED_BY + ', DATE:' + DATE + ', TIME:' + TIME + ', NOTE:' + NOTE + ', ENTRY BY:' + ENTRY_BY);
//val.add('From: ' + FROM + ', To: ' + TO +', Other External Name: ' + OTH_EXT_NAME);
//val=('From: ' + FROM + ', To: ' + TO +'\n');
//}
//else
//
//}
}
} catch (JSONException e) {
e.printStackTrace();
}
return val.toString();
}
@override
protected void onPostExecute(String strFromDoInBg)
{
//TextView tv1 = (TextView) findViewById(R.id.editText2);
//tv1.setText(val.toString());
//ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,val);
//GridView gv=(GridView)findViewById(R.id.gridView1);
//gv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
-----------------------------------------------
And following is my associated xml file for the above java file:- --------------------
<?xml version='1.0' encoding='utf-8'?>
<android.support.constraint.ConstraintLayout xmlns:android='http://schemas.android.com/apk/res/android'
xmlns:app='http://schemas.android.com/apk/res-auto'
xmlns:tools='http://schemas.android.com/tools'
android:layout_width='match_parent'
android:layout_height='match_parent'
android:background='@android:color/holo_blue_light'
tools:context='com.meticuloustech.myaccounts.ViewRecord'>
<TextView
android:id='@+id/lbl_viewRecord'
android:layout_width='129dp'
android:layout_height='48dp'
android:ems='10'
android:inputType='textPersonName'
android:labelFor='@id/lbl_viewRecord'
android:text='View Records'
android:textColor='@android:color/background_dark'
android:textStyle='bold'
app:layout_constraintBottom_toTopOf='parent'
app:layout_constraintLeft_toRightOf='parent'
app:layout_constraintRight_toLeftOf='parent'
app:layout_constraintTop_toBottomOf='parent'
app:layout_constraintVertical_bias='0.03'
app:layout_constraintHorizontal_bias='0.561' />
<GridView
android:id='@+id/gridView1'
android:layout_width='300dp'
android:layout_height='432dp'
android:layout_marginBottom='0dp'
android:layout_marginLeft='0dp'
android:layout_marginRight='0dp'
android:layout_marginTop='0dp'
android:clickable='false'
app:layout_constraintBottom_toTopOf='parent'
app:layout_constraintLeft_toRightOf='parent'
app:layout_constraintRight_toLeftOf='parent'
app:layout_constraintTop_toBottomOf='parent'
app:layout_constraintVertical_bias='0.837' />
</android.support.constraint.ConstraintLayout>
------------------------------
Please let me know where the problem is and oblige me there by.
Thanks.
I am new to Android Development. Now I am wanting to display data to a gridview from a php made web api. It was working nicely But got stopped and I am unable to find out the issue.
My java file code, as follows:- -----------------------------------------------
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import java.util.ArrayList;
public class ViewRecord extends AppCompatActivity {
private final ArrayList<String> val = new ArrayList<String>();
private ArrayAdapter<String> adapter;// = new ArrayAdapter(this, android.R.layout.simple_list_item_1,val2);
private GridView gv;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_record);
//String val3='';
//ArrayList<String> val2 = new ArrayList<String>();
//TextView tv1 = (TextView) findViewById(R.id.editText2 hi);
//val2.add(tv1.getText().toString());
//ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,val2);
gv= (GridView)findViewById(R.id.gridView1);
adapter= new ArrayAdapter(this, android.R.layout.simple_list_item_1,val);
//adapter= new ArrayAdapter(this, R.layout.gridview_dashboard);
gv.setAdapter(adapter);
// we will using AsyncTask during parsing
new AsyncTaskParseJson().execute();
}
// you can make this class as another java file so it will be separated from your main activity.
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = 'AsyncTaskParseJson.java';
// set your json string url here
String yourJsonStringUrl = 'http://... ';
// contacts JSONArray
JSONArray dataJsonArr = null;
//public String val='';
//ArrayList<String> val = new ArrayList<String>();
@override
protected void onPreExecute() {}
@override
protected String doInBackground(String... arg0) {
try {
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray('DocumentSequence');
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing each json item in variable
String ID_TXN = c.getString('idtransactions');
String FROM = c.getString('from');
String TO = c.getString('to');
String OTH_EXT_NAME = c.getString('other_external_name');
String PAYMDODE = c.getString('paymentMode');
String FLDN1 = c.getString('pmd_fieldname1');
String FLDV1 = c.getString('pmd_fieldvalue1');
String FLDN2 = c.getString('pmd_fieldname2');
String FLDV2 = c.getString('pmd_fieldvalue2');
String FLDN3 = c.getString('pmd_fieldname3');
String FLDV3 = c.getString('pmd_fieldvalue3');
String FLDN4 = c.getString('pmd_fieldname4');
String FLDV4 = c.getString('pmd_fieldvalue4');
String FLDN5 = c.getString('pmd_fieldname5');
String FLDV5 = c.getString('pmd_fieldvalue5');
String FLDN6 = c.getString('pmd_fieldname6');
String FLDV6 = c.getString('pmd_fieldvalue6');
String FLDN7 = c.getString('pmd_fieldname7');
String FLDV7 = c.getString('pmd_fieldvalue7');
String FLDN8 = c.getString('pmd_fieldname8');
String FLDV8 = c.getString('pmd_fieldvalue8');
String FLDN9 = c.getString('pmd_fieldname9');
String FLDV9 = c.getString('pmd_fieldvalue9');
String FLDN10 = c.getString('pmd_fieldname10');
String FLDV10 = c.getString('pmd_fieldvalue10');
String NAME_OF_TXN_PERSON = c.getString('transactionMadeByThePerson');
String AMT = c.getString('amount');
String PURPOSE = c.getString('purpose');
String INITIATED_BY = c.getString('initiatedby');
String DATE = c.getString('date');
String TIME = c.getString('time');
String NOTE = c.getString('note');
String ENTRY_BY = c.getString('dataEnteredBy');
//String username = c.getString('city');
// show the values in our logcat
Log.e(TAG, 'From: ' + FROM + ', To: ' + TO +', Other External Name: ' + OTH_EXT_NAME + ', Payment Mode: ' + PAYMDODE + ', FLDN1: ' + FLDN1 + ', FLDV1:' +FLDV1+ ', FLDN2: ' + FLDN2 + ', FLDV2:' +FLDV2+ ', FLDN3: ' + FLDN3 + ', FLDV3:' +FLDV3+ ', FLDN4: ' + FLDN4 + ', FLDV4:' +FLDV4+ ', FLDN5: ' + FLDN5 + ', FLDV5:' +FLDV5 +', FLDN6: ' + FLDN6 + ', FLDV6:' +FLDV6+ ', FLDN7: ' + FLDN7 + ', FLDV7:' +FLDV7+ ', FLDN8: ' + FLDN8 + ', FLDV8:' +FLDV8+ ', FLDN9: ' + FLDN9 + ', FLDV9:' +FLDV9+ ', FLDN10: ' + FLDN10 + ', FLDV10:' + FLDV10 + ', TXN MADE BY:' + NAME_OF_TXN_PERSON + ', AMOUNT:' + AMT + ', PURPOSE:' + PURPOSE + ', INTIATED BY:' + INITIATED_BY + ', DATE:' + DATE + ', TIME:' + TIME + ', NOTE:' + NOTE + ', ENTRY BY:' + ENTRY_BY);
val.add('From: ' + FROM + ', To: ' + TO +', Other External Name: ' + OTH_EXT_NAME + ', Payment Mode: ' + PAYMDODE + ', FLDN1: ' + FLDN1 + ', FLDV1:' + FLDV1 + ', FLDN2: ' + FLDN2 + ', FLDV2:' + FLDV2 + ', FLDN3: ' + FLDN3 + ', FLDV3:' + FLDV3 + ', FLDN4: ' + FLDN4 + ', FLDV4:' + FLDV4 + ', FLDN5: ' + FLDN5 + ', FLDV5:' + FLDV5 + ', FLDN6: ' + FLDN6 + ', FLDV6:' + FLDV6 + ', FLDN7: ' + FLDN7 + ', FLDV7:' + FLDV7 + ', FLDN8: ' + FLDN8 + ', FLDV8:' + FLDV8 + ', FLDN9: ' + FLDN9 + ', FLDV9:' + FLDV9 + ', FLDN10: ' + FLDN10 + ', FLDV10:' + FLDV10 + ', TXN MADE BY:' + NAME_OF_TXN_PERSON + ', AMOUNT:' + AMT + ', PURPOSE:' + PURPOSE + ', INTIATED BY:' + INITIATED_BY + ', DATE:' + DATE + ', TIME:' + TIME + ', NOTE:' + NOTE + ', ENTRY BY:' + ENTRY_BY);
//val.add('From: ' + FROM + ', To: ' + TO +', Other External Name: ' + OTH_EXT_NAME);
//val=('From: ' + FROM + ', To: ' + TO +'\n');
//}
//else
//
//}
}
} catch (JSONException e) {
e.printStackTrace();
}
return val.toString();
}
@override
protected void onPostExecute(String strFromDoInBg)
{
//TextView tv1 = (TextView) findViewById(R.id.editText2);
//tv1.setText(val.toString());
//ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,val);
//GridView gv=(GridView)findViewById(R.id.gridView1);
//gv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
-----------------------------------------------
And following is my associated xml file for the above java file:- --------------------
<?xml version='1.0' encoding='utf-8'?>
<android.support.constraint.ConstraintLayout xmlns:android='http://schemas.android.com/apk/res/android'
xmlns:app='http://schemas.android.com/apk/res-auto'
xmlns:tools='http://schemas.android.com/tools'
android:layout_width='match_parent'
android:layout_height='match_parent'
android:background='@android:color/holo_blue_light'
tools:context='com.meticuloustech.myaccounts.ViewRecord'>
<TextView
android:id='@+id/lbl_viewRecord'
android:layout_width='129dp'
android:layout_height='48dp'
android:ems='10'
android:inputType='textPersonName'
android:labelFor='@id/lbl_viewRecord'
android:text='View Records'
android:textColor='@android:color/background_dark'
android:textStyle='bold'
app:layout_constraintBottom_toTopOf='parent'
app:layout_constraintLeft_toRightOf='parent'
app:layout_constraintRight_toLeftOf='parent'
app:layout_constraintTop_toBottomOf='parent'
app:layout_constraintVertical_bias='0.03'
app:layout_constraintHorizontal_bias='0.561' />
<GridView
android:id='@+id/gridView1'
android:layout_width='300dp'
android:layout_height='432dp'
android:layout_marginBottom='0dp'
android:layout_marginLeft='0dp'
android:layout_marginRight='0dp'
android:layout_marginTop='0dp'
android:clickable='false'
app:layout_constraintBottom_toTopOf='parent'
app:layout_constraintLeft_toRightOf='parent'
app:layout_constraintRight_toLeftOf='parent'
app:layout_constraintTop_toBottomOf='parent'
app:layout_constraintVertical_bias='0.837' />
</android.support.constraint.ConstraintLayout>
------------------------------
Please let me know where the problem is and oblige me there by.
Thanks.