i want change the order of listview to be the recent items added be in the top of listview , old be last down
i get the data from sqlite , then fill them to arraylist of objects called reports_items_obj
i use custom adpater to view
ListView list_messages = (ListView) findViewById(R.id.list);
Report_adapter adapter = new Report_adapter(getApplicationContext(), reports_items_obj);
list_messages.setAdapter(adapter);
xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
The order in which listview is based on the order of items in arraylist or cursor or from whereever the data is stored which will be populated in the listview. So try changing the order of arraylist.
Solution lies in the data source. Lets say you are getting the data from a sqlite db, when you are querying the db use order by desc on a column.
You need to specify the data source to give a complete answer.
Related
I implemented this example into my project:
http://camposha.info/source/aandroid-data-passing-from-textbox-to-second-activity
So, and now I want to save the text, I've added through the edittext. Is this possible?
Or can I change the grid view with list view? I want to enter text and the text will be displayed on second activity. If it's in a grid view or list view . I don't know if it's possible to do that with list view. If so, please help me :)
I am beginner, so thanks for helping.
Gustav
I want to save the text, I've added through the edittext. Is this possible?
Yes, You can use SharedPreferences, SQLite Database and other ways. I suggest you read this link which completely explains how to preserve data in android. You can modify DataHolder class, so this class can save and load data from one of these storage, and use it to save texts in MainActivity and load them in SecondActivity.
And I did not understand the second part of your question! Do you want to show data in a ListView instead of GridView? If so, then you can replace that GridView with a ListView in ActivitySecond.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.tutorials.hp.txttoactivitygrid.SecondActivity">
<!-- Replace with this -->
<ListView
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
And then in your SecondActivity.java replace GridView gv; with ListView ls; and then change
gv= (GridView) findViewById(R.id.gv);
gv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, DataHolder.spacecrafts));
with
lv= (ListView) findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, DataHolder.spacecrafts));
The idea is to replace every code which uses GridView to use ListView instead .I think this modification is enough and should work. If it doesn't let me know so I can help you.
Also you can read about ListView and how to use it here. (You can find lots of tutorials when you search for it on the internet :D)
I have a RecyclerView as follows
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nav_header_container"
android:layout_marginTop="15dp" />
I have a string array like this
<string-array name="my_array">
<item>#string/item_1</item>
<!--<item>#string/item_2</item>-->
<item>#string/item_3</item>
<item>#string/item_4</item>
<item>#string/item_5</item>
<item>#string/item_6</item>
This string array is used to display data in RecyclerView. I want to display a textview alongside with item_4, Is it possible? How to make it?
From Activity Pass this list to RecyclerView adapter
try {
String[] array = getApplicationContext().getResources().getStringArray(R.array.my_array);
List<String> list = new ArrayList<String>();
list = Arrays.asList(array);
//pass this list to RecyclerView adapter
}catch (Exception e){e.printStackTrace();}
In your onBindViewHolder method just put a check for the element you want to display differently and do what you want to do with it.
Some solutions:
1.- You can concat a string to the 4th item when you check the position in the onBindViewHolder.
2.- You can create a layout and inflate it on onCreateViewHolder which has two textViews one for the items and one for the text that will go next to it. When you detect that you need to show the second textview change the content and the visibility of the textview.
<LinearLayout android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="#+id/items"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textViewNextToItemOfChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Then on code you just change the visibility of textview "textViewNextToItemOfChoice" and the content of it.
I think you should make an object (You own model) instead of using just an array of string to populate the list. In that way you can create an object with 2 Strings and in the adapter when you check if it has both string you show them.
I have a row.xml file which define a row whose content and number of rows will be dynamic.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/room_detail_row_in_hotel_detail">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/sz_blue"
android:id="#+id/price"/>
</LinearLayout>
The best solution as suggested by #Hell is to use a Listview.
Say you are operating within a fragment, add a listview in the xml for the same.
The fragment (or activity) containing the listview will dynamically create list(preferrable)/array of data.
In your case the dataset will essentially either be a list of class object which uniquely have two objects, price and name.
Create an adapter, could be a BaseAdapter or any other form depending on your specific use-case. This adapter would essentially inflate your row layout (as specified in your question) and dynamically add the data.
Add the data into the adapter using it's constructor. Following which assign this adapter as the listview's adapter and call the "notify" method for the adapter indicating that a new dataset has been loaded and the list needs to be refreshed.
Also note, it's highly suggestible to add smoothScroll to your listview for obvious reasons.
I guess what you are looking for is a RecyclerView.
You should use the Viewholder-Pattern to reuse your layouts
I am currently developing application kind of notepad or Todo list. But I need to know to how to show empty list into listview, I guess android default ListView can not handle empty list view. Basically I need to show listview with no data, which I will add later, but initially the UI for listView should be empty.
Thank You!
this may helps you when there are no data in list
listview.setVisibility(View.INVISIBLE);
nodat.setVisibility(View.VISIBLE);
Hre nodata is Contain Empty Textview with SetText Your String like No Data
if you added the empty list into the adapter and that adapter set to listview then it will show you empty listview. after you add item to the list then you need to call notifydatasetchanged() of the adapter.
ListView as display initially empty till you add an item to the list and notify via adapter.
Note you must call method notifydatasetchanged() of an adapter when you populate the list item for add/edit/delete.
According to Developer Guidelines describing ListView you can easilty setup a TextViev, any other widget will be fine as well. Where you will encurage user to create ListItem.
Here is a snippet.
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
There are a lot of tutorials for creating a ListView to display contents from a single table. (Take this one for example: http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/)
My problem is that I'd like to display two ListViews in a single LinearLayout parent. The ListViews will draw from two different tables in the same database. Will anyone point me to a tutorial that tells me how to do that (in, hopefully, a clean, DRY way)?
Can I use multiple SimpleCursorAdapters? What about the layouts? How will it know where to place the items?
There's no need to use separate ListViews/CursorAdapters, just do a JOIN on your tables for the data you want, and you'll get a single Cursor back. Then you only have to deal with one ListView and one adapter.
You can create a layout like this (two equal sized lists in a vertical LinearLayout):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/list1"
android:layout_weight="1"
android:layout_height="0dip"
android:layout_width="fill_parent"></ListView>
<ListView
android:id="#+id/list2"
android:layout_weight="1"
android:layout_height="0dip"
android:layout_width="fill_parent"></ListView>
</LinearLayout>
Then you just use them in your activity:
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.your_layout);
ListView list1 = (ListView) findViewById(R.id.list1);
list1.setAdapter(...);
ListView list2 = (ListView) findViewById(R.id.list2);
list2.setAdapter(...);
}
Maybe you could insert a colored line between those two list so the user doesn't confuse the list as a single one.
You can create a single adapter which will query both the sources and combine the data and then display it using a single listview.