i develop an android app which contain a listview initialized by loading item from my server.
the data in my server contain three columns id and content and information , i load the content in the listview.
i need when i click item in the list view to load the item information in new activity.
the problem here is in new activity when i try to load the item information i need the item id .
so how i can do this ?
This is the code for initialized the listview
JSONArray jarray = new JSONArray(qresult);
for ( int i =0; i<jarray.length();i++)
{
JSONObject json = jarray.getJSONObject(i);
s = json.getString("Qcontent");
q=json.getString("Qid");// how to send this id to the next activity
listitems.add(s);
adapter.notifyDataSetChanged();
}
Add it to your intent's extras.
intent.putExtra("id", q);
Then start the activity.
In the second activity's onCreate(), add
Bundle extras = getIntent().getExtras();
String q = extras.getString("id"); //this is your id
Also, you may consider moving notifyDataSetChanged() to outside the for loop.
Related
I am fetching the JSON data via URL and displaying it in text view inside the horizontal scroll view.
JSONArray ja = response.getJSONArray("results");
ArrayList<Details> myModelList = new ArrayList<Details>();
for (int i = 0; i < ja.length(); i++) {
JSONObject jsonObject = ja.getJSONObject(i);
mymodel = new Details();
mymodel.id = Integer.parseInt(jsonObject.optString("id").toString());
mymodel.url = jsonObject.getString("resLink");
mymodel.resType = jsonObject.getString("resType");
mymodel.name = jsonObject.getString("resName");
myModelList.add(mymodel);
setData(myModelList);
I am showing my data in text view but it shows the first data only
private void setData(List<Details> mList) {
for (int i =0; i <=mymodel.getResType().length();i++)
{
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(mymodel.getResType());
}
You are initializing the mymodel in the for loop again and again so I think it contains only one value that too the last one(not the first one).
If that is the problem, try declaring the following outside the for loop.
mymodel = new Details();
So there's quite a few problems in your code:
1) You are always replacing the text of the same TextView. Normally with scrolling views you back them up with an adapter, place your data into the list that is then added to the adapter, then rely on the scrolling list to dynamically create the TextView which you can populate. If you have a known number of views in your scroll view and you want to avoid adapters, make sure they all have unique IDs so that you could find them.
2) Second problem is you're calling your setData inside the loop. Finish the loop first, then call you method (although you won't really have to do this if you do the adapter thing)
3) Your setData method is all sorts of wrong. If you want to pass a list to a method then iterate through it, you do it like this:
for (Details d : list) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(d.getResType());
}
But like I said, that probably won't help you on its own. Look into documentation for the scroll lists or recycler view and adapters. There are many examples available around.
I am trying to send data of a List Array to an Array Adapter in another activity. This is based arround a smiple counter app that when i open the second activity it shows which buttons have been pressed and how many times.
So far i have created my array list the first activity
String player1data = lifepointsP1.getText().toString();
ArrayList<String> myList1 = new ArrayList<String>();
myList1.add(player1data);
and this updates everytime the lifepointsP1 text is changed. Then on the button pushed to open the new activity i do this:
Intent mi = new Intent(v.getContext(), MatchHistory.class);
mi.putExtra("p1L", myList1);
startActivity(mi);
which sends the data across to the second activity and i retrieve it like this:
String p1List = (getIntent().getStringExtra("p1L"));
i then create my array adapter and insert that string as the value:
lvP1 = (ListView) findViewById(R.id.lvP1R);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MatchHistory.this, android.R.layout.simple_list_item_1, p1List);
lvP1.setAdapter(arrayAdapter);
although, this then gives me an error and tells me to take out the 'p1List' string out of the arrayadapter parameters. i dont know why it is doing this.
As other mentioned you are using wrong data type to get the extra from intent.
Replace this line:
String p1List = (getIntent().getStringExtra("p1L"));
With:
ArrayList<String> p1List = getIntent().getStringArrayListExtra("p1L");
basically my problem is this. I am trying to display a list view within a fragment set within the xml file, this list view gets its data from an ArrayList that gets its data from a form I created in another activity using Edit Texts which are put into an ArrayList and passed as a Parcelable Array List to the fragment class.
At the start of the fragment activity there of course are no values stored so I bring up the form activity for data input upon checking that there is no intent stored with the id I specify the passed Parcelable ArrayList as. The problem I have is that whenever I try populate the list view with more than one item (having the first item showing up, returning to the form to add another item in), it seems to only display the one item I add each time and not the items I added previously.
public class MasterFragment extends ListFragment {
private DetailFragment ingredDetails;
private ArrayList<Ingrediants> ingredList = new ArrayList<Ingrediants>();
private ArrayList<String> test2 = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getActivity().getIntent();
Bundle extras = intent.getExtras();
if (extras != null) { //If extras are present
boolean data = extras.getBoolean("ingrediant", true);
if (data) {//Data present from form, retrieve from ArrayList<Ingrediants> and populate the ArrayList<String>
ingredList = getActivity().getIntent().getParcelableArrayListExtra("ingrediant");
test2.add(ingredList.get(i).ingrediantName);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, test2));
}//END OF IF DATA
}//END OF IF EXTRAS
else {//No data found, bring up the FormActivity
Intent intent3 = new Intent(getActivity(), FormActivity.class);
startActivity(intent3);
}//END OF ELSE
}//END OF onCreate()
I think it has something to do with how I am creating the list view and populating it with the ArrayList with the data got from the Parcelable ArrayList intent as it could be recreating the list view each time and only displaying the data supplied to it which in this case will be one item at a time. I've been stuck on this for a while now and was wondering if anyone has any ideas ? Thanks much.
The ArrayList you use (test2) to keep the data does not persist. You can either save the ingredients in sqlite db and call all items onCreate in your fragment, or move the test2 to the main activity and populate items before passing it with Intent bundle.
I am making an android app in which i have an activity X that displays a list and a button. Activity X calls a listview to display that list. Each list item has a number(textview) and a checkbox. I used a setonclicklistener on the checkbox, so whenever the checkbox is checked i am storing the number associated with it in a string. Now i want that whenever i click the button the msg activity should start and the numbers to be sent are the ones that are checked.
I am using the following code to start the msg activity in my X activity.
Intent msgIntent = new Intent(Intent.ACTION_VIEW, Uri
.fromParts("sms", msgnumbers, null));
msgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(msgIntent);
Now "msgsnumbers" variable is present in my listview. How do I pass it to this activity X??
I found the same question here but with no appropriate solution.
-Thanks in advance
Intent in = new Intent(Quote.this, Purchase Ysn.class);
in.putExtra("price", salesprc);
public static String price = "price";
if (getIntent().getExtras().containsKey(price)) {
purces_nbcpy = getIntent().getExtras().getDouble(price);
}
onItemClickListener for ListView has a param position that tells you what position has been clicked.
so if you are using an ArrayList (for eg) to provide values for listItems in adapter you can use this inside onItemClickListener
MyBeanObject object=arraList.get(position);
//use getters of object to retrieve values and pass it as intent
//where arrayList may be your list of objects MyBeanObject
I am writing an Android application with a main page being a list of categories, and then beside each category there is an edit button.
When edit button is clicked, an entry form with the "category" properties filled up is supposed to be shown.
How to pass the category ID or name to the new created form so that it could load from database the properties of selected category
I have no issue with getting the selected category name, by using the code below:
public String getSelectedItem(View view) {
LinearLayout vwParentRow = (LinearLayout) view.getParent();
TextView child = (TextView) vwParentRow.getChildAt(0);
return (String) child.getText();
}
//edit button clicked
public void editCategoryHandler(View view) {
Intent i = new Intent(this, EntryCategory.class);
startActivity(i);
}
I am sure you have just passed String array or ArrayList to display inside that Category List, instead you should prepare an ArrayList of Category objects, and then whenever user clicked a particular item from a category list, get that object at that position after implementing OnItemClickListener, and then pass this received object to the desired activity.
In short, get object for a clicked item and then pass this object.
To pass the value to a new activity, you have to use putExtra() and getExtra().
Please refer the following page
How to use putExtra() and getExtra() for string data