Android - Why ListView inside ListView does not work? - android

My xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="#+id/address" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:layout_below="#+id/address"
android:id="#+id/win_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/win_list"
android:layout_below="#+id/win_title"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/lose_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lose_list"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/cannot_compare_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cannot_compare_list"
android:layout_weight="1" />
</LinearLayout>
And this is my custom Adapter:
public class AnalysisResultAdapter extends ArrayAdapter<ComparisonResult> {
static class ViewHolder {
TextView address;
TextView win_title;
ListView win_list;
TextView lose_title;
ListView lose_list;
TextView cannot_compare_title;
ListView cannot_compare_list;
}
private Hashtable<String, SevenEleven[]> sevenElevenData;
public AnalysisResultAdapter(Context context, int resource, List<ComparisonResult> houses) {
super(context, resource, houses);
}
public void setSevenElevenData(Hashtable<String, SevenEleven[]> sevenElevenData) {
this.sevenElevenData = sevenElevenData;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater viewInflater;
viewInflater = LayoutInflater.from(getContext());
view = viewInflater.inflate(R.layout.analysis_result_list_item, null);
holder = new ViewHolder();
holder.address = (TextView) view.findViewById(R.id.address);
holder.win_title = (TextView) view.findViewById(R.id.win_title);
holder.win_list = (ListView) view.findViewById(R.id.win_list);
holder.lose_title = (TextView) view.findViewById(R.id.lose_title);
holder.lose_list = (ListView) view.findViewById(R.id.lose_list);
holder.cannot_compare_title = (TextView) view.findViewById(R.id.cannot_compare_title);
holder.cannot_compare_list = (ListView) view.findViewById(R.id.cannot_compare_list);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if (this.sevenElevenData.size() == 0) {
return view;
}
ComparisonResult result = getItem(position);
holder.address.setText(result.house.Address);
if (result.win.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.win) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.win_title.setText("win");
holder.win_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
if (result.lose.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.lose) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.lose_title.setText("lose");
holder.lose_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
if (result.cannotBeCompared.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.cannotBeCompared) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.cannot_compare_title.setText("cannotBeCompared");
holder.cannot_compare_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
return view;
}
}
The problem is: Only the #+id/address TextView shows the text successfully, other things are not shown. But I don't know why this happened.
How can I solve this problem ? Can someone help me ?
Thanks.

Use ScrollView intead of outer(parent) ListView.
And use LisView for the inner ListView.
It gives you the scrolling feature.

1/ layout_below only works in RelativeLayout, you are using a LinearLayout with horizontal orientation so all the elements will be rendered side by side, and all elements have the layout_width:"match_parent" so only the first element will be shown.
2/ when you use a ListView in another scrollView the renderer can't calculate the height of the second one

Related

How to change Custom Listview row color change alternatively?

I have created custom LISTVIEW for showing searched result list want to show each row with background color I tried out with some code but not working as per my requirement please help me out
here is my code
public class ListCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResult> searchArrayList;
private LayoutInflater mInflater;
public ListCustomBaseAdapter(Context context,
ArrayList<SearchResult> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.custNm);
holder.txtProsNm = (TextView) convertView.findViewById(R.id.prosNm);
holder.txtFrmPort = (TextView) convertView
.findViewById(R.id.frmPort);
holder.txtToPort = (TextView) convertView.findViewById(R.id.ToPort);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position % 2 == 0) {
holder.txtName.setBackgroundColor(Color.WHITE);
holder.txtProsNm.setBackgroundColor(Color.WHITE);<----- change bgcolor of textview not whole row of View
holder.txtFrmPort.setBackgroundColor(Color.WHITE);
holder.txtToPort.setBackgroundColor(Color.WHITE);
} else {
holder.txtName.setBackgroundColor(Color.DKGRAY);
holder.txtProsNm.setBackgroundColor(Color.DKGRAY);
holder.txtFrmPort.setBackgroundColor(Color.DKGRAY);
holder.txtToPort.setBackgroundColor(Color.DKGRAY);
}
holder.txtName.setText(searchArrayList.get(position).getCustNm());
holder.txtProsNm.setText(searchArrayList.get(position).getProsNm());
holder.txtFrmPort.setText(searchArrayList.get(position).getFrmPort());
holder.txtToPort.setText(searchArrayList.get(position).getToPort());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtFrmPort;
TextView txtToPort;
TextView txtProsNm;
}
}
calling adapter
public void onCreate(Bundle savedInstanceState) throws SQLException {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Connection object for Spinner
con = new DatabaseConnection(this);
try {
// Get Filtermap from Parent Activity
// this Bundle contains HashMap
Bundle wrapper = getIntent().getBundleExtra("salesActList");
salesLstObj = (Map<String, Object>) wrapper.getSerializable("salesActCriteriaList");
salesLst = con.searchSalesActivity(salesLstObj);
ArrayList<SearchResult> searchResults = getSearchResults();
lv = (ListView) findViewById(R.id.srListView);
lv.setAdapter(new ListCustomBaseAdapter(this, searchResults));
});
} catch (SQLException se) {
se.getStackTrace();
String flag = "fail";
dialogBox(flag);
}
here is output img
want to show Blue color area to be in gray alternately how to make it?
Custome row view code
<TableLayout
style="#style/TableLayoutStyle"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TableRow
android:id="#+id/tblRwCust"
style="#style/TableRowStyle" >
<TextView
android:id="#+id/custNm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF7F24"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tblRwPros"
style="#style/TableRowStyle" >
<TextView
android:id="#+id/prosNm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</TableRow>
<TableRow
android:id="#+id/tblRwPort"
style="#style/TableRowStyle" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/frmPort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:textColor="#808080" />
<TextView
android:id="#+id/ToPort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#808080" />
</LinearLayout>
</TableRow>
</TableLayout>
if(position%2 == 0 ){
holder.convertView.setBackgroundColor(Color.CYAN);
}else{
holder.convertView.setBackgroundColor(Color.YELLOW);
}
Here is my solution with Table layout instead Relative layout
<TableLayout
style="#style/TableLayoutStyle"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:id="#+id/myTable"
android:background="#FFFFFF">
in adapter
holder.myTable = (TableLayout) convertView.findViewById(R.id.myTable);
if (position % 2 == 0) {
holder.myTable.setBackgroundColor(Color.WHITE);
} else {
holder.myTable.setBackgroundColor(Color.GRAY);
}

setadapter for android not working. empty adapter or listview?

im just trying to create a listview using a simple adapter. the setadapter is not working. this is my code. my setcontentview is before this block of code and this is in oncreate.
ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = new HashMap<String,String>();
map.put("CType", "Alarm");
map.put("RType", "Once");
map.put("hour", "1");
map.put("minute", "1");
map.put("second", "30");
arraylist.add(map);
String[] stringarray= new String[] {"CType", "RType", "hour","minute","second"};
int[] intarray = new int[] {R.id.clocktextviewalarmtimer,R.id.clocktextviewrepeatonce,
R.id.clocktextviewhours,R.id.clocktextviewminutes,R.id.clocktextviewseconds};
ListView clocklistview = (ListView)findViewById(R.id.clocklistview);
SimpleAdapter adapter = new SimpleAdapter(this,arraylist,R.layout.list_clock,stringarray,intarray);
clocklistview.setAdapter(adapter);
this is the xml that contains my listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/clocklistview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView></LinearLayout>
this is for my list_clock.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="wrap_content"
android:weightSum="3">
<TextView
android:id="#+id/clocktextviewalarmtimer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/clocktextviewrepeatonce"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/clocktextviewhours"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/clocktextviewminutes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/clocktextviewseconds"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Here's an example of a custom adapter. Try this ...
public class ClockListAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> _arrayList;
ClockListAdapter(Context context, ArrayList<HashMap<String, String>> arrayList) {
this._arrayList = arrayList;
}
#Override
public int getCount() {
return (_arrayList != null) ? _arrayList.size() : 0;
}
#Override
public Object getItem(int position) {
return (_arrayList != null) ? _arrayList.get(position) : null;
}
#Override
public long getItemId(int position) {
return (_arrayList != null) ? _arrayList.indexOf(_arrayList.get(position)) : 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
// REPLACE WITH YOUR LAYOUT FILE vvvvvvvvvv
convertView = layoutInflater.inflate(android.R.layout.simple_list_item_1, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder._textView.setText("set some text here");
return convertView;
}
public class ViewHolder {
// ADD YOUR VIEW(S) vvvvvvvvv
TextView _textView;
ViewHolder(View v) {
// REPLACE WITH YOUR TEXT vvvvvvvvv
_textView = (TextView) v.findViewById(R.id.textView1);
}
}
}

RelativeLayout Buttons on top and custom listview below buttons

I've searched but not found any examples for what I want to do. I'm sure its trivial but I'm stumped. I have a custom listview adapter and a defined relative layout. The entire layout is being called for each item, I want my buttons to only appear once on top. Any advice would be greatly appreciated.
This is how it looks now:
http://img440.imageshack.us/img440/2803/device20120104173817.png
This is how I want it to look:
http://img215.imageshack.us/img215/805/devicewant.png
<?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="horizontal" >
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/relativeLayout2">
<TextView
android:id="#+id/postinfo"
android:layout_below="#+id/FirstPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<TextView
android:id="#+id/dateinfo"
android:layout_alignRight="#+id/postinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<TextView
android:id="#+id/post"
android:layout_below="#+id/postinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<ImageView
android:id="#+id/icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/post"
android:contentDescription="OCForums thread icon"
android:focusable="false"
/>
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top" >
<Button
android:id="#+id/LastPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/last" />
<Button
android:id="#+id/NextPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/LastPage"
android:text="#string/next" />
<Button
android:id="#+id/FirstPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/first" />
<Button
android:id="#+id/PreviousPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/FirstPage"
android:text="#string/previous" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
static class MyCustomAdapter2 extends BaseAdapter {
private LayoutInflater mInflater;
public MyCustomAdapter2(Context context, int textViewResourceId,List<List<String>> objects) {
super();
mInflater = LayoutInflater.from(context);
rid = textViewResourceId;
fcontext = context;
usernames = objects.get(0);
usernamecolors = objects.get(1);
dates = objects.get(2);
messages = objects.get(3);
Log.i("is it empy?(messages)",Integer.toString(messages.size()));
Log.i("is it empy?(dates)",Integer.toString(dates.size()));
Log.i("is it empy?(usernamecolors)",Integer.toString(usernamecolors.size()));
Log.i("is it empy?(usernames)",Integer.toString(usernames.size()));
}
public int getCount() {
return lists.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.postinfo);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(usernames.get(position));
holder.text.setTextColor(Color.parseColor(usernamecolors.get(position)));
ViewHolder holderd;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holderd = new ViewHolder();
holderd.text = (TextView) convertView.findViewById(R.id.dateinfo);
convertView.setTag(holderd);
} else {
holderd = (ViewHolder) convertView.getTag();
}
holderd.text.setText(dates.get(position));
ViewHolder holderm;
if (convertView == null) {
convertView = mInflater.inflate(rid, null);
holderm = new ViewHolder();
holderm.text = (TextView) convertView.findViewById(R.id.post);
convertView.setTag(holderm);
} else {
holderm = (ViewHolder) convertView.getTag();
}
holderm.text.setText(messages.get(position));
ImageView icon=(ImageView)convertView.findViewById(R.id.icon2);
if (newlist.size() > 0 && lists.get(position).matches(newlist.get(position))){
icon.setImageResource(R.drawable.forum_new);
}
else{
icon.setImageResource(R.drawable.forum_old);
}
return convertView;
}
static class ViewHolder {
TextView text;
}
}
This is where I am calling it all.
ListView listview = (ListView) findViewById(R.id.listView3);
listview.setAdapter(new CustomListView.MyCustomAdapter2(ThreadActivity.this, R.layout.row2, output));
You need to define the header as a separate layout (separate xml file) then instantiate it and add it to the listview as a header -- then this layout will show as the first item in the list.
See this question: Android ListView with complex header

Pagination for ListView in android

I have to apply pagination concept on ListView my list view contains data parsed from web service. below is code given that how I have displayed data in list view as below.
try {
ArrayList<HashMap<String, String>> arl (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
System.out.println("...serialized data.."+arl);
lv1 = (ListView) findViewById(R.id.lstlodgingresult);
adapter = new SimpleAdapter(this, arl, R.layout.custom_row_view,
new String[] { "Srno", "Names", "URL", "Address1", "Address2", "Telephone", "Category", "PetH",
"PetInfo" }, new int[] { R.id.txtSrno,R.id.txtname, R.id.txturl, R.id.txtaddress1, R.id.txtaddress2, R.id.txtphone, R.id.txtcategory,
R.id.txtpetpolicyH, R.id.txtpetpolicyC }
);
lv1.setScrollbarFadingEnabled(false);
lv1.refreshDrawableState();
lv1.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
you just need to add a Footer View in the Listyou created. Then for the footer view (might be button/image/text) set a ClickListener for that and in Listener add the items into your list and again refresh the activity. I am adding a little tutorial that will help you in this.
I used the following Method for Pagination:
The List Class:
public class customListView extends Activity implements OnClickListener{
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
Context context;
public EfficientAdapter(Context context) {
this.context = context;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return add_Names.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listcontent, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.txt1);
holder.text2 = (TextView) convertView
.findViewById(R.id.txt2);
holder.text3 = (TextView) convertView
.findViewById(R.id.txt3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(add_Names.get(position).toString());
holder.text2.setText(location.get(position).toString());
holder.text3.setText(details.get(position).toString());
return convertView;
}
static class ViewHolder {
TextView text;
TextView text2;
TextView text3;
}
}//end of efficient Adapter Class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
adapter = new EfficientAdapter(this);
l1 = (ListView) findViewById(R.id.ListView01);
View footerView =
((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_footer, null, false);
l1.addFooterView(footerView);
l1.setAdapter(adapter);
mLayout = (LinearLayout) footerView.findViewById(R.id.footer_layout);
more = (Button) footerView.findViewById(R.id.moreButton);
more.setOnClickListener(this);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(), "You clciked "+add_Names.get(arg2).toString(), Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.moreButton:
//Your code to add some more data into list and then call the following to refresh your lits
adapter.notifyDataSetChanged();
break;
}//end of switch
}//end of onClick
}//end of Custom List view class
layout_footerview.xml:(you can add whatever you link in the footer for the list. I used button you can use Text or image or whatever you want)
<?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="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<Button
android:text="Get more.."
android:id="#+id/moreButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold">
</Button>
</LinearLayout>
</LinearLayout>
listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>
</RelativeLayout>
list-content.xml:(modify as u like to be your list row)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/image1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/icon"></ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt1" android:layout_toRightOf="#+id/image1"
android:text="Test Description" android:textSize="15dip" android:textStyle="bold">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt2" android:layout_below="#+id/txt1" android:layout_toRightOf="#+id/image1"
android:text="Address" android:textSize="10dip"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt3" android:layout_below="#+id/txt2" android:layout_toRightOf="#+id/image1"
android:text="Details" android:textSize="10dip" ></TextView>
</RelativeLayout>
I Hop this will definetly help you.!
Mark this as true and UpVote; if this helps you.
Thanks
sHaH..

Android - empty list in Custom List view

Here is my custom List view layout.
I've added an "empty list" text view with id "android:empty", but the 'empty text' view is not displayed when the list is empty.
Any advice please.
UPDATE - updated the layout
<?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="?android:attr/listPreferredItemHeight"
android:paddingLeft="5dip" android:paddingRight="5dip"
android:paddingTop="15dip" android:paddingBottom="15dip"
android:cacheColorHint="#color/match_bg2" android:background="#color/match_bg">
<ImageView android:id="#+id/flag_left" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/flag"
android:src="#drawable/flag_default" />
<LinearLayout android:orientation="vertical"
android:padding="10dip" android:layout_width="0dip" android:gravity="center"
android:layout_weight="1" android:layout_height="fill_parent">
<TextView android:id="#+id/item_match_label"
android:layout_width="wrap_content" android:layout_height="0dip"
android:layout_weight="1" android:gravity="center"
android:textColor="#color/match_fg" android:textStyle="bold"
android:singleLine="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
/>
</LinearLayout>
<ImageView android:id="#+id/flag_right" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/flag"
android:src="#drawable/flag_default" />
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
Here is my Listview Activity
public class LatestMatchesListActivity extends ListActivity {
...
private void createMatchList() {
/*
Log.i(MY_DEBUG_TAG, "Checking Match store size before passing it to custom adapter: "+mStore.size());
if(mStore.size() == 0){
Log.i(MY_DEBUG_TAG, "Got Empty match store, so checking connectivity..");
Match m = new Match();
if(isOnline()){
Log.i(MY_DEBUG_TAG, "Internet is accessible, so adding no matches object: ");
m.setId(-1);
} else{
Log.i(MY_DEBUG_TAG, "No Internet accessible, so adding mo caonnectivity match object: ");
m.setId(-2);
}
mStore.add(m);
} else {
Log.e(MY_DEBUG_TAG, "Match store is not empty ");
}
*/
//mStore.clear();
Log.i(MY_DEBUG_TAG, "Passing match list to custom adapter: "+mStore.toString());
this.mAdapter = new MatchAdapter(this, R.layout.list_matches, mStore);
this.setListAdapter(this.mAdapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setBackgroundDrawable(null);
lv.setBackgroundResource(0);
}
and the custom ArrayAdapter
public class MatchAdapter extends ArrayAdapter<Match> {
private ArrayList<Match> items;
private final String MY_DEBUG_TAG = "MatchAdapter";
public MatchAdapter(Context context, int textViewResourceId, ArrayList<Match> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_matches, null);
}
Match m = items.get(position);
if (m != null) {
TextView itemLabel = (TextView) v.findViewById(R.id.item_match_label);
ImageView imageFlagLeft = (ImageView) v.findViewById(R.id.flag_left);
ImageView imageFlagRight = (ImageView) v.findViewById(R.id.flag_right);
if(m.getId() == -1){
itemLabel.setText("No Live Matches.");
imageFlagLeft.setVisibility(View.INVISIBLE);
imageFlagRight.setVisibility(View.INVISIBLE);
} else if(m.getId() == -2){
itemLabel.setText("No Intenet connectivity.");
imageFlagLeft.setVisibility(View.INVISIBLE);
imageFlagRight.setVisibility(View.INVISIBLE);
} else {
itemLabel.setText(m.getTeam1() + " v/s " + m.getTeam2());
imageFlagLeft.setImageResource(getFlagResource(m.getTeam1()));
imageFlagRight.setImageResource(getFlagResource(m.getTeam2()));
}
}
return v;
}
private int getFlagResource(String teamName) {
Log.i(MY_DEBUG_TAG, "Getting the flag of " + teamName);
String flagString = "flag_" + teamName.replace(" ", "_").toLowerCase();
int resId = getContext().getResources().getIdentifier(flagString, "drawable", "com.itflux.DroidChirp");
if (resId != 0) {
return resId;
}
return R.drawable.flag_default;
}
}
You have your ListView fit the screen, and in LinearLayout your TextView is displayed under that ListView so it's not visible (it would be, if you'd use a ScrollView).
You need to use as the root element of your layout a RelativeLayout for instance, to have the TextView on top of the ListView.
Seems like the question is still unsolved. So here is how I solved it. I triggered a function which will basically refresh the list layout whenever the data changes as below:
ListView lv = getListView();
lv.requestLayout();
if(aa.arrayValue.size() > 0) {
lv.setVisibility(ListView.VISIBLE);
setListAdapter(new Adaptor(this, R.layout.main_list_item, aa.arrayValue));
}
else {
lv.setVisibility(ListView.INVISIBLE);
TextView tv = (TextView) findViewById(android.R.id.empty);
tv.requestLayout();
tv.setVisibility(TextView.VISIBLE);
}
Hope this helps you out!
Use #+id in empty and #id in list : android:id="#+id/android:empty"
Your problem is with the ListView height property. Set it to wrap_content.

Categories

Resources