ArrayAdapter wont load string from another activity - android

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");

Related

How can I transfer TextView to ListView

I have two activities. The first activity has one TextView, and the second activity has one ListView. I couldn't transfer between the two via an intent. I must transfer data momentarily. Because I have a timer in the first activity, when the timer goes to 0, then the second activity will start. I must show results in second activity ListView.
You need an arraylist for this. First declare an arraylist in your first activity.
ArrayList<String> clickeddata=new ArrayList<String>();
On button click add the text from textview to arraylist.
clickeddata.add(your_textview.getText().toString());
And when counter is 0 send the arraylist to the second activity.
Intent intent = new Intent(Your_First_Activity.this, Your_second_Activity.class);
intent.putStringArrayListExtra("sentdata", clickeddata);
startActivity(intent);
On second Activity receive it by following code
Intent i = getIntent();
ArrayList<String> mylist=new ArrayList<String>();
mylist = i.getStringArrayListExtra("sentdata");
Now you can create listview by simple adapter like.
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
your_list_view.setAdapter(adapter);
You can do it by Intent
Intent intent=new Intent(Context,SecondActivity);
intent.putExtra("key",textView.getText().toString());
startActivity(intent);
In Second Activity
String data=getIntent().getStringExtra("key");
and use it.

How to send these datas made through dynamic view to another activity (ListView probably)?

So, I have done exactly what's on this video: http://android-er.blogspot.com.br/2013/05/add-and-remove-view-dynamically.html
Now, i'd like to get these names I added and send to another activity when I click the "Next" button (it's not on the video, btw). These names should be displayed like in a list view.
To exemplify it better: think of it as a Bowling software. The add/remove view on the video would be the add/remove players from the game. Once you add everyone you want to, you would click the "Next" button and then it would start another activity with the names you have just added and their scores, etc. I think I could use SQLite to store those names, but since it lasts for one game only, I thought using putExtra or whatever, would be the best way. But I have never tried this before, so I don't know how to send this data to a ListView.
Create an arraylist in your first activity and add your data to it
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("data");
arrayList.add("anotherdata");
Send data by Intent putExtra
Intent intent = new Intent(this,SecondActivity.class);
intent.putStringArrayListExtra("arrayList", (ArrayList<String>) arrayList);
startActivity(intent);
In your secondActivity get the data like
ArrayList<String> mylist=getIntent().getExtras().getStringArrayList("arrayList");
Set your arraylist to your listview like following using adapter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mylist);
listview.setAdapter(arrayAdapter);
You can pass data to a new Activity by using a Bundle. It works like this:
First create a new Bundle and put the data you want to send into that...
Bundle bundle = new Bundle();
bundle.putString("name1", "John");
bundle.putString("name2", "Larry");
bundle.putString("name3", "Cindy");
Then create a new Intent and attach the Bundle to it. Then start your new Activity using the Intent you just created...
Intent intent = new Intent(this, YourSecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);
Then when the new Activity starts up, in the onCreate() method you can extract the data from the Bundle like this...
Bundle bundle = getIntent().getExtras();
String name1 = bundle.getString("name1");
String name2 = bundle.getString("name2");
String name3 = bundle.getString("name3");
Then if you wanted to put those names in a ListView you could do something like this:
Put the names in an ArrayList...
ArrayList<String> nameList = new ArrayList<>();
nameList.add(name1);
nameList.add(name2);
nameList.add(name3);
Then inflate your view and populate your ListView with the ArrayList...
View view = inflater.inflate(R.layout.your_activity_layout, container, false);
ListView listView = (ListView) view.findViewById(R.id.your_listview);
ArrayAdapter arrayAdapter = new ArrayAdapter<>(view.getContext(), R.layout.your_list_item_layout, nameList);
listView.setAdapter(arrayAdapter);

initialized listview with extra data

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.

"Tracking" ListView with Multiple choices

I have a list view set to multiple choices, my question is is there a way to track what was "checked" by its name?
Intent formed = new Intent(this, formedlist.class);
SparseBooleanArray sp=getListView().getCheckedItemPositions();
String str="";
for(int i=0;i<sp.size();i++) {
str+=names[sp.keyAt(i)]+",";
formed.putExtra("strings", str);
if(names[sp.keyAt(i)].toString().equals(<String>))
startActivity(formed);
}
is this how I should be iterating through the string? and is the code for the next class that supposed to show the newly formed list is
if(names[sp.keyAt(i)].toString().equals(<String>))
//get the Listview from the previous class (this is in a new class seperate from other methods
Intent formed = getIntent();
String [] needed = formed.getStringArrayExtra("strings");
//ArrayAdapter will makes strings appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_2, needed));`
the way to see what is inside of the list of checked items?
I don't see a problem using this approach. Are you having a problem doing it this way?
You are not creating the intent correctly. To create an Intent for navigating to a separate Activity, use this format
Intent formed = new Intent(this, OtherActivity.class);
If you are never executing inside the if statement, try logging what values you are comparing, or stepping through with the debugger to see where things go awry.

passing data from a view to an activity

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

Categories

Resources