0

I have a listView that I want to dynamically add data to. here's the XML I want to add the data to.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView android:id="@+id/rowTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="70dp"
    android:padding="12dp"
    android:textSize="16sp" >
  </TextView>

  <TextView android:id="@+id/alarm_name_text" 
   android:layout_height="wrap_content" 
   android:text="" 
   android:layout_below="@+id/rowTextView" 
   android:layout_width="fill_parent">
   </TextView>

  <CheckBox android:id="@+id/CheckBox01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp"
    android:layout_alignParentRight="true" android:layout_marginRight="6sp"
    android:focusable="false">
  </CheckBox>

</RelativeLayout>

I want to add data to the 2nd TextView with the id alarm_name_text after a user has entered some data in an editText dialogView. I've been told to make a function that adds the data the my ArrayList/adapter but I'm not sure when to call it or how it's being used.. I needs help plz :(. Here's the function.

public void addItems(View v)
        {
            rowSavedText.setText(getString());
            planetList.add(new Planet("This one"));
            listAdapter.notifyDataSetChanged();
        }
2
  • Please give more info: what is the class of your adapter? What is the container of your data? Commented Jul 17, 2011 at 17:21
  • it's not necessary for me to put the class, and I specified that I would be using an ArrayList/adapter? I need help regarding how to add/delete data from a listView of just textViews. Should be relatively simple but I'm have great difficulty with it Commented Jul 17, 2011 at 19:00

2 Answers 2

1

You'll need to work on the part of the code that renders the individual rows. Take a look at the tutorial at http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx: specifically how it uses getView.

Sign up to request clarification or add additional context in comments.

5 Comments

This looks extremely helpful, I'm goona check it out right now thanks! :) But I'm still wondering how to dynamically change a textView that I already have from a users input? This just populates the View based on current data.
You can always remove an item and insert a new one into your Adapter: that should do what you want. You'll need to invoke the notifyDataSetChanged method on the adapter to force it to pick up the change.
wow thanks, My brain just started working. Got an idea :) One last question about having to add or remove items from my List. Where should I put the functions to add or remove them? And how are they getting invoked?
You'll need to make sure the invoking happens on the main Activity thread: so in either a listener callback, using a Handler or using View.post. vogella.de/articles/AndroidPerformance/article.html#handler gives a good overview.
Thanks very much. I've had this problem for about a week now and it's preventing me from going any further. I'll keep trying :) One thing that I'm not understanding is why I'm getting an error when I Implement my own getItem() function. I can't declare it of type Object, I have to declare it the type of class I want to be. Unlike in the first link you posted for me, geekswithblogs.net/bosuch/archive/2011/01/31/…
0

Just add the element to your ArrayList and call notifyDataSetChanged() to your array adapter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.