Android ListView dynamically adding data - android

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();
}

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.

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

Related

Custom layout for each row in a ListView

I am trying to get a button to work in a ListView Row. I been Reading about Custom Adapter but can't get it to work. Basically what i have a List_item class that have 4 TextView and 1 button which fieds a ListView in another class. one of the textViews has id/Name. so what i want is when i press the button on that row. it will get that Value of TextView "name" and add it to a ArrayList and if i press another button on different row it will add the Value of "name" to the same arraylist. so basically will add all the ones that i pressed on the button to an ArrayList. is that possible??.
JUST TO ADD. the textView Are being feed in by my database ..thanks upfront for any replies.
List_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false" >
<TextView
android:id="#+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:visibility="gone" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_weight="3"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="15dp"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
<Button
android:id="#+id/order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:focusable="false"
android:minHeight="0dp"
android:minWidth="50dp"
android:text="+"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tableRow1"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif"
android:visibility="gone" />
</TableRow>
</RelativeLayout>
my menu.xml has
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="697dp" >
</ListView>
now i want to get Values of TextView " name " when i press the button on its row.
that is how i used to fill my listView before i add the button and it was working fine
ListAdapter adapter = new SimpleAdapter (getCategoryItems.this,itemsList,R.layout.list_item, new String[]{ TAG_ID, TAG_NAME,TAG_PRICE, TAG_DESCRIPTION},new int[] { R.id.id, R.id.name, R.id.price, R.id.description});
setListAdapter(adapter);
but now after i added the button it doesn't get any values so i reaserched and i found that i have to change my sampleAdapter to a custom Adapter. and add the button inside getView() but i can't get that hang of it.
my button OnClickListener
Button order = (Button) view.findViewById(R.id.order);
order.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
String name = ((TextView)view.findViewById(R.id.name)).getText().toString();
Log.d("Selected : ", name);
}
});
You will need to create a custom adapter class for your ListView. Since you are getting data from a database, I suggest that you extend CursorAdapter and override newView() and bindView(). From my experience, how to do this correctly is poorly documented, so I'll try to give some pointers here.
In newView(), you need to use a LayoutInflator which is provided to you by calling getLayoutInflator() on your ListActivity. After you call inflate() on this LayoutInflator, you can then get the Button for that specific row and set its OnClickListener.
In bindView(), you do just about all the same things, except that you don't need to inflate a new view since Android is requesting that you reuse an existing View object. You might want to create some extra methods in order to reduce the amount of repeated code.
I hope this helps even though it is explained in English rather than with a code example.

Custom ListView with Switch and TextView in each item

I would like a custom ListView containing a set of queues that a user can subscribe or unsubscribe to. Each item in my listview should consist of a RelativeLayout containing a TextView and a Switch. How do I go about populating this list ?
I have an array of JSON Objects where each JSON Object has a name and a string value(whether this user is subscribed to the queue or not. So :
qInfo[i].optString("name") should set the Text of my TextView.
qInfo[i].optString("value") should set the status of my Switch.
queues_item :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/queuesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/queues_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Switch
android:id="#+id/queues_item_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textOn="Tilmeldt"
android:textOff="Frameldt"/>
</RelativeLayout>
You can just create a custom adapter and then inflate your view and return it with the correct data entered in. There is a link below for an example, the main part is implementing the getView() method.
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

How to remove row in ListView using a SimpleCursorAdapter

I have three different activities sharing exactly the same layout (a ListView and a TextView). The ListView uses this layout for its SimpleCursorAdapter:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingBottom="2dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/locationName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Länsnamn"
android:textAppearance="?android:attr/textAppearanceMedium">
</TextView>
<LinearLayout
android:id="#+id/num_stores_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/numStores_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textSize="10dp"
android:text="Antal butiker: ">
</TextView>
<TextView
android:id="#+id/numStores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="0">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/numItems_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textSize="10dp"
android:text="Antal artiklar: ">
</TextView>
<TextView
android:id="#+id/numItems"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:text="0">
</TextView>
</LinearLayout>
</LinearLayout>
The program goes from activity #1, user picks an item in the list, activity #2 starts, user picks list, activity #3 starts.
Now, all the list items turn up fine just like they should, but for activity #3 I'd like to remove the entire row with id num_stores_row, and I seem to have a hard time doing this. I cannot do this in the setViewBinder since the row is not targeted by the adapter, and I cannot do it outside since the ListView is not activated until the data arrives. I've tried findViewById(R.id.num_stores_row).setVisibility(View.GONE); here and there, but the simulator crashes every time.
How do I remove it?
To add to what Mark said, override SimpleCursorAdapter's getView and put your findViewById(R.id.num_stores_row).setVisibility(View.GONE); in there once you inflate the row's view.
Do not include that row in the Cursor that you wrap in the SimpleCursorAdapter. Or, create your own subclass of SimpleCursorAdapter that overrides a bunch of methods (e.g., getCount()) to filter out that row.

registerForContextMenu not get calling properly - what am I doing wrong?

The answer to this question might be really obvious but its giving me a headache. I have a simple LinearLayout with one single ListView in it. I do this: onCreate
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.friends);
ListView listView = (ListView) findViewById(R.id.friend_list);
listAdapter = new CheckinListAdapter(checkins, listView, R.layout.checkin_list_item);
listView.setAdapter(listAdapter);
if (getLastNonConfigurationInstance() != null) {
FriendsActivity last = (FriendsActivity) getLastNonConfigurationInstance();
this.checkins.addAll(last.checkins);
this.sort = last.sort;
} else {
refresh();
}
registerForContextMenu(listView);
}
But for some reason onCreateContextMenu never gets called! So I did some research and since I am loading the list after the register, perhaps it doesn't register it correctly. If I go in my ListAdapter and do registerForContextMenu it does show up. But it doesn't behave correctly with the keyboard. So I am now confused on what can be the error because it seems a little non intuitive for me to have to register each child item. All the examples I find online are using ArrayAdapter. :(
Any suggestions?
Edit
Here is more detail, in case it something I don't see:
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="#string/check_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onCheckInClicked"/>
<ListView android:id="#+id/friend_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
List item xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<ImageView android:id="#+id/user_photo"
android:layout_width="40dip"
android:layout_height="40dip"
android:scaleType="centerCrop"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="8dip">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="#+id/user" style="#style/TextButton"/>
<TextView android:text="#string/at"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="#+id/venue"
android:singleLine="true"
android:ellipsize="end"
style="#style/TextButton"/>
</LinearLayout>
<TextView android:id="#+id/venue_address" style="#style/GreyLarge"/>
<LinearLayout android:id="#+id/checkin_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip">
<ImageView android:id="#+id/checkin_image"
android:layout_width="70dip"
android:layout_height="60dip"
android:layout_marginRight="8dip"
android:scaleType="centerCrop"/>
<TextView android:id="#+id/checkin_shout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView android:id="#+id/elapsedTime" style="#style/GreySmall"/>
</LinearLayout>
</LinearLayout>
This took me 6 hours to figure out but it turns out I had to add:
android:focusable="false"
to all my <Button/> tags.
Related post: TextView and Button in each row and onListItemClick()
Do, registerForContextMenu(listView);
before setting the adapter, that is before:
listView.setAdapter(listAdapter);
You have mentioned that the menu is not responding correctly with the keyboard actions. Can you tell me what exactly the problem is?

Custom list: EditText consumes list's OnItemClickListener

I've been implementing a number of custom-lists, all worked fine. Now I'm trying to add an EditText to the list's rows. The rows look like that:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/tv_quest_listrow_categorical_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="blabla"
android:paddingLeft="10dp"
android:minHeight="48dp"
android:gravity="center_vertical" />
<EditText android:id="#+id/tv_quest_listrow_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:gravity="right|center_vertical"
android:singleLine="true"
android:enabled="false"
android:imeOptions="actionDone" />
</RelativeLayout>
When constructing the list I set an ItemClickListener for the list with listView.setOnItemClickListener(onItemClick);. This used to work fine but fails as soon as there is an EditText in the list rows.
Does anyone have a hint on how to do this?
Regards,
Steff
I found this page, which has some good information on it. Apparently others have had this issue as well.
Try iterating through the items of the ListView and set the OnItemClick listner individually for each item
thanks

Categories

Resources