I have created listview and i want to implement sectionindexer and alphabetIndexer in my listview.
I am reffering http://www.anddev.org/tutalphabetic_fastscroll_listview_-_similar_to_contacts-t10123.html. That works excellent. But for my code , i am not getting how to implement that functionality.
here is my list_item.xml code :
<?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"
android:orientation="vertical"
android:gravity="center"
android:background="#drawable/list_selector"
android:padding="5dp"
android:id="#+id/tab">
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/musicsymbol"/>
</LinearLayout>
<TextView
android:id="#+id/songTitle"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:layout_alignTop="#+id/thumbnail"
android:color="#10bcc9"
android:maxLines="1"
android:layout_toRightOf="#+id/thumbnail"
/>
<TextView
android:id="#+id/duration"
android:layout_width="56dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/songTitle"
android:gravity="right"
android:layout_marginRight="5dip"
android:textSize="15dip"
android:textColor="#10bcc9"
android:textStyle="bold"
android:maxLines="1"
/>
<TextView
android:id="#+id/songArtist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/songTitle"
android:textSize="14dp"
android:color="#10bcc9"
android:textStyle="bold"
android:lines="1"
android:layout_toRightOf="#+id/thumbnail"
/>
My JAVA code :
Cursor cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null , null, null,MediaStore.Audio.Media.TITLE );
if (cursor == null)
{
//Query Failed , Handle error.
}
else if (!cursor.moveToFirst())
{
//No media on the device.
}
else
{
int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DATA);
int artistcolumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int durationcolumn =cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);
for(int i=0;i<cursor.getCount();i++)
{
String thisTitle = cursor.getString(titleColumn);
String path = cursor.getString(idColumn);
String artist = cursor.getString(artistcolumn);
Long duration = cursor.getLong(durationcolumn);
Utilities objUtilities = new Utilities();
String timeDuration = objUtilities.milliSecondsToTimer(duration);
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle",thisTitle);
song.put("songPath", path);
song.put("artist", artist);
song.put("duration",timeDuration);
// Adding each song to SongList
songsList.add(song);
cursor.moveToNext();
}
}
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
String[] from = {"songTitle", "artist" , "duration"};
int[] to={R.id.songTitle,R.id.songArtist, R.id.duration};
adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, from, to);
setListAdapter(adapter);
so how to implement sectionindexer with my this listadapter. Please suggest me some solution. Thanx.
You can use this library. This is the simplest Section Adapter available for Android's ListView.
Related
Whatever I set ListView attribute,it has a empty line at the top,I can't get rid of,I find many methods,but can't figure it out.My environment is in Android 4.1,so please help,thanks in advance.
My xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f0f0"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17px" />
<TextView
android:id="#+id/tvSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10px" />
<com.ibelieve.news.View.XListView
android:id="#+id/lvArray"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000" >
</com.ibelieve.news.View.XListView>
</LinearLayout>
Code file:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_list);
listview = (XListView) findViewById(R.id.lvArray);
ArrayList<News> newslist = getIntent().getParcelableArrayListExtra("newslist");
ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
String[] result = new String[newslist.size()];
for (int i = 0; i < newslist.size(); i++) {
for (int j = 0; j < 4; j++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", newslist.get(i).Title);
map.put("source", newslist.get(i).Source + " " + newslist.get(i).Time);
list.add(map);
}
}
simpleAdapter = new SimpleAdapter(NewsListActivity.this, list, R.layout.news_list, new String[] { "title", "source" }, new int[] { R.id.tvTitle, R.id.tvSource });
listview.setAdapter(simpleAdapter);
}
You are using your activity's resource as an item for list view. Move
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17px" />
<TextView
android:id="#+id/tvSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10px" />
To a new resource files and use that resource file in new SimpleAdapter(...) parameters
edit: oh and those "empty lines" are those textviews that you added in your activity's resources. And each your listview item contains an empty listview...ception
I'm new to android and now trying to make a small project.
It includes a listview with textviews and a linear progressbar (its value doesn't need to change).
Simple Adapter was used to fill data to textviews.
But to set progressbar value, which adapter should be used.
I can't image how to set value to progressbar.
Please help with this.
Here is my code:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/dblist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
list_row.xml
<?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"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="#drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
<!-- Title Of Song-->
<TextView
android:id="#+id/col1tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Rihanna Love the way lie"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<!-- Artist Name -->
<TextView
android:id="#+id/col2tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/col1tv"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="Just gona stand there and ..." />
<TextView
android:id="#+id/package_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/col1tv"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/col2tv"
android:text=" puzzles, 0 solved" />
<!-- Rightend Duration -->
<!-- Rightend Arrow -->
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
main.java
String[] from = new String[] {"id", "name", "size"};
int[] to = new int[] { R.id.id, R.id.name, R.id.size };
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < pkg_id_col.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", id_col.get(i));
map.put("name", name_col.get(i));
map.put("size", size.get(i));
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_row_add_new, from, to);
lv_add_new.setAdapter(adapter);
The SimpleAdapter knows how to bind data only for TextViews and ImageView. To bind your ProgressBar use a SimpleAdapter.ViewBinder:
String[] from = new String[] {"id", "name", "size", "progress"};
int[] to = new int[] { R.id.id, R.id.name, R.id.size, R.id.theIdOfTheProgressBar}; // it seems that you don't have a `ProgressBar` in the layout
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < pkg_id_col.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", id_col.get(i));
map.put("name", name_col.get(i));
map.put("size", size.get(i));
map.put("progress", valueForProgressBar); // progress will hold the progress for your ProgressBar
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_row_add_new, from, to);
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (view.getId == R.id.theIdOfTheProgressBar) {
((ProgressBar) view).setProgress(Integer.parseInt(data));
return true;
}
return false;
}
});
I have a custom listview in my application. The needs of the user varies with the option he selects and the result is on the listview. I am representing the same list once by using ArrayList<String> and other time, while using ArrayList<HashMap<String,String>>. Now, while using the former i.e. ArrayList<String>, the result in the listView row gets aligned perfectly but then, while using the later i.e. ArrayList<HashMap<String,String>> the alignment gets worse. I am not understanding what the problem can be. Please help!
Below I am posting the images, code and source code of the XML which is used as a row in my custom Listview.
Images Link: (As I am new user I cannot upload images)
Using ArrayList<String>
http://i46.tinypic.com/2r2b0g5.jpg
Using ArrayList<HashMap<String, String>>
http://i47.tinypic.com/11w7tqe.jpg
Code:
For Arraylist< HashMap< String, String>>
ArrayList<HashMap<String,String>> searchList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<productName.size();i++)
{
String currentListItem=productName.get(i).toString();
if(textLength<=currentListItem.length()){
//compare the String in EditText with Names in the ArrayList
if(searchText.equalsIgnoreCase(currentListItem.substring(0,textLength))){
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("PRODUCTNAME",productName.get(i));
temp.put("PRODUCTID", productID.get(i));
temp.put("BRANDNAME", brandName.get(i));
temp.put("VENDORNAME", vendor.get(i));
temp.put("OPENINGSTOCK", openingStock.get(i));
temp.put("AVAILABLESTOCK", availableStock.get(i));
temp.put("ADDSTOCK", addStock.get(i));
temp.put("PURCHASRPRICE", purchasePrice.get(i));
temp.put("SALESPRICE", salesPrice.get(i));
searchList.add(temp);
}
}
}
SimpleAdapter aaadapter = new SimpleAdapter(
new_stock_io.this,
searchList,
R.layout.list_otcmain123,
new String[] {"PRODUCTID", "PRODUCTNAME", "BRANDNAME","VENDORNAME","OPENINGSTOCK","AVAILABLESTOCK","ADDSTOCK","SALESPRICE", "PURCHASEPRICE"},
new int[] {R.id.txt_id,R.id.txt_name, R.id.txt_bname, R.id.txt_vendor, R.id.txt_openingstock,R.id.txt_availablestock,R.id.txt_addstock,R.id.txt_salesprice,R.id.txt_purchaseprice}
);
lv.setAdapter(aaadapter);
For ArrayList< String>
List<String> vendorName = new ArrayList<String>();
List<String> vendorID = new ArrayList<String>();
List<Object> objectArray = new ArrayList<Object>();
List<String> stockID = new ArrayList<String>();
List<String> picture = new ArrayList<String>();
List<String> productID = new ArrayList<String>();
List<String> productName = new ArrayList<String>();
List<String> brandName = new ArrayList<String>();
List<String> vendor = new ArrayList<String>();
List<String> openingStock = new ArrayList<String>();
List<String> availableStock = new ArrayList<String>();
List<String> addStock= new ArrayList<String>();
List<String> purchasePrice= new ArrayList<String>();
List<String> salesPrice= new ArrayList<String>();
List<String> subCategoryID = new ArrayList<String>();
List<String> subCategoryName= new ArrayList<String>();
List<String> productCategory= new ArrayList<String>();
List<String> subCategoriesNameArray = new ArrayList<String>();
List<String> subCategoriesIDArray= new ArrayList<String>();
for (int i = 0; i < productID.size(); i++)
{
Object obj = new Object();
objectArray.add(obj);
}
adapter = new ListItemsAdapter(objectArray, 1);
lv.setAdapter(adapter);
//List Adapter for listView.
public class ListItemsAdapter extends ArrayAdapter<Object> {
ViewHolder holder1;
public ListItemsAdapter(List<Object> items, int x) {
super(new_stock_io.this,android.R.layout.simple_list_item_1, items);
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.list_otcmain123, null);
holder1 = new ViewHolder();
holder1.tv_id=(TextView)convertView.findViewById(R.id.txt_id);
holder1.tv_name=(TextView)convertView.findViewById(R.id.txt_name);
holder1.tv_brandname=(TextView)convertView.findViewById(R.id.txt_bname);
holder1.tv_openingstock=(TextView)convertView.findViewById(R.id.txt_openingstock);
holder1.tv_availablestock=(TextView)convertView.findViewById(R.id.txt_availablestock);
holder1.tv_addstock=(TextView)convertView.findViewById(R.id.txt_addstock);
holder1.tv_addstock.setTag(position);
holder1.tv_addstock.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
// TODO Auto-generated method stub
Object o = (Object)holder1.tv_addstock.getTag();
Log.d("method focus", "call");
Log.d("position", position+"");
Log.d("edit text value", o.toString());
Log.d("value",":"+holder1.tv_addstock.getText().toString());
addStock.set(position, holder1.tv_addstock.getText().toString());
}
});
holder1.tv_prizeperunit=(TextView)convertView.findViewById(R.id.txt_salesprice);
holder1.tv_vendor=(TextView)convertView.findViewById(R.id.txt_vendor);
holder1.tv_prizeperunitpurchase=(TextView)convertView.findViewById(R.id.txt_purchaseprice);
convertView.setTag(holder1);
if (checkboxClicked==0)
{
holder1.tv_addstock.setTag(objectArray.get(position));
holder1.tv_id.setText(productID.get(position));
holder1.tv_name.setText(productName.get(position));
holder1.tv_brandname.setText(brandName.get(position));
holder1.tv_openingstock.setText(openingStock.get(position));
holder1.tv_availablestock.setText(availableStock.get(position));
holder1.tv_addstock.setId(position);
holder1.tv_addstock.setText(addStock.get(position));
holder1.tv_prizeperunit.setText(salesPrice.get(position));
holder1.tv_vendor.setText(vendor_Name);
holder1.tv_prizeperunitpurchase.setText(purchasePrice.get(position));
}
else if (checkboxClicked==1)
{
//holder1.tv_addstock.setTag(objectArray.get(position));
holder1.tv_id.setText(onCheckProductID.get(position));
holder1.tv_name.setText(onCheckProductName.get(position));
holder1.tv_brandname.setText(onCheckBrandName.get(position));
holder1.tv_openingstock.setText(onCheckOpeningStock.get(position));
holder1.tv_availablestock.setText(onCheckAvailableStock.get(position));
holder1.tv_addstock.setId(position);
holder1.tv_addstock.setText(onCheckAddStock.get(position));
holder1.tv_prizeperunit.setText(onCheckSalesPrice.get(position));
holder1.tv_vendor.setText(onCheckVendor.get(position));
holder1.tv_prizeperunitpurchase.setText(onCheckPurchasePrice.get(position));
}
return convertView;
}
}
//Simple holder class for above Adapter class.
public class ViewHolder
{
TextView tv_id,tv_name,tv_brandname,tv_vendor,tv_openingstock,tv_availablestock,tv_addstock,tv_prizeperunit,tv_prizeperunitpurchase;
}
XML:
<LinearLayout
android:id="#+id/ll_list_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txt_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:gravity="center"
android:text="ID"
android:textSize="6dip"
android:textStyle="bold" />
<View
android:id="#+id/view1"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.07"
android:gravity="center"
android:text="Name"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view2"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<View
android:id="#+id/view3"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" /> -->
<TextView
android:id="#+id/txt_bname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:gravity="center"
android:text="Brand Name"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view3"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_vendor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.08"
android:gravity="center"
android:text="Vendor"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view27"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_openingstock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:gravity="center"
android:text="Opening Stock"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view26"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_availablestock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:gravity="center"
android:text="Available Stock"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view25"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_addstock"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="0.10"
android:gravity="center"
android:hint="Rs."
android:textSize="6dip"
android:textStyle="bold" />
<View
android:id="#+id/view24"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_salesprice"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="0.10"
android:gravity="center"
android:hint="Rs."
android:textSize="6dip"
android:textStyle="bold" android:width="7dp" />
<View
android:id="#+id/view24"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_purchaseprice"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="0.12"
android:gravity="center"
android:hint="Rs."
android:textSize="6dip"
android:textStyle="bold" android:width="7dp"/>
</LinearLayout> </RelativeLayout>
I'm having some issue creating a layout for a league table. In my current layout, i have created a table with consists of two rows. The first row has the headings i want, and the second row has black fields that consist of the data that is obtained. I get the data from my server which contains an xml file. The problem i am having is not getting the data as i can do that.. The problem is that when i display the data i use a ListAdapter which need a layout linked to it. But when i link the appropriate layout, i get a repeat of header after every line... e.g.
TEAM P W L D P
Arsenal 1 1 1 1 1
TEAM P W L D P
Aston Villa 1 1 1 1 1
I'm sure its a simple error to fix.... maybe i have to you something else to display the data???
Below is the code for the main
public class tab_1 extends ListActivity {
public int a = Football_appActivity.league;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
if(a==1){
String xml = XMLfunctions.getXML_prem_table();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(tab_1.this, "League Table", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("team");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("name", "" + XMLfunctions.getValue(e, "name"));
map.put("played", "" + XMLfunctions.getValue(e, "played"));
map.put("won", "" + XMLfunctions.getValue(e, "won"));
map.put("drawn", "" + XMLfunctions.getValue(e, "drawn"));
map.put("lost", "" + XMLfunctions.getValue(e, "lost"));
map.put("points", "" + XMLfunctions.getValue(e, "points"));
mylist.add(map);
}
**
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main_tab,
new String[] { "name", "played", "won", "drawn", "lost", "points"},
new int[] { R.id.item_title, R.id.item_played, R.id.item_won,
R.id.item_drawn, R.id.item_lost, R.id.item_points});**
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(tab_1.this, o.get("name") + " have Won '" + o.get("won") + "' games, and are currently on '" + o.get("points") + "' points!", Toast.LENGTH_LONG).show();
}
});
}
Below is the layout called main_tab.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:padding="7dp"
>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="#+id/table123">
<TableRow>
<TextView
android:text="Name"
android:id="#+id/team_title">
</TextView>
<TextView android:text="P "
android:gravity="right"
android:id="#+id/team_played">
</TextView>
<TextView android:text="W"
android:id="#+id/team_won">
</TextView>
<TextView android:text="D"
android:id="#+id/team_drawn">
</TextView>
<TextView android:text="L"
android:id="#+id/team_lost">
</TextView>
<TextView android:text="P"
android:id="#+id/team_points">
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="#+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp" />
<TextView
android:id="#+id/item_played"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="#+id/item_won"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="#+id/item_drawn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="#+id/item_lost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="#+id/item_points"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
</TableRow>
</TableLayout>
</LinearLayout>
Below is the loyout for ListPlaceHolder.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No data">
</TextView>
</LinearLayout>
Ok cool.. so i have changed the layout of the main_tab.xml and created a new layout called table_header.xml That contains the headings for the league e.g. Name, Played, Won, Lost etc... But now i have an error in the logcat.. Below is the code what for main page...
if(a==1){
String xml = XMLfunctions.getXML_prem_table();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(tab_1.this, "League Table", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("team");
setContentView(R.layout.table_header);
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("name", "" + XMLfunctions.getValue(e, "name"));
map.put("played", "" + XMLfunctions.getValue(e, "played"));
map.put("won", "" + XMLfunctions.getValue(e, "won"));
map.put("drawn", "" + XMLfunctions.getValue(e, "drawn"));
map.put("lost", "" + XMLfunctions.getValue(e, "lost"));
map.put("points", "" + XMLfunctions.getValue(e, "points"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main_tab,
new String[] { "name", "played", "won", "drawn", "lost", "points"},
new int[] { R.id.item_title, R.id.item_played, R.id.item_won,
R.id.item_drawn, R.id.item_lost, R.id.item_points});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(tab_1.this, o.get("name") + " have Won '" + o.get("won") + "' games, and are currently on '" + o.get("points") + "' points!", Toast.LENGTH_LONG).show();
}
});
The line for changing the layout i have used is:
setContentView(R.layout.table_header);
is this correct????
The first error i get is (there is a long list, but i think this is the cause):
04-15 09:46:42.268: E/AndroidRuntime(346): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.julian.football_app/com.julian.football_app.tab_1}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
As you suspected, your problem is here:
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main_tab,
new String[] { "name", "played", "won", "drawn", "lost", "points"},
new int[] { R.id.item_title, R.id.item_played, R.id.item_won,
R.id.item_drawn, R.id.item_lost, R.id.item_points});
Specifically this:
R.layout.main_tab
That is supposed to be the list row layout. Since you provided that with header and data, it is repeating the header for each list line. Separate them. You need to create a layout for JUST the list. Basically remove the second TableLayout from that file, put it in it's own XML file and call that one for your list adapter. Add your ListView underneath the header table layout in main_tab.
row_layout.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:id="#+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="2dp"
android:textSize="20dp" />
<TextView
android:id="#+id/item_played"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="#+id/item_won"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="#+id/item_drawn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="#+id/item_lost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
<TextView
android:id="#+id/item_points"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp" />
</TableRow>
</TableLayout>
I have a list view with following row template.
<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout android:clickable="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#drawable/android_btn_large" android:gravity="center_vertical"
android:layout_weight="1">
<TextView android:id="#+id/txtLeftButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="#+id/imgLeftButton"
android:text="Riverside Park" android:textColor="#FFFFFF"
android:layout_alignParentLeft="true"></TextView>
<ImageView android:id="#id/imgLeftButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="#id/txtLeftButton"
android:layout_alignParentLeft="true" android:layout_centerVertical="true"
android:layout_alignParentRight="true" android:src="#drawable/plus_icon_480">
</ImageView>
</RelativeLayout>
<RelativeLayout android:clickable="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#drawable/android_btn_large" android:gravity="center_vertical"
android:layout_weight="1">
<TextView android:id="#+id/txtRightButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="#+id/imgRightButton"
android:text="Riverside Park" android:textColor="#FFFFFF"
android:layout_alignParentLeft="true"></TextView>
<ImageView android:id="#id/imgRightButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="#id/txtRightButton"
android:layout_alignParentLeft="true" android:layout_centerVertical="true"
android:layout_alignParentRight="true" android:src="#drawable/plus_icon_480">
</ImageView>
</RelativeLayout>
</LinearLayout>
And the data is stored in a List as...
List<String[]> rows = new ArrayList<String[]>();
rows.add(new String[] { "id", "name" });
I want to know what Adapter I should use to fill the ListView with these rows assuming I need 2 column data in a single row?
Just use a SimpleAdapter with a list of HashMap
ListView list = (ListView) findViewById(R.id.ListView01);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", "1");
map.put("name", "bob");
mylist.add(map);
//Each row is a new hashmap
map = new HashMap<String, String>();
map.put("id", "2");
map.put("name", "sally");
mylist.add(map);
//ect...
mSchedule = new SimpleAdapter(this, mylist, R.layout.listrow,
new String[] {"id", "name"}, new int[] {R.id.txtID, R.id.txtName});
list.setAdapter(mSchedule)
You could implement the ListViewAdapter interface in a class and inflate the row-xml in the getView-method. You can use a regular activity with a simple LinearLayout and a list view in it.