simple listview (not activity) example? - android

I've been doing this for a few months now and I've found it almost impossible to find an example where the ListView is a ListView not a ListActivity. In my program all I'm wanting to do is have half the screen a ListView and the other half something else so the ListView can be moved around in the layout. Does anyone know any such examples?

I have created implementation of ListView, not ListActivity.
list_produk.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvListProduk"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.98"
tools:listitem="#layout/list_produk_row" >
</ListView>
<Button
android:id="#+id/btnListProdukBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back_" />
</LinearLayout>
list_produk_row.xml:
<?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" >
<TextView
android:id="#+id/tvProdukName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
ListProduk.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_produk);
String[] from = new String[] {"prd_name"};
int[] to = new int[] {R.id.tvProdukName};
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("prd_name", "BlackBerry");
fillMaps.add(map);
map = new HashMap<String, String>();
map.put("prd_name", "Android");
fillMaps.add(map);
map = new HashMap<String, String>();
map.put("prd_name", "iPhone");
fillMaps.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_produk_row, from, to);
ListView listView = (ListView) findViewById(R.id.lvListProduk);
listView.setAdapter(adapter);
}
list_produk_row.xml, that contains a TextView, is the layout for list item of ListView.

Related

How to add a footer using AddFooterView with text from a String?

I've made an application with a ListView. I'm to trying to add a footer to my ListView (which scrolls along with the ListView), using addFooterView. In this footer I want to add a TextView with some text which is defined in my Activity as a String called notification.
I've tried several things but it wouldn't work.
My ListActivity, RoosterWijzigingen.java:
public class RoosterWijzigingen extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
ArrayList<HashMap<String, String>> roosterwijziginglist = new ArrayList<HashMap<String, String>>();
String hour = "4";
String info = "Af 123";
String notification = "Het is 40min-rooster!";
// Creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// Adding each child node to HashMap key => value
map.put("hour", hour);
map.put("info", info);
// Adding HashList to ArrayList
roosterwijziginglist.add(map);}
ListAdapter adapter = new SimpleAdapter(this, roosterwijziginglist , R.layout.roosterwijzigingen,
new String[] {"hour", "info"},
new int[] {R.id.hour, R.id.info});
setListAdapter(adapter);
setContentView(R.layout.listplaceholder);
}
}
I get the information for the strings hour, info and notification from a JSON file, but I omited this to shorten the code.
listplaceholder.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true" />
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No data" />
roosterwijzigingen.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/hour"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/info"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</LinearLayout>
You have to put your String in a TextView before adding it to your list, and it will look something like that :
String footerText = "Footer text";
TextView textView = new TextView(this);
textView.setText(footerText);
getListView().addFooterView(textView);
Remember that 99% of the time when you want to display something on the screen, it has to be a View.

Android - is there a way to have a 2-column row layout and tell which column a user presses?

In a 2-column row layout, is there a way to tell which of the columns the user clicked or pressed on?
If the second column can be a button, it would be even better, but I am not sure how to make it into a button. Here is what I am doing:
private List<HashMap<String, String>> fillMaps;
private SimpleAdapter simple_adapter;
ListView list = null;
and later:
list = (ListView) findViewById(android.R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
// My data
fillMaps = new ArrayList<HashMap<String, String>>();
simple_adapter = new SimpleAdapter(this, fillMaps, R.layout.comment_list,
new String[] {"train", "from"},
new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL });
// This was the middle item R.id.FROM_CELL,
list.setAdapter(simple_adapter);
list.setTextFilterEnabled(true);
and I populate the list like this:
for ( int i = 0; i < obj.length(); i++ )
{
JSONObject o = obj.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
... Some variables set here...
map.put("train", comment);
map.put("from", "Edit");
fillMaps.add(map);
discussion.add( d );
}
}
simple_adapter.notifyDataSetChanged();
Here is the comment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:weightSum="1.0"
android:orientation="horizontal">
<TextView android:id="#+id/TRAIN_CELL"
android:textSize="16sp"
android:layout_height="wrap_content"
android:layout_width="275dip"/>
<TextView android:id="#+id/FROM_CELL"
android:textSize="16sp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_width="50dip"/>
</LinearLayout>
Thanks!
try using some clickable items in the two columns of the row and handle those click events.
Suppose give two buttons in those two columns and add setOnClickListeners to them and finally check which button is click in the activity
You can use something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:weightSum="1.0"
android:orientation="horizontal">
<Button
android:id="#+id/TRAIN_CELL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/FROM_CELL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_grid_view" />
</LinearLayout>
Be back if you have any issues

Custom list item to ListView android

I have been playing with the list activity tutorial here:
http://developer.android.com/resources/tutorials/views/hello-listview.html
which tells you to start off extending List activity.
by public class Main extends ListActivity {
Which is based on inflating a textview only layout.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
If I want to customize the layout more by adding images,and an extra linear layout above the list adapter etc- is it possible using this method- if so how do I do it?
It is possible by using a SimpleAdapter.
Here is an example :
// Create the item mapping
String[] from = new String[] { "title", "description" };
int[] to = new int[] { R.id.title, R.id.description };
Now "title" is mapped to R.id.title, and "description" to R.id.description (defined in the XML below).
// Add some rows
List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "First title"); // This will be shown in R.id.title
map.put("description", "description 1"); // And this in R.id.description
fillMaps.add(map);
map = new HashMap<String, Object>();
map.put("title", "Second title");
map.put("description", "description 2");
fillMaps.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.row, from, to);
setListAdapter(adapter);
This is the corresponding XML layout, here named row.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
I used two TextViews but it works the same with any kind of view.

How to make a android interface with "football league table" style?

I need to show a football league table on my app. Like this: http://www.fuencalientedelapalma.org/sitios/Fuencaliente/VF7/PublishingImages/Clasificacion.png
As you see i need to do some kind of Android XML layout that shows 9 columns, and 10 rows, plus one more row for the name of the column.
I read all the documentation of listview on the android developers guide, but i can't find the way to do it. And also i search on google and i can't find the way to do it.
Code examples are welcome
thanks
You can use listView like this, inflating you own listlayout :
maListViewPerso = (ListView) findViewById(R.id.listviewperso);
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i<statDB.getListStat().size(); i++){
Stat stat= statDB.getListStat().get(i);
listId.add(statDB.getListStat().get(i).getId()+"");
String message = " Chiffre d'affaire : "+statDB.getListFinancier(stat.getId()+"").get(0).getCaBrut()+" euros";
map = new HashMap<String, String>();
titre
map.put("titre", stat.getDate());
map.put("description", message);
map.put("img", String.valueOf(R.drawable.icon_crisalid));
map.put("stat_in",""+statDB.getListStat().get(i).getId());
listItem.add(map);
}
statDB.close();
SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.listview,
new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
maListViewPerso.setAdapter(mSchedule);
maListViewPerso.setOnItemClickListener(new OnItemClickListener() {
#Override
#SuppressWarnings("unchecked")
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
HashMap<String, String> map = (HashMap<String, String>) maListViewPerso.getItemAtPosition(position);
Intent intent=new Intent().setClass(ListActivity.this, DetailActivity.class);
intent.putExtra("stat_in", map.get("stat_in") );
intent.putExtra("listId",listId);
startActivity(intent);
}
});
this is my listview code :
<?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"
>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="20px"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10px"
android:layout_weight="1"
>
<TextView android:id="#+id/titre"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="16px"
android:textStyle="bold"
android:textColor="#color/textcol"
/>
<TextView android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="12px"
android:textColor="#color/textcol"
android:paddingRight="10px"
/>
</LinearLayout>
and this is how I check the listview :
<?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="#color/backgroundcolor"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:paddingTop="5px"
android:paddingBottom="5px"
android:background="#drawable/bg2"
>
<TextView
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
/>
</LinearLayout>
<ListView
android:id="#+id/listviewperso"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
you just have to adapt the listview.

How can I set up an inline ListView?

I'm just starting to develop on Android so this may be a very basic thing, but. . .
I'm having a really hard time setting up a ListView within a LinearLayout. Maybe it's a problem in my XML layout, or maybe it's a problem with my databinding code.
I'm confused by the many tutorials that say that my Activity should extend ListActivity. Why would this one widget require that my entire UI be changed?
The thing is, I only want the scrolling box of items to occupy a part of this one screen. Is ListView the wrong widget to use? Should I be using a ScrollView instead?
Thanks -- here's my XML:
<?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="#FFF"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/enter_text"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:id="#+id/txtTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:width="200px"/>
</LinearLayout>
<ListView android:id="#+id/listRecent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And here's my code:
public class MyApp extends Activity {
private String TAG = "MyApp";
//UI ELEMENTS
EditText txtTo;
ListView listRecent;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//GET REFERENCES TO UI ELEMENTS
listRecent = (ListView) this.findViewById(R.id.listRecent);
try {
//----------------------------------------
// create the grid item mapping
String[] from = new String[] {"zero", "one", "two", "three"};
int[] to = new int[] {0, 1, 2, 3};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("col_1", "col_1_item_" + i);
map.put("col_2", "col_2_item_" + i);
map.put("col_3", "col_3_item_" + i);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.recently_dingdonged, from, to);
listRecent.setAdapter(adapter);
//----------------------------------------
}
catch(Exception e) {
Log.e(TAG, "Error here (3116019)", e);
}
}
ListView should be nested inside of scrollview. By extending listActivity you gain the ability to setAdapter and populate the data into your list view through the main object and not through instantiating the list. This is by no means a requirement though.
You don't need to make your Activity extend ListActivity. You can, but it's not necessary.
In relation with your xml, try this out:
<?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="#FFF"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/enter_text"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/txtTo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="0.5"
android:width="200px"/>
</LinearLayout>
<ListView android:id="#+id/listRecent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
</LinearLayout>

Categories

Resources