Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, December 25, 2015

Passing data between activities from listview to another activity

Passing data between activities from listview to another activity


I am trying to pass the data from this main activity to another activity

I am successful in sending data between activities .... Like the data from Edit-text to next activity through putExtra and GetExtra methods and passing as intents

  • But i am facing challenge in this particular task where it involves sending data from listview to an ordinary activity
  • data is populated in the list view from JSON so when on click of a row how can i send the data from that row to a new activity

Any Ideas,


ativity_main.xml

                    

MainActivity.java

public class MainActivity extends Activity {        // url to make request      private static String url = "http://54.218.73.244:7002/";      List yourData = new ArrayList();        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      {            @Override          protected void onPreExecute() {              // TODO Auto-generated method stub              super.onPreExecute();              progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);              }            @Override          protected Void doInBackground(Void... params) {              // TODO Auto-generated method stub                // Creating JSON Parser instance              JSONObjParser jParser = new JSONObjParser();                // getting JSON string from URL              JSONArray json = jParser.getJSONFromUrl(url);                try {                  for (int i = 0; i < json.length(); i++) {                      JSONObject c = json.getJSONObject(i);                        // Storing each json item in variable                      String NAME=c.getString("restaurantNAME");                        yourData.add(new Item(NAME));                  }              } catch (JSONException 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();              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) {                      // When clicked, show a toast with the TextView text                      if(position == 0)                      {                          //code specific to first list item                              Intent myIntent = new Intent(MainActivity.this,CopperChimneyDesc.class);                          startActivity(myIntent);                      }else if(position == 1)                      {                          //Intent myIntent = new Intent(MainActivity.this,AroyDesc.class);                          //startActivity(myIntent);                                        }                    }              });          }        }    }  

item.java

public class Item{      private String Name;        public Item(String name){          this.Name = name;      }      public String getName(){          return Name;      }  }  

Thanks,

Answer by Sushil for Passing data between activities from listview to another activity


You can save the data in sharedPreferences and can access it from other activity. Just an idea :)

Answer by user1525382 for Passing data between activities from listview to another activity


you can parcle data and send it to other activity with putxtera method.please see How to store values in onSaveInstanceState() and retrive?

Answer by Cody Caughlan for Passing data between activities from listview to another activity


Since you have the index of the item in the listview the user has clicked you can get the item value from the adapter:

String value = (String)customAdapter.getItem(position);  Intent myIntent = new Intent(MainActivity.this,CopperChimneyDesc.class);  intent.putExtra("restaurant_name", value);  startActivity(myIntent);  

Answer by Peshal for Passing data between activities from listview to another activity


Ok so you would do

String item = yourData.get(position).getName();   

Then you can add the string to intent using:

intent.putExtra("restaurent", item);  

On the other end if its textview you would do

textView.setText(getIntent().getExtras().getString("restaurent"));   

Answer by Nik Patel for Passing data between activities from listview to another activity


intent.putExtra("City Name", String.valueOf(lvCity.getSelectedItem()));  

try this one to cast.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.