Android: About ListView and SimpleAdapter - android

I've tried to using a SimpleAdapter to fill a ListView but only to failed.
I've read many cases but still can't find a useful method.
Here is the Code. I wonder what's the problem.
ArrayList<HashMap<String,Object>> listdata=new ArrayList<HashMap<String,Object>>();
for (int i=0;i<3;i++){
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("title", "title");
hm.put("context", "context");
listdata.add(hm);
}
String[] from = {"title", "context"};
int[] to={R.id.title,R.id.context};
SimpleAdapter adapter = new SimpleAdapter(this, listdata, R.layout.list_item, from, to);
ListView lv=(ListView)this.findViewById(R.id.listView1);
lv.setAdapter(adapter);
//code for the list_item
<LinearLayout
<ListView android:layout_height="wrap_content" android:id="#+id/listV1" android:layout_width="match_parent">
<LinearLayout>
<TextView android:layout_height="wrap_content" android:id="#+id/title" android:text="TextView" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<TextView android:text="TextView" android:id="#+id/context" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</ListView>
I do appreciate if anyone can help!

I suspect the problem is with your Xml, try declaring a second layout file for the list item. e.g.
your_activity.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"
>
<ListView android:layout_height="wrap_content" android:id="#+id/listView1" android:layout_width="fill_parent" />
</LinearLayout>
and list_item.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">
<TextView android:layout_height="wrap_content" android:id="#+id/title" android:text="TextView" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<TextView android:text="TextView" android:id="#+id/context" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
Your code looks ok and should work correctly if you use the above two layout files.

I have always put my list items in a seperate xml file. Maybe try putting your LinearLayout with the two TextViews in a file (maybe call it listrow.xml).
As an example:
Here is the listitem (list_stat_item.xml):
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="" android:id="#+id/txtKey"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="10dp" android:textSize="16sp"></TextView>
<TextView android:text="" android:id="#+id/txtValue"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="10dp" android:textSize="16sp"></TextView>
</LinearLayout>
And here is the main view (view_stats.xml):
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1"
android:transcriptMode="normal" android:divider="#drawable/list_divider"
android:dividerHeight="1px" />
</LinearLayout>
And here is the code:
Create an ArrayList, and add items to it, then bind to a custom adapter.
ArrayList<StatItem> lst = new ArrayList<StatItem>();
lst.add(new StatItem("Current Week: ", String.valueOf(currentWeek)));
lst.add(new StatItem("Weeks Left: ", String.valueOf(GeneralUtils.daysUntilBirth(this, pregnancyID) / 7)));
lst.add(new StatItem("Days Left: ", String.valueOf(GeneralUtils.daysUntilBirth(this, pregnancyID))));
lst.add(new StatItem("Current Trimester: ", String.valueOf(currentTrimester)));
lst.add(new StatItem("Posts: ", String.valueOf(blogCount)));
lst.add(new StatItem("Baby Names: ", String.valueOf(babyNamesCount)));
getListView().setSelector(android.R.color.transparent);
StatListAdapter sa = new StatListAdapter(this, R.layout.list_stat_item, lst );
setListAdapter(sa);
If you want to see the adapter code (StatListAdapter extends ArrayAdapter):
public class StatListAdapter extends ArrayAdapter<StatItem>
{
public ArrayList<StatItem> items;
public StatListAdapter(Context context, int textViewResourceId, ArrayList<StatItem> objects) {
super(context, textViewResourceId, objects);
this.items = objects;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.list_stat_item, null);
}
StatItem i = items.get(position);
if (i != null)
{
TextView kt = (TextView)v.findViewById(R.id.txtKey);
TextView vt = (TextView)v.findViewById(R.id.txtValue);
kt.setText(i.key);
vt.setText(i.value);
}
return v;
}
}

Related

Android listview hide button

I have a problem how to hide the button in specific row in my listview with SimpleAdapter. This is my sample code.
alert_noti_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
alert_pop_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view1"
android:textSize="20dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/confirm"
android:layout_toStartOf="#+id/confirm" />
<TextView
android:id="#+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="10dp"
android:textStyle="bold"
android:visibility="visible"
android:layout_below="#+id/view1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/confirm"
android:layout_toStartOf="#+id/confirm" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/confirm"
android:layout_alignBottom="#+id/view2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/ic_confirm" />
</RelativeLayout>
Activity :
List<Map<String, String>> listData = new ArrayList<Map<String, String>>();
Map<String, Object> names = new HashMap<String, Object>();
names.put("First","head1");
names.put("Second", "txt1");
names.put("First","head2");
names.put("Second", "txt2");
names.put("First","head3");
names.put("Second", "txt3");
...
listData.add((HashMap) names);
ListView lv = (ListView) dialog_mail.findViewById(R.id.listView1);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(home.this,, android.R.layout.two_line_list_item, names);
SimpleAdapter adapter = new SimpleAdapter(home.this, listData,
R.layout.alert_pop_list,
new String[] {"First", "Second" },
new int[] {R.id.view1, R.id.view2});
lv.setAdapter(adapter);
how do i hide the button if the value is head2 and show to the rest of the list.
You need to custom a BaseAdapter instead of SimpleAdapter and write some code at getView() method。 just like:
public View getView(int p,View view,ViewGroup parent) {
if (view == null) {
view = LayoutInflate.xxxx;
}
Button btn = (Button) view.findViewById(R.id.btn);
DataModel d = list.get(p);
if (d.isHidden || d.data.equals("head2")) {
btn.setVisibility(View.GONE);
} else {
btn.setVisibility(View.visible);
}
return view;
}
so you need to create a DataModel to store some property.
public class DataModel {
public boolean isHidden;
public HashMap<String,String> dataMap;
//and so on.....
}

want to use my own layout at ArrayAdapter

I use the following code to use the ArrayAdapter for my ListView:
ArrayList<String> mylist = new ArrayList<String>();
String xml = ParseXMLMethods.getXML();
Document doc = ParseXMLMethods.XMLfromString(xml);
NodeList children = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < children.getLength(); i++) {
Element e = (Element)children.item(i);
mylist.add(ParseXMLMethods.getValue(e, KEY_TITLE));
mylist.add(ParseXMLMethods.getValue(e, KEY_CITY));
mylist.add(ParseXMLMethods.getValue(e, KEY_COUNT));
}
adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
mylist);
list2.setAdapter(adapter1);
Here I am able to see only Title, but I want to see all three in my listView Row.For this I create another layout R.layout.list_layout2 as follows:
My layout
<?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="50dp"
android:paddingLeft="6dp"
>
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:padding="1dp"
android:textSize="16dp"
android:textStyle="bold"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="1dp"
android:textSize="12dp"
android:textColor="#000000" />
<TextView
android:id="#+id/subtitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="12dp"
android:textColor="#000000"
android:layout_toRightOf="#+id/subtitle" />
</RelativeLayout>
</LinearLayout>
How can I use this layout?
You're going to want to look at a Custom ListView Adapter. Note that your original code uses a custom ListView but not the Adapter. You're going to want to create your own Adapter that extends ArrayAdapter since your ListView row is more complex than just one TextView.
See Tutorial and enter link description here.
Ex:
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
CustomList.java
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context, String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
In the example above, you're going to want to switch out the ImageView and replace it with your TextViews.

How to get two textviews in a listview on the same line?

I have two TextViews tvA and tvB which appear in a ListView lvA.
tvA and tvB display data pulled from a Sqlite Database using Cursor.
Cursor cursor = myDb.getAllRows();
String[] from = new String[]
{DBAdapter.KEY_A,DBAdapter.KEY_B};
int[] to = new int[]
{R.id.tvA,R.id.tvB};
SimpleCursorAdapter myCursorAdapter =new SimpleCursorAdapter(this,R.layout.lvA, cursor,from,to);
myList = (ListView) findViewById(R.id.lvA);
myList.setAdapter(myCursorAdapter);
registerForContextMenu(myList);
The issue is that tvA and tvB doesn't appear as one item in lvA (i.e. they don't appear on the same line) and appear as two different items.
Below is the 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="?android:attr/listPreferredItemHeight"
android:padding="5dip"
>
<TextView
android:id="#+id/tvA"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_toRightOf="#id/tvA"/>
</RelativeLayout>
If you want both the TextViews in the same align then you can use LinearLayout with android:layout_weight attribute.
To have equal width TextView, give same weight value.
You can use TableLayout
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView android:id="#+id/tvA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
<TextView android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
</TableRow></TableLayout >
or LinearLayout with android:orientation="horizontal"
and TextView with android:layout_weight="1"
Here's how I do it with three items on the same row = This is my item list filled from a cursor
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView_StudentName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:layout_gravity="left"
android:text="#string/student_id"
android:padding="5dp"></TextView>
<TextView
android:id="#+id/TextView_StudentLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:gravity="center"
android:text="#string/student_Location"
android:padding="5dp"></TextView>
<TextView
android:id="#+id/TextView_StudentClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:layout_alignParentRight="true"
android:text="#string/student_class" >
</TextView>
</RelativeLayout>`
I'm in the process of replacing the hard sizes with info from the dimen file, but so far this has worked.
TRY this :
Main Java:
//DECLARE:
private Map<String, Map<String, String>> mMenuView = new HashMap<String, Map<String, String>>();
//IN onCreateView
Adapter mAdapter = new NewAdapter(getActivity().getApplicationContext(), mMenuView);
mListView.setAdapter(mAdapter);
// ADD onActivityCreated or onStart save data from your DB and store in mMenuView
Map<String, String> tempMap = new HashMap<String, String>();
tempMap.put("TEXTVALE_A", datafromdb_A);
tempMap.put("TEXTVALE_B", datafromdb_B);
mMenuView.put(Integer.toString(mMenuView.size()), tempMap);
}
//NEWADAPTER
private class NewAdapter extends BaseAdapter {
private Context mContext;
private TextView mTitle1;
private TextView mTitle2;
private Map<String, Map<String, String>> mData = new HashMap<String, Map<String, String>>();
public NewAdapter(Context applicationContext,Map<String, Map<String, String>> mMenuValue) {
mContext = applicationContext;
mData = mMenuValue;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Map<String, String> getItem(int position) {
return mData.get(Integer.toString(position));
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Map<String, String> data = mData.get(Integer.toString(position));
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.linear_layout, null);
mTitle1 = (TextView) convertView.findViewById(R.id.t1A);
mTitle2 = (TextView) convertView.findViewById(R.id.t1B);
}
if (data != null) {
mTitle1.setText(data.get("TEXTVALE_A"));
mTitle2.setText(data.get("TEXTVALE_B"));
}
return convertView;
}
}
linear_layout.xml
<LinearLayout
android:id="#+id/content_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/tab1_wrapper"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="center|center_horizontal"
>
<TextView
android:id="#+id/t1A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="13sp"
android:gravity="center|center_horizontal"
android:textColor="#color/text_color3x"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2_wrapper"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="0.5"
android:orientation="vertical"
android:gravity="left|center_vertical|center_horizontal"
>
<TextView
android:id="#+id/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="13sp"
android:gravity="center|center_horizontal"
/>
</LinearLayout>
</LinearLayout>

Android : Listview of text and Image and the weird scrolling behaviour

In my application I need to create two lists each with different data . I have designed the UI like adding two listviews one after another in a TableLayout within a LinearLayout. I have stored the data in two String arrays and used arrayadapter to populate into the listview.My listview contain a textview and an image.The problem is
1.There is no error shown in the code but the list is getting populated in weird manner. 1st list item is shown, 2nd,3rd,4th list item not shown and 5th is shown 6th not shown, from 7th its fine. When i scroll up, the list is showing all the list items.
2.each list should have its own scrolling control. like if I scroll List "A" ,it should only be scrolled not the List"B" and vice versa. I have attached the code here . Thanks in advance.
mainscreen.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_LinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/request_Table"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#color/black"
android:orientation="vertical"
android:padding="20dp" >
<TableRow
android:id="#+id/request_Row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:orientation="vertical" >
<TextView
android:id="#+id/request_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="REQUESTS"
android:textColor="#color/white"
android:textStyle="bold" />
<ImageView
android:id="#+id/widget163"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</TableRow>
<TableRow
android:id="#+id/request_Row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/gray"
android:orientation="horizontal" >
<ListView
android:id="#+id/request_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:scrollbarAlwaysDrawVerticalTrack="true" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/offered_Table"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#color/black"
android:orientation="vertical"
android:padding="20dp" >
<TableRow
android:id="#+id/offered_Row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:orientation="horizontal" >
<TextView
android:id="#+id/offered_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="OFFERED"
android:textColor="#color/white"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/offered_Row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ListView
android:id="#+id/offered_ListView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/gray"
android:padding="5dp"
android:scrollbars="vertical"
android:smoothScrollbar="true" />
</TableRow>
</TableLayout>
</LinearLayout>
mainscreen.java
public class MainMenu extends Activity {
RequestAdapter mRequestAdapter;
ListView mListView_req ;
public class RequestData {
int icon;
String list_text;
public RequestData( String mText) {
super();
//this.icon=image;
this.list_text=mText;
}
}
private RequestData[] getListItems(){
Resources mRes = getResources();
String[] sListItems= mRes.getStringArray(R.array.request_list);
int listsize = sListItems.length;
RequestData[] obj = new RequestData[listsize];
int size = obj.length;
for(int i=0; i<size; i++){
obj[i] = new RequestData(sListItems[i]);
}
return obj;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu_screen);
mListView_req= (ListView)findViewById(R.id.request_ListView) ;
mRequestAdapter = new RequestAdapter(this,R.layout.listitem, getListItems());
mListView_req.setAdapter(mRequestAdapter);
}
class RequestAdapter extends ArrayAdapter{
private RequestData rList[]= null;
Context context;
public RequestAdapter(Context context, int textViewResourceId,
RequestData[] data) {
super(context, textViewResourceId, data);
// TODO Auto-generated constructor stub
this.rList= data;
}
#Override
public RequestData getItem(int position){
return rList[position];
}
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
View row= convertView;
if (row== null){
row = getLayoyutinflater().inflate(R.layout.listitem,null);
}
else {
RequestData items = getItem(pos);
TextView text= (TextView) row.findViewById(R.id.List_Text);
if(text!= null){
text.setText(items.list_text);
}
}
return row;
}
listitem.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="150dp">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:id="#+id/text"/>
<ImageView>
android:layout_width="40dp"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_marginRight="10dp"/>
</LinearLayout>

Fix the image in the background of list activity

Hello I have called a image behind the listview and listview is transparent. Each record in the list is appearing on repeating image . how i can fix this repeat ion of image . Here is the 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"
android:padding="7dp"
android:background="#drawable/ac"
android:cacheColorHint="#00000000"
>
<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_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="18dp" />
</LinearLayout>
Thanks
Now i have updated xml files like this
main 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"
android:background="#drawable/ac"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView android:id="#+id/listView"
android:background="#drawable/ac"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
List 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"
android:cacheColorHint="#00000000"
>
<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_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="18dp" />
</LinearLayout>
and in the activity calling in this way
{ R.id.item_title, R.id.item_subtitle }
the whole code looks like
public class Test extends ListActivity {
Prefs myprefs = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
this.myprefs = new Prefs(getApplicationContext());
// install handler for processing gui update messages
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("...............");
try{
JSONArray earthquakes = json.getJSONArray("services");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", e.getString("taskid"));
map.put("pic", "Service name : " + e.getString("employeepic"));
map.put("serviceinfo", "" + e.getString("employeename")+ " : "+ e.getString("starttime")
+" To " + e.getString("endtime"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.test,
new String[] { "servicename", "serviceinfo" },
new int[] { R.id.item_title, R.id.item_subtitle });
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(Test.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
I suppose you are using the above XML to populate the items in the list view (i.e) inside getView() ...
1. Remove android:background="#drawable/ac" from the above XML.
2. Add it to the ListView's XML say,
<ListView android:id="#+id/listView"
android:background="#drawable/ac"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Explanation:
Setting Background to the layout that you are using to populate the list will produce 'repeating image' because you are setting background to the child (i.e) the listView's item and not to the List.
EDIT:
1.For your Activity, you will be loading the ListView from an Layout File(say main.xml), inside that layout file for the ListView add the background.
2. Remove the background from the LinearLayout in your above code.
Edit Again:
use the following XML,
main.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="#drawable/ac"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView android:id="#+id/listView"
android:background="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
List 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"
android:cacheColorHint="#00000000"
android:background="#00000000"
>
<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_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="18dp" />
</LinearLayout>

Categories

Resources