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)
Related
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.
How do I findViewById for the following XML?
<ListView android:id="#android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
I need it to set a list adapter.
Your XML must look like this:
<ListView android:id="#+id/ListView_Name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
in your code:
ListView listview = (ListView) findViewById(R.id.ListView_Name);
findViewById(android.R.id.list)
The ids prefixed with android: are all accessible under android.R.id. Similarly #android:drawableare accessible as android.R.drawable, etc...
This basic example shows you how to use an ArrayAdapter with a ListView. You should really need to ever actually mess with the ListView itself, just the elements that are put in it.
http://developer.android.com/resources/tutorials/views/hello-listview.html
I need to show a list of items, the items are read from a database, and it is possible there is no item, in this case, I just want to show a TextView saying "there is no item", I think I could implement this by using relative layout, both list and text are in center of parent, they are displayed alternatively, but is there any way better than this?
Thanks!
Adding to Aleadam , Bill Mote
You may call at any time AdapterView.setEmptyView(View v) on an AdapterView object to attach a view you would like to use as the empty view.
The snippet is as follows:
empty = (TextView)findViewById(R.id.empty1);
list = (ListView)findViewById(R.id.list1);
list.setEmptyView(empty);
Make sure you keep the ListView and TextView inside the same parent.
For detailed description please refer this
If you're using a ListActivity, that is the default behavior. If not, then you can set the ListView visibility to GONE and a TextView visibility to VISIBLE.
Aleadam is right. Here's an example XML in support of his answer:
<?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"
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#drawable/red"
android:gravity="center_vertical|center_horizontal"
android:textStyle="bold"
android:text="#string/error_no_groups"
/>
</LinearLayout>
The first tutorial on the Android developer website explains how to do this:
http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
(Look under Step 4)
Snippet:
The ListView and TextView can be thought as two alternative views, only one of which will be displayed at once. ListView will be used when there are notes to be shown, while the TextView (which has a default value of "No Notes Yet!" defined as a string resource in res/values/strings.xml) will be displayed if there aren't any notes to display.
The View with the empty id is used automatically when the ListAdapter has no data for the ListView. The ListAdapter knows to look for this name by default. Alternatively, you could change the default empty view by using setEmptyView(View) on the ListView.
I have listview collection that show id and name in Android. I following example from this resource site.
But actually that example only display name. I wish similar like checkboxlist in ASP.NET that contain property text and value. So the text to show name and value store id. When user select one item, I could retrieve id by viewtext.
My question is how do I set id and name in same viewtext item? I wish could pass the id to another layout.
I know that possible using multi viewtext then set invisible of viewtext using map. How that could be waste memory.
i would suggest a custom listview. you can google for "custom listview" and will find a bunch of tutorials on that. simply said, it is a listview that has a custom row that you can specify in a xml file.
here is a quite useful tutorial to start with.
here is what i coded, maybe it will help you. note that this is a bit different, because i am not defining a listactivity but a listview widget. but concerning the custom row item, it won't matter.
this will create a listview and specify an adapter for that listview.
CustomListView listview = (CustomListView) findViewById(R.id.list);
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String(getApplicationContext(), R.layout.rowoflistview, R.id.label);
listview.setAdapter(arrayadapter);
my rowoflistview.xml looks like the following. it adds an image and a text to each row of the list. you can of course change it to (mostly) whatever you want.
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/rowselector"
>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/musicicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/musicicon"
android:paddingLeft="3px"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="26px"
android:layout_marginLeft="3px"
android:singleLine="true"
android:ellipsize="end"
android:focusable="false"
/>
</LinearLayout>
as i wanted the listview as a widget in my main activity and not as a fullscreen activity, i had to do it other than you when it comes to event listening and click listening. if you want it a bit easier, be sure to extend the listactivity in your custom listview-activity and override the default methods.
hope that was understandable to get a grip on the topic ;)
Views have an associated tag Object map, which could contain your id http://developer.android.com/reference/android/view/View.html#getTag%28int%29
I want to create a custom view (extends View) which contains an image and a text.
Is it possible to set an listener just for image if i use canvas.drawBitmap(imageBitmap, ...) method?
Thank you!
If you want your view just to show an image and a text, why not create a specific layout in your XML file and use setContentView to display the view?
If thats what you were looking for, I think that is the easiest approach. XML example coming soon.
Here is the contents of my personal view to display my main application, just to demonstrate how I can switch view's, your view would be different including a TextView and an ImageView.
dblist.xml inside of res/layout folder.
<?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"
android:background="#FFFFFF">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"/>
</LinearLayout>
This contents display my database results onto a Spinner
I can call that view by using:
setContentView(R.layout.dblist);
And if I want to return to my main layout, I can switch the view by calling
setContentView(R.layout.main);
EDIT:
You can manipulate the view you are working with programmatically by using findViewById method. Let's use my dblist.xml file as an example, notice that my ListView has an id of list. So I would call:
ListView myList = (ListView) findViewById(R.id.list);
With corresponds to that layout, so whenever I want to change anything to my XML file, I would reference myList use methods and callbacks to meet my needs.
I hope this helps.