remove row from another activity - android

I have a listview which is inflated by custom array adapter and onclick takes it to another activity with the data related to that row .After clicking on delete its supposed to delete the item from the list and get back to the list.
I am using the code below for this :
int deleteposition=CustomizedListView.deleteposition;
CustomizedListView.list.removeViewAt(deleteposition);
CustomizedListView.adapter.notifyDataSetChanged();
finish();
But there is an error at this line :
CustomizedListView.list.removeViewAt(deleteposition);
Please Tell me how to fix it ?
Logcat Details:
java.lang.UnsupportedOperationException: removeViewAt(int) is not supported in AdapterView
at android.widget.AdapterView.removeViewAt(AdapterView.java:511)
at com.example.androidhive.openedmsg$1.onClick(openedmsg.java:35)
at android.view.View.performClick(View.java:3549)
at android.view.View$PerformClick.run(View.java:14393)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
The entire Code :
CustomizedListView.class
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "https://itunes.apple.com/us/rss/topalbums/limit=20/json";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static ListView list;
static LazyAdapter adapter;
HashMap<String, String> map;
public static String newactivityno;
public static int deleteposition;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL(URL);
try {
JSONObject arr2 = json.getJSONObject("feed");
JSONArray arr = arr2.getJSONArray("entry");
for (int i = 0; i < arr.length(); i++) {
JSONObject e1 = arr.getJSONObject(i);
JSONArray arr3 = e1.getJSONArray("im:image");
JSONObject arr8 = e1.getJSONObject("im:name");
JSONObject arr10 = e1.getJSONObject("im:artist");
JSONObject e12 = arr3.getJSONObject(0);
// creating new HashMap
map = new HashMap<String, String>();
map.put(KEY_THUMB_URL, e12.getString("label"));
map.put(KEY_ARTIST, arr8.getString("label"));
map.put(KEY_TITLE, arr10.getString("label"));
// adding HashList to ArrayList
songsList.add(map);
}
} catch (JSONException e) {
// Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getBaseContext(),
"Network communication error!", 5).show();
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o= (HashMap<String, String>) list.getItemAtPosition(position);
// Toast.makeText(CustomizedListView.this, "ID '" + o.get("title") + "' was clicked.", Toast.LENGTH_SHORT).show();
deleteposition=position;
newactivityno= o.get("title");
Intent ii= new Intent(getBaseContext(),newactivity.class);
startActivity(ii);
}
});
}
}
newactivity.class
public class newactivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newlayout);
TextView tv1=(TextView)findViewById(R.id.textView11);
TextView tv2=(TextView)findViewById(R.id.textView12);
Button bn=(Button)findViewById(R.id.button1);
tv2.setText(CustomizedListView.newactivityno);
bn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int deleteposition=CustomizedListView.deleteposition;
CustomizedListView.list.removeViewAt(deleteposition);
CustomizedListView.adapter.notifyDataSetChanged();
finish();
}
});
}
}

You are approaching this problem in the wrong manner. Rather than remove the View from the AdapterView, simply remove the data from the data set and call notifyDataSetChanged(). (This will remove the unwanted View automatically.)

Related

How do I refresh the data in the ListView?(json&http)

First of all , there is a listview in the activity named "user" . In the activity class, I have a thread that runs every 10 seconds. The thread calls a function which executes an AsyncTask . The AsyncTask does an http request and the response is parsed by a class named JSONparse . The data is then used in a listview inside a for loopwith a simple adapter.
The actual problem here is that the older data stay in the listview , and the same elements are added every 10 seconds.
The onPostExecute is inside the JSONparse class:
private static final String TAG_OS = "istekler";
private static final String TAG_VER = "isim";
private static final String TAG_NAME = "istek";
private static final String TAG_API = "mesaj";
protected void onPostExecute(JSONObject json) {
try {
// Getting JSON Array from URL
android = json.getJSONArray(TAG_OS);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String isim = c.getString(TAG_VER);
String mesaj = c.getString(TAG_NAME);
String istek = c.getString(TAG_API);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_VER, isim);
map.put(TAG_NAME, mesaj);
map.put(TAG_API, istek);
oslist.add(map);
list=(ListView)findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(user.this, oslist,
R.layout.list_v,
new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] {
R.id.isim,R.id.mesaj, R.id.istek});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(user.this, "Parça: "+oslist.get(+position).get("istek"), Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
on your code you just add new elements to your list. You should call oslist.clear() to remove the old data.

How to update listview after changing data in JSON?

Hello everyone and happy new year
I have JSON class where I retrieve some data that are retrieve from a database. The format of this JSON file is
{"people":[{"id":"15","first_name":"Theo","last_name":"Tziomakas","bio":"Hello from
Theo!!!","created":"2015-01-11 21:48:51"},
{"id":"16","first_name":"Jim","last_name":"Chytas","bio":"Hello from Chytas","created":"2015-01-11
21:53:42"}]}.
The idea is to retrieve the "first_name" and "second_name" in a listview. The "bio" should appear in another activity,but I don't know how to do that:(.
Here is my code.
public class MainActivity extends ActionBarActivity {
ListView list;
TextView fname;
TextView lname;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private static String url = "http://xxxxxxx/tutorials/index.php";
//JSON Node Names
private static final String TAG_OS = "people";
private static final String TAG_FIRST_NAME = "first_name";
private static final String TAG_SECOND_NAME = "last_name";
//private static final String TAG_BIO = "bio";
JSONArray android = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oslist = new ArrayList<HashMap<String, String>>();
new JSONParse().execute();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
fname = (TextView)findViewById(R.id.first_name);
lname = (TextView)findViewById(R.id.last_name);
//abio = (TextView)findViewById(R.id.bio);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
android = json.getJSONArray(TAG_OS);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String first_name = c.getString(TAG_FIRST_NAME);
String last_name = c.getString(TAG_SECOND_NAME);
//String bio = c.getString(TAG_BIO);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_FIRST_NAME, first_name);
map.put(TAG_SECOND_NAME, last_name);
//map.put(TAG_BIO, bio);
oslist.add(map);
list=(ListView)findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
R.layout.list_v,
new String[] { TAG_FIRST_NAME,TAG_SECOND_NAME }, new int[] {
R.id.first_name,R.id.last_name});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String bio = ((TextView) view.findViewById(R.id.bio))
.getText().toString();
switch (position) {
case 0:
Intent i = new Intent(MainActivity.this, SingleActivity.class);
i.putExtra("bio", bio);
startActivity(i);
break;
}
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
And finally I have the SecondActivity class,but nothing is shown when I click the first row of list.
public class SingleActivity extends ActionBarActivity {
TextView text;
JSONArray android = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single);
text = (TextView)findViewById(R.id.text);
String bio = getIntent().getStringExtra("bio");
try {
JSONObject profileJSON = new JSONObject(bio);
android = profileJSON.getJSONArray(bio);
text.setText(""+android);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Thank you.
EDIT:The problem I had is fixed. Now I want to do something else. Let us suppose that we update the data in an existing row by changing "first_name,"last_name" and finally bio. It is done very easily in phpmyadmin tool. The problem is that the updated row is not shown in the listview. I have to reinstall the app in order to see it. Any ideas in that?
try this ,
replace onPostExecute with this
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
list=(ListView)findViewById(R.id.list);
// Getting JSON Array from URL
android = json.getJSONArray(TAG_OS);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String first_name = c.getString(TAG_FIRST_NAME);
String last_name = c.getString(TAG_SECOND_NAME);
//String bio = c.getString(TAG_BIO);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_FIRST_NAME, first_name);
map.put(TAG_SECOND_NAME, last_name);
map.put(TAG_BIO, bio);
oslist.add(map);
}
ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
R.layout.list_v,
new String[] { TAG_FIRST_NAME,TAG_SECOND_NAME }, new int[] {
R.id.first_name,R.id.last_name});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String bio =oslist.get(position).get(TAG_BIO);
// switch (position) {
// case 0:
Intent i = new Intent(MainActivity.this, SingleActivity.class);
i.putExtra("bio", bio);
startActivity(i);
break;
//}
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
and SingleActivity
text = (TextView)findViewById(R.id.text);
String bio = getIntent().getStringExtra("bio");
text.setText(bio );
Pay attention to what you put in the intent.
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String bio = ((TextView) view.findViewById(R.id.bio))
.getText().toString();
switch (position) {
case 0:
Intent i = new Intent(MainActivity.this, SingleActivity.class);
i.putExtra("bio", bio);
startActivity(i);
break;
}
}
notice that you take the text from a text view and add it to and intent.
When you retreive it use it as if its a json object.
If you want the bio string, you could put that in a variable, and then send it to an intent with just the string.
for example
ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
R.layout.list_v,
new String[] { TAG_FIRST_NAME,TAG_SECOND_NAME }, new int[] {
R.id.first_name,R.id.last_name});
list.setAdapter(adapter);
String bio = c.getString("bio");
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
Intent i = new Intent(MainActivity.this, SingleActivity.class);
i.putExtra("bio", bio);
startActivity(i);
break;
}
}
});
when retrieving it its easier to just do it with
public class SingleActivity extends ActionBarActivity {
TextView text;
JSONArray android = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single);
text = (TextView)findViewById(R.id.text);
String bio = getIntent().getStringExtra("bio");
text.settext(bio)
}
}
now there is prob some bugs in these example, since i have not java or android sdk installed on my computer

Fragment list data not passing correctly to activity

When I am clicking on my list items only the 1st item's id is going to the next activity. As I am getting patient name and id in this list fragment and passing the patient id to another activity, only the 1st list item's id is passing through even if I tap on another item... Hope you got my point...
Here is my ListFragment,
public class AllPatient extends ListFragment {
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String,String>>();
// url to get all products list
private static String url_all_patients = "http://192.168.44.208/get_all_patients.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PATIENT_ID = "patient_id";
private static final String TAG_PATIENT_NAME = "patient_name";
JSONArray products = null;
Context ctx;
String pid;
EditText inputSearch = null;
ListAdapter adapter;
ListView lv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.patient_list, container, false);
//ListView lv;
lv = (ListView)view.findViewById(android.R.id.list);
//ListView v = getListView();
new LoadAllPatients().execute();
return view;
}
#Override
public void onListItemClick(ListView lv, View v, int position, long id) {
// TODO Auto-generated method stub
onAttach(getActivity());
//lv.setAdapter(adapter);
String id1 = ((TextView) getView().findViewById(R.id.pid)).getText().toString();
System.out.println("all patient"+id1);
Bundle bundle = new Bundle();
bundle.putString("TAG_PATIENT_ID",id1 );
System.out.println("bundle"+id1);
Intent i = new Intent(getActivity(),DocPresc.class);
i.putExtras(bundle);
startActivity(i);
//passData(date);
Toast.makeText(
getActivity(),
getListView().getItemAtPosition(position).toString(),
Toast.LENGTH_LONG).show();
}
class LoadAllPatients extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_patients, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Patients: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PATIENT_ID).toUpperCase();
String name = c.getString(TAG_PATIENT_NAME).toUpperCase();
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PATIENT_ID, id);
map.put(TAG_PATIENT_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
// pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
getActivity(), productsList,
R.layout.list_item1, new String[] { TAG_PATIENT_ID,
TAG_PATIENT_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
//setListAdapter(adapter);
setListAdapter(adapter);
}
});
}
}
}
Try this.
Change this line:
String id1 = ((TextView) getView().findViewById(R.id.pid)).getText().toString();
to
String id1 = ((TextView) v.findViewById(R.id.pid)).getText().toString();

Android compilation errors e.g. "adapter cannot be resolved to a type"

It is my mainactivity code. Following is my main activity code and it shows four errors:
adapter cannot be resolved to a type
Syntax error on token ")", { expected after this token
Syntax error on token "adapter", VariableDeclaratorId expected after this token.
Source code:
public class HospitalParseActivity extends ListActivity {
//url where request is made
private static String url="url";
//JSON node names
private static final String TAG_NETFOX="transfer";
private static final String TAG_DATE="date";
private static final String TAG_CWEB="c_web";
private static final String TAG_CBANK="c_bank";
private static final String TAG_CCASH="c_cash";
private static final String TAG_SWEB="s_web";
private static final String TAG_SBANK="s_bank";
private static final String TAG_SCASH="s_cash";
//creation of JSONArray
JSONArray netfoxlimited=null;
private List<? extends Map<String, ?>> contactList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
netfoxlimited = json.getJSONArray(TAG_NETFOX);
// looping through All Contacts
for(int i = 0; i < netfoxlimited.length(); i++){
JSONObject c = netfoxlimited.getJSONObject(i);
// Storing each json item in variable
String date = c.getString(TAG_DATE);
String c_web = c.getString(TAG_CWEB);
String c_bank = c.getString(TAG_CBANK);
String c_cash = c.getString(TAG_CCASH);
String s_web = c.getString(TAG_SWEB);
String s_bank = c.getString(TAG_SBANK);
String s_cash = c.getString(TAG_SCASH);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_DATE, date);
map.put(TAG_CWEB, c_web);
map.put(TAG_CBANK, c_bank);
map.put(TAG_CCASH, c_cash);
map.put(TAG_SWEB, s_web);
map.put(TAG_SBANK, s_bank);
map.put(TAG_SCASH, s_cash);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_DATE, TAG_CWEB, TAG_CBANK,TAG_CCASH, TAG_CWEB,TAG_CBANK, TAG_CCASH }, new int[] {
R.id.date, R.id.cweb, R.id.cbank,R.id.sweb,R.id.sbank,R.id.scash });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String dt = ((TextView) view.findViewById(R.id.date)).getText().toString();
String web = ((TextView) view.findViewById(R.id.cweb)).getText().toString();
String bank = ((TextView) view.findViewById(R.id.cbank)).getText().toString();
String cash = ((TextView) view.findViewById(R.id.ccash)).getText().toString();
String web1 = ((TextView) view.findViewById(R.id.sweb)).getText().toString();
String bank1 = ((TextView) view.findViewById(R.id.sbank)).getText().toString();
String cash1 = ((TextView) view.findViewById(R.id.scash)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_DATE, dt);
in.putExtra(TAG_CWEB, web);
in.putExtra(TAG_CBANK, bank);
in.putExtra(TAG_CCASH, cash);
in.putExtra(TAG_SWEB, web1);
in.putExtra(TAG_SBANK, bank1);
in.putExtra(TAG_SCASH, cash1);
startActivity(in);
}
});
}
}
Are you writing code outside the onCreate function? Seems like onCreate function ended at the '}' after the catch block. You may just want to remove that

Android SimpleAdaptor NPE

I'm having issues inflating JSON object data into my ListView fragment.
The data is getting pulled from a database and is converted into a JSON array via PHP then to JSON Objects. Everything is working to that point (see my inline Log.i()'s within my code).
I am getting a NPE as follows:
threadid=1: thread exiting with uncaught exception (group=0x40a781f8)
FATAL EXCEPTION: main java.lang.NullPointerException at
android.widget.SimpleAdapter.(SimpleAdapter.java:85) at
com.andaero.app.NavigationListFragment$1.callback(NavigationListFragment.java:97)
at
com.andaero.app.NavigationListFragment$1.callback(NavigationListFragment.java:1)
at
com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:440)
at
com.androidquery.callback.AbstractAjaxCallback.afterWork(AbstractAjaxCallback.java:1010)
at
com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:804)
at android.os.Handler.handleCallback(Handler.java:605) at
android.os.Handler.dispatchMessage(Handler.java:92) at
android.os.Looper.loop(Looper.java:137) at
android.app.ActivityThread.main(ActivityThread.java:4424) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:511) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) at
dalvik.system.NativeStart.main(Native Method)
I'm not sure what I have wrong. Please show corrected methods in your answer so I can better understand and learn from this. Thanxs
public class NavigationListFragment extends ListFragment {
Context context;
private Activity c;
final AQuery aq = new AQuery(c);
private static String url = "http://192.168.1.17/Andaero/php/regulatory_list.php";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.listview, container, false);
Log.i("NavigationListFragment", "ListView Inflated!!");
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// AsyncTasks.getJSONArrays(context);// asynchronous task for getting
// JSONarray
aq.ajax(url, JSONArray.class, new AjaxCallback<JSONArray>() {
// JSON Node names
private static final String TAG_ID = "_id";
private static final String TAG_LABEL = "label";
private static final String TAG_TITLE = "title";
private static final String TAG_DISCR = "description";
private static final String TAG_GO2URL = "gotoURL";
public void callback(String url, JSONArray json, AjaxStatus status) {
if (json != null) {
// NavigationListFragment.jsonListCallback(json);
Log.i("NavigationListFragment", "Caught JSON: " + json.toString());
// Hashmap for ListView
List<HashMap<String, String>> mList = new ArrayList<HashMap<String, String>>();
try {
// Parse the string to a JSON object
for (int i = 0; i < json.length(); i++) {
JSONObject json_data = json.getJSONObject(i);
// Storing each json item in variable
String id = json_data.getString(TAG_ID);
String label = json_data.getString(TAG_LABEL);
String title = json_data.getString(TAG_TITLE);
String description = json_data.getString(TAG_DISCR);
String gotoURL = json_data.getString(TAG_GO2URL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_LABEL, label);
map.put(TAG_TITLE, title);
map.put(TAG_DISCR, description);
map.put(TAG_GO2URL, gotoURL);
// adding HashList to ArrayList
mList.add(map);
Log.i("NavigationListFragment", "Hash: " + map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
// create the grid item mapping
String[] from = new String[] {TAG_LABEL, TAG_TITLE, TAG_DISCR, TAG_GO2URL};
int[] to = new int[] { R.id.listLabel, R.id.listTitle, R.id.listDiscription, R.id.dummy };
// Updating parsed JSON data into ListView
SimpleAdapter adapter = new SimpleAdapter(c, mList, R.layout.list_item, from, to);??<--THIS IS LINE 97
getListView().setAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//TODO
}
});
}
// Log any network/JSON Errors
switch (status.getCode()) {
case AjaxStatus.TRANSFORM_ERROR :
Log.i("GetJSONArray", "TRANSFORM_ERROR");
break;
case AjaxStatus.NETWORK_ERROR :
Log.i("GetJSONArray", "NETWORK_ERROR");
// TODO Create Alert Dialog
case AjaxStatus.AUTH_ERROR :
Log.i("GetJSONArray", "AUTH_ERROR");
break;
}
}
});
}
#Override
public void onSaveInstanceState(Bundle outState) {
// TODO
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// TODO
}
}
IFound the answer here
SimpleAdapter adapter = new SimpleAdapter(c, mList, R.layout.list_item, from, to);
Needed to be:
SimpleAdapter adapter = new SimpleAdapter(getActivity(), mList, R.layout.list_item, from, to);
The answer to your question might be here -> https://stackoverflow.com/a/7493347/1149631
As it seems to be an issue with your implementation of your SimpleAdapter.

Categories

Resources