Sunday, 25 August 2013

sending data as intents to another activity from listview

sending data as intents to another activity from listview

I have a main activity which onclick sends an url to another activity
My JSON URL is http://54.218.73.244:7002/
structure response ::
{
"restaurants": [
{
"restaurantID": 1,
"restaurantNAME": "CopperChimney",
"url": "http://54.218.73.244:7002/CopperChimney"
},
{
"restaurantID": 2,
"restaurantNAME": "Aroy",
"url": "http://54.218.73.244:7002/Aroy"
},
{
"restaurantID": 3,
"restaurantNAME": "MarkBoulevard",
"url": "http://54.218.73.244:7002/MarkBoulevard"
},
{
"restaurantID": 4,
"restaurantNAME": "Indian",
"url": "http://54.218.73.244:7002/Indian"
}
],
"RestaurantTimings": [
{
"_id": 1,
"RestaurantTime": "8pm to 11pm"
},
{
"_id": 2,
"RestaurantTime": "10pm to 12pm"
},
{
"_id": 3,
"RestaurantTime": "11pm to 9pm"
},
{
"_id": 4,
"RestaurantTime": "10pm to 5pm"
}
]
}
How to add condition such that on click of a perticular row i could send a
perticular url parsed from the JSON



MainActivity.java
public class MainActivity extends Activity {
// url to make request
private static String url = "http://54.218.73.244:7002/";
private HashMap<Integer, String> TimeMap = new HashMap<Integer,
String>();
List<Item> yourData = new ArrayList<Item>();
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiating ProgressDialog with onCreate method
progressDialog=new ProgressDialog(MainActivity.this);
new ParsingAsync().execute();
}
private class ParsingAsync extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=ProgressDialog.show(MainActivity.this, "",
"Please Wait", true, false);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
String _response = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet(url);
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
_response =EntityUtils.toString(resEntity);
JSONObject jsonObject = new JSONObject(_response);
JSONArray first_array = jsonObject.getJSONArray("restaurants");
JSONArray second_array =
jsonObject.getJSONArray("RestaurantTimings");
for(int i=0;i<first_array.length();i++)
{
JSONObject c = second_array.getJSONObject(i);
Item item = new Item();
// Storing each json item in variable
int id = c.getInt("_id");
String TIME = c.getString("RestaurantTime");
item.setTime(TIME);
c = first_array.getJSONObject(i);
String NAME=c.getString("restaurantNAME");
item.setName(NAME);
String URL=c.getString("url");
item.setUrl(URL);
yourData.add(item);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
//TextView timedisplay=(TextView)
findViewById(R.id.RestaurantTimeID);
ListView yourListView = (ListView) findViewById(R.id.listViewID);
ListAdapter customAdapter = new ListAdapter(MainActivity.this,
R.layout.itemlistrow, yourData);
yourListView.setAdapter(customAdapter);
yourListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(MainActivity.this,
RestaurantDesc.class);
i.putExtra("key",
"http://54.218.73.244:7002/CopperChimney");
startActivity(i);
}
});
}
}
}
Item.java
public class Item{
private String Name;
private String Time;
private String Url;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getTime() {
return Time;
}
public void setTime(String time) {
Time = time;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
}
[EDIT]
I tried using
i.putExtra("key", yourData.get(position).getUrl());
i can get the successfull data onfirst onclick of listview but on second ,
third ,..... i get below error
error
08-25 19:14:57.133: D/dalvikvm(332): GC_EXTERNAL_ALLOC freed 165K, 50%
free 2896K/5703K, external 2111K/2137K, paused 107ms
08-25 19:21:47.553: W/dalvikvm(370): threadid=6: spin on suspend #1
threadid=9 (pcf=0)
08-25 19:21:47.553: D/dalvikvm(370): Temporarily moving tid 378 to fg (was 0)
08-25 19:21:47.553: D/dalvikvm(370): Temporarily raised priority on tid
378 (10 -> 0)
08-25 19:21:47.633: W/dalvikvm(370): threadid=6: spin on suspend resolved
in 1078 msec
08-25 19:21:47.633: D/dalvikvm(370): Restored policy of 378 to 0
08-25 19:21:47.633: D/dalvikvm(370): Restored priority on 378 to 10
08-25 20:10:30.833: W/dalvikvm(407): threadid=6: spin on suspend #1
threadid=9 (pcf=0)
08-25 20:10:30.833: D/dalvikvm(407): Temporarily moving tid 415 to fg (was 0)
08-25 20:10:30.833: D/dalvikvm(407): Temporarily raised priority on tid
415 (10 -> 0)
08-25 20:10:30.913: W/dalvikvm(407): threadid=6: spin on suspend resolved
in 1079 msec
08-25 20:10:30.913: D/dalvikvm(407): Restored policy of 415 to 0
08-25 20:10:30.913: D/dalvikvm(407): Restored priority on 415 to 10
08-25 20:10:35.985: D/dalvikvm(407): GC_EXTERNAL_ALLOC freed 166K, 50%
free 2896K/5703K, external 2111K/2137K, paused 85ms
08-25 20:10:46.523: W/System.err(407): org.json.JSONException: No value
for CopperChimney
08-25 20:10:46.773: W/System.err(407): at
org.json.JSONObject.get(JSONObject.java:354)
08-25 20:10:46.773: W/System.err(407): at
org.json.JSONObject.getJSONArray(JSONObject.java:544)
08-25 20:10:46.773: W/System.err(407): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:115)
08-25 20:10:46.783: W/System.err(407): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-25 20:10:46.783: W/System.err(407): at
android.os.AsyncTask$2.call(AsyncTask.java:185)
08-25 20:10:46.783: W/System.err(407): at
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-25 20:10:46.783: W/System.err(407): at
java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-25 20:10:46.783: W/System.err(407): at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-25 20:10:46.783: W/System.err(407): at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-25 20:10:46.783: W/System.err(407): at
java.lang.Thread.run(Thread.java:1019)
08-25 20:10:46.793: D/AndroidRuntime(407): Shutting down VM
08-25 20:10:46.793: W/dalvikvm(407): threadid=1: thread exiting with
uncaught exception (group=0x40015560)
08-25 20:10:46.813: E/AndroidRuntime(407): FATAL EXCEPTION: main
08-25 20:10:46.813: E/AndroidRuntime(407): java.lang.NullPointerException
08-25 20:10:46.813: E/AndroidRuntime(407): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-25 20:10:46.813: E/AndroidRuntime(407): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-25 20:10:46.813: E/AndroidRuntime(407): at
android.os.AsyncTask.finish(AsyncTask.java:417)
08-25 20:10:46.813: E/AndroidRuntime(407): at
android.os.AsyncTask.access$300(AsyncTask.java:127)
08-25 20:10:46.813: E/AndroidRuntime(407): at
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-25 20:10:46.813: E/AndroidRuntime(407): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-25 20:10:46.813: E/AndroidRuntime(407): at
android.os.Looper.loop(Looper.java:123)
08-25 20:10:46.813: E/AndroidRuntime(407): at
android.app.ActivityThread.main(ActivityThread.java:3683)
08-25 20:10:46.813: E/AndroidRuntime(407): at
java.lang.reflect.Method.invokeNative(Native Method)
08-25 20:10:46.813: E/AndroidRuntime(407): at
java.lang.reflect.Method.invoke(Method.java:507)
08-25 20:10:46.813: E/AndroidRuntime(407): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-25 20:10:46.813: E/AndroidRuntime(407): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-25 20:10:46.813: E/AndroidRuntime(407): at
dalvik.system.NativeStart.main(Native Method)
08-25 20:10:52.563: I/Process(407): Sending signal. PID: 407 SIG: 9
08-25 20:10:58.774: W/IInputConnectionWrapper(422): showStatusIcon on
inactive InputConnection
08-25 20:25:43.273: D/dalvikvm(422): GC_EXTERNAL_ALLOC freed 160K, 49%
free 2880K/5639K, external 2082K/2137K, paused 87ms
08-25 20:25:49.403: W/System.err(422): org.json.JSONException: No value
for CopperChimney
08-25 20:25:49.473: W/System.err(422): at
org.json.JSONObject.get(JSONObject.java:354)
08-25 20:25:49.473: W/System.err(422): at
org.json.JSONObject.getJSONArray(JSONObject.java:544)
08-25 20:25:49.483: W/System.err(422): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:115)
08-25 20:25:49.483: W/System.err(422): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-25 20:25:49.483: W/System.err(422): at
android.os.AsyncTask$2.call(AsyncTask.java:185)
08-25 20:25:49.483: W/System.err(422): at
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-25 20:25:49.483: W/System.err(422): at
java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-25 20:25:49.493: W/System.err(422): at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-25 20:25:49.493: W/System.err(422): at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-25 20:25:49.493: W/System.err(422): at
java.lang.Thread.run(Thread.java:1019)
08-25 20:25:49.493: D/AndroidRuntime(422): Shutting down VM
08-25 20:25:49.503: W/dalvikvm(422): threadid=1: thread exiting with
uncaught exception (group=0x40015560)
08-25 20:25:49.513: E/AndroidRuntime(422): FATAL EXCEPTION: main
08-25 20:25:49.513: E/AndroidRuntime(422): java.lang.NullPointerException
08-25 20:25:49.513: E/AndroidRuntime(422): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-25 20:25:49.513: E/AndroidRuntime(422): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-25 20:25:49.513: E/AndroidRuntime(422): at
android.os.AsyncTask.finish(AsyncTask.java:417)
08-25 20:25:49.513: E/AndroidRuntime(422): at
android.os.AsyncTask.access$300(AsyncTask.java:127)
08-25 20:25:49.513: E/AndroidRuntime(422): at
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-25 20:25:49.513: E/AndroidRuntime(422): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-25 20:25:49.513: E/AndroidRuntime(422): at
android.os.Looper.loop(Looper.java:123)
08-25 20:25:49.513: E/AndroidRuntime(422): at
android.app.ActivityThread.main(ActivityThread.java:3683)
08-25 20:25:49.513: E/AndroidRuntime(422): at
java.lang.reflect.Method.invokeNative(Native Method)
08-25 20:25:49.513: E/AndroidRuntime(422): at
java.lang.reflect.Method.invoke(Method.java:507)
08-25 20:25:49.513: E/AndroidRuntime(422): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-25 20:25:49.513: E/AndroidRuntime(422): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-25 20:25:49.513: E/AndroidRuntime(422): at
dalvik.system.NativeStart.main(Native Method)
08-25 20:26:51.563: W/dalvikvm(481): threadid=6: spin on suspend #1
threadid=9 (pcf=0)
08-25 20:26:51.563: D/dalvikvm(481): Temporarily moving tid 489 to fg (was 0)
08-25 20:26:51.563: D/dalvikvm(481): Temporarily raised priority on tid
489 (10 -> 0)
08-25 20:26:51.643: W/dalvikvm(481): threadid=6: spin on suspend resolved
in 1080 msec
08-25 20:26:51.643: D/dalvikvm(481): Restored policy of 489 to 0
08-25 20:26:51.643: D/dalvikvm(481): Restored priority on 489 to 10
08-25 20:27:48.342: D/dalvikvm(481): GC_EXTERNAL_ALLOC freed 158K, 50%
free 2896K/5703K, external 2111K/2137K, paused 134ms
08-25 20:28:07.754: D/dalvikvm(481): GC_FOR_MALLOC freed 353K, 44% free
3770K/6727K, external 2458K/3069K, paused 752ms
08-25 20:28:08.203: D/dalvikvm(481): GC_CONCURRENT freed 1K, 44% free
3770K/6727K, external 2458K/3069K, paused 7ms+16ms
08-25 20:28:08.843: W/System.err(481): org.json.JSONException: No value
for CopperChimney
08-25 20:28:08.843: W/System.err(481): at
org.json.JSONObject.get(JSONObject.java:354)
08-25 20:28:08.853: W/System.err(481): at
org.json.JSONObject.getJSONArray(JSONObject.java:544)
08-25 20:28:08.883: W/System.err(481): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:115)
08-25 20:28:08.883: W/System.err(481): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.doInBackground(RestaurantDesc.java:1)
08-25 20:28:08.913: W/System.err(481): at
android.os.AsyncTask$2.call(AsyncTask.java:185)
08-25 20:28:08.913: W/System.err(481): at
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-25 20:28:08.913: W/System.err(481): at
java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-25 20:28:08.923: W/System.err(481): at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-25 20:28:08.933: W/System.err(481): at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-25 20:28:08.943: W/System.err(481): at
java.lang.Thread.run(Thread.java:1019)
08-25 20:28:08.943: D/AndroidRuntime(481): Shutting down VM
08-25 20:28:08.943: W/dalvikvm(481): threadid=1: thread exiting with
uncaught exception (group=0x40015560)
08-25 20:28:08.963: E/AndroidRuntime(481): FATAL EXCEPTION: main
08-25 20:28:08.963: E/AndroidRuntime(481): java.lang.NullPointerException
08-25 20:28:08.963: E/AndroidRuntime(481): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:138)
08-25 20:28:08.963: E/AndroidRuntime(481): at
com.project.findmybuffet.RestaurantDesc$ParsingAsync.onPostExecute(RestaurantDesc.java:1)
08-25 20:28:08.963: E/AndroidRuntime(481): at
android.os.AsyncTask.finish(AsyncTask.java:417)
08-25 20:28:08.963: E/AndroidRuntime(481): at
android.os.AsyncTask.access$300(AsyncTask.java:127)
08-25 20:28:08.963: E/AndroidRuntime(481): at
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-25 20:28:08.963: E/AndroidRuntime(481): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-25 20:28:08.963: E/AndroidRuntime(481): at
android.os.Looper.loop(Looper.java:123)
08-25 20:28:08.963: E/AndroidRuntime(481): at
android.app.ActivityThread.main(ActivityThread.java:3683)
08-25 20:28:08.963: E/AndroidRuntime(481): at
java.lang.reflect.Method.invokeNative(Native Method)
08-25 20:28:08.963: E/AndroidRuntime(481): at
java.lang.reflect.Method.invoke(Method.java:507)
08-25 20:28:08.963: E/AndroidRuntime(481): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-25 20:28:08.963: E/AndroidRuntime(481): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-25 20:28:08.963: E/AndroidRuntime(481): at
dalvik.system.NativeStart.main(Native Method)
RestaurantDesc.java
public class RestaurantDesc extends Activity{
// url to make request
String url ;
ProgressDialog progressDialog;
JSONObject jsonObject;
JSONArray first_array ;
JSONArray second_array;
TextView textView;
TextView text;
private SparseArray<String> startarsMap = new SparseArray<String>();
private SparseArray<String> saladsMap = new SparseArray<String>();
private SparseArray<String> maincourseMap = new SparseArray<String>();
private SparseArray<String> desertMap = new SparseArray<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.restaurant_desc);
url = getIntent().getStringExtra("key");
progressDialog=new ProgressDialog(RestaurantDesc.this);
new ParsingAsync().execute();
}
private class ParsingAsync extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=ProgressDialog.show(RestaurantDesc.this, "",
"Please Wait", true, false);
Button BACKBUTTON=(Button)
findViewById(R.id.TopNavigationBarRestaurantDescActivityBackButton);
BACKBUTTON.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent emp1=new
Intent(RestaurantDesc.this,MainActivity.class);
startActivity(emp1);
}
});
Button PHOTOBUTTON=(Button)
findViewById(R.id.RestaurantPhotosButton);
PHOTOBUTTON.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent pht=new
Intent(RestaurantDesc.this,RestaurantPhotos.class);
pht.putExtra("key", getIntent().getStringExtra("key"));
startActivity(pht);
}
});
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
String _response = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet(url);
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
_response =EntityUtils.toString(resEntity);
jsonObject = new JSONObject(_response);
first_array = jsonObject.getJSONArray("CopperChimney");
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
//TextView timedisplay=(TextView)
findViewById(R.id.RestaurantTimeID);
for(int i=0; i < first_array.length(); i++)
{
try {
JSONObject detail_obj = first_array.getJSONObject(i);
int id = detail_obj.getInt("_id");
String STARTERS = detail_obj.getString("Starters");
String SALADS = detail_obj.getString("Salads");
String MAINCOURSE = detail_obj.getString("MainCourse");
String DESERT = detail_obj.getString("Desert");
startarsMap.put(id, STARTERS);
saladsMap.put(id, SALADS);
maincourseMap.put(id, MAINCOURSE);
desertMap.put(id, DESERT);
//FIRST - - - - -- - - - ROW
TextView textView=(TextView)
findViewById(R.id.textView1);
textView.setText(startarsMap.get(1));
textView=(TextView) findViewById(R.id.textView2);
textView.setText(startarsMap.get(2));
textView=(TextView) findViewById(R.id.textView3);
textView.setText(startarsMap.get(3));
//SECOND - - - - -- - - - ROW
textView=(TextView) findViewById(R.id.textView4);
textView.setText(saladsMap.get(1));
textView=(TextView) findViewById(R.id.textView5);
textView.setText(saladsMap.get(2));
textView=(TextView) findViewById(R.id.textView6);
textView.setText(saladsMap.get(3));
//THIRD - - - - -- - - - ROW
textView=(TextView) findViewById(R.id.textView7);
textView.setText(maincourseMap.get(1));
textView=(TextView) findViewById(R.id.textView8);
textView.setText(maincourseMap.get(2));
textView=(TextView) findViewById(R.id.textView9);
textView.setText(maincourseMap.get(3));
//FOURTH - - - - -- - - - ROW
textView=(TextView) findViewById(R.id.textView10);
textView.setText(desertMap.get(1));
textView=(TextView) findViewById(R.id.textView11);
textView.setText(desertMap.get(2));
textView=(TextView) findViewById(R.id.textView12);
textView.setText(desertMap.get(3));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
I hope i am clear with my explanation. Any ideas? Thanks.

No comments:

Post a Comment