I'm a bit confused. Was wondering if you could help. If you need any additional details please let me know. thanks :D
-
Are you trying to dynamically add items to your list view - meaning, does your app start first by showing a list, and then allow the user to "add new items"? This is important to understand the flow of your app because the approach to achieving this might be different. – ishmaelMakitla Apr 20 '16 at 21:39
-
Yes in deed. That's exactly what i'm trying to do. The picture should help. It has all of the full details. – Isaiah Reid Apr 20 '16 at 21:45
3 Answers
In your Activity that shows the list of items - you need to add a button (that allows the user to add a new item) - when this button is clicked, you then start the "add item activity" (using startActivityForResult()
) as Max suggests. When the user clicks the submit button on the "add item activity" - you then set the results setResult(Activity.RESULT_OK, resultIntentObject)
(which will be received back by the activity that shows the list of items in the method onActivityResult(int requestCode, int resultCode, Intent data))
. Now, if the user did insert data, you "refresh" the list of items - you do this by calling adapter.notifyDataSetChanged()
after inserting the new data.
Let me further suggest that you look at the accepted answer describing how to Add Items to ListView, you can also explore options provided here on how to Dynamically add elements to a listView Android

- 1
- 1

- 3,784
- 3
- 26
- 32
-
So does the button have to be on the same activity of the list. I was thinking maybe it could be on the description activity. Is it mandatory that I add the button to the same activity of the listview? – Isaiah Reid Apr 20 '16 at 22:28
-
But how will you add a new item if you start off with an empty list on the first Activity/screen? You will need an Add button, preferably on your ActionBar - [see how to Add an ActionBar](http://developer.android.com/intl/pt-br/training/appbar/index.html) – ishmaelMakitla Apr 21 '16 at 10:55
If you have made the button in some other XML
than the main and the onClick
method is located in some other Activity than MainActivity
then you need to make an onClick
method in the MainActivity
and call the method from the other activity in your MainActivity
.
Also, worth pointing out it is that you should use RecyclerView
instead of ListView
.
I hope this was of any help. I couldn't exactly make out what you are trying to do and this is how I understood it.
Suppose this is your XML code for the button :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clearCanvas"
android:onClick="clearCanvas"
android:layout_gravity="bottom|center"
android:text="CLEAR"/>
On this you clearly have set the onClick
for the method clearCanvas
.
Now, this method will be looked for in your mainActivity
.
If you have another activity in which the onClick
method for the said Button exists, remove the parameters inside the braces of that method.
Example : ActiviyNotMain.java :
//...
public void clearCanvas(View v) {
*do this whatever is here*
}
//...
Remove
View v
from here.
MainActivity.java :
//...
private ActivityNotMain activity;
//...
public void clearCanvas(View v) {
activity.clearCanvas();
}
//...
Note : In the
MainActivity
do not removeView v
I hope this helps.

- 1,438
- 2
- 15
- 28
You can use fragments instead because they provide more flexibility on it. Then the Activity has control of both "Activities" (Fragments) and you can toogle between them with .replace on the FragmentManager. The other thing you can do it's pass the data using parcelable but you have to keep in a database or something the previous items. With fragments you don't need that. If you could provide more information of what exactly you want to do it would be really helpful. Thanks!

- 1,295
- 11
- 21