I am trying to set the checked state of my checkbox in listview, here's the code:
mShalatAdapter = new ArrayAdapter<String>(getActivity(),R.layout.pengaturan_list_item, R.id.label_shalat, waktuShalat);
mListShalat = (ListView) v.findViewById(R.id.pengaturan_checkbox);
mListShalat.setAdapter(mShalatAdapter);
CheckBox cb;
TextView txv;
for (int i = 0; i < mListShalat.getCount(); i++) {
View vListSortOrder = mListShalat.getAdapter().getView(i, null, null);
txv = (TextView) vListSortOrder.findViewById(R.id.label_shalat);
cb = (CheckBox) vListSortOrder.findViewById(R.id.status_shalat);
String labelShalat = txv.getText().toString();
Toast.makeText(getActivity(), labelShalat, Toast.LENGTH_SHORT).show();
if (cb.isChecked() == false) {
cb.setChecked(true);
}
}
And this is my pengaturan_list_item.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/label_shalat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="Label Ibadah"
android:textColor="#android:color/white"
android:textSize="20dp" />
<CheckBox
android:id="#+id/status_shalat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/ibadah_checkbox_list"
android:visibility="visible" />
</LinearLayout>
I got the value for TextView and CheckBox but I don't know why the CheckBox not showing the true state in device. Please help, thanks in advance.
my ibadah_checkbox_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true"
android:drawable="#drawable/checkbox_ibadah_checked" />
<item android:state_checked="false"
android:drawable="#drawable/checkbox_ibadah_unchecked" />
</selector>
It might be worth altering the way your adapter works.
Instead of creating a stardard ArrayAdapter, try inheriting from it instead, then inflating views from the list_item.xml inside the getView() method.
Once there, you can extract the checkbox from the view like you're doing in the loop & set its value according to what you need. This is the standard way of doing things like this.
My guess for why what you're doing is not working is that the ArrayAdapter is creating the views when it needs to display them, rather than in this loop you're using, so while it generates and gives you the views while you're requesting them, these views won't necessarily be the ones that are displayed.
Here's how I would do it:
public class MyArrayAdapter extends ArrayAdapter<String>
{
public MyArrayAdapter(Context context, int textViewResourceId, List<String> objects)
{
super(context, textViewResourceId, objects);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if(v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.pengaturan_list_item, null);
}
TextView tv = (TextView)v.findViewById(R.id.label_shalat);
CheckBox cb = (CheckBox)v.findViewById(R.id.status_shalat);
tv.setText(getItem(position));
if (!cb.isChecked())
cb.setChecked(true);
return v;
}
}
Then, set your adapter like this (objectList is your list of Strings for the labels):
mShalatAdapter = new MyArrayAdapter(this, R.layout.pengaturan_list_item, objectList);
mListShalat = (ListView) v.findViewById(R.id.pengaturan_checkbox);
mListShalat.setAdapter(mShalatAdapter);
Hope this helps :)
I think the problem is that in your layout. Your TextView has a layout_width of match_parent, and in that it covers the CheckBox and fills the whole width layout. So you need to divide the layout properly.
Try the layout below and tell me the effect.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:weightSum="2"
android:paddingTop="5dp" >
<TextView
android:id="#+id/label_shalat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="Label Ibadah"
android:textColor="#android:color/white"
android:background="#android:color/black"
android:textSize="20dp" />
<CheckBox
android:id="#+id/status_shalat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#drawable/ibadah_checkbox_list"
android:visibility="visible" />
</LinearLayout>
Add android:descendantFocusability="blocksDescendants" in your ListView.Hope it works....
Related
I'm trying to create an adapter for a listview, which contains two elements in a same row.
The LayoutFile got two linear LinearLayouts Layout Image with two images and name
The adapter only fill the first LinearLayout data, and don't show the second LinearLayout.
Someone could help me? Thanks in advance !
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="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageViewItem"
android:layout_width="match_parent"
android:layout_height="175dp"
android:src="#drawable/icon" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left">
<TextView
android:id="#+id/textViewTitleItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Title"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right">
<TextView
android:id="#+id/textViewPriceItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Price"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
It is only a View, I want to duplicate it in the same row.
Below the adapter:
public ItemAdapter(Activity activity, int layoutResourceId)
{
this.activity = activity;
this.layoutResourceId = layoutResourceId;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var row = convertView;
var currentItem = this[position];
TextView txtViewTitle;
TextView txtViewPrice;
ImageView imgViewItem;
if (row == null)
{
var inflater = activity.LayoutInflater;
row = inflater.Inflate(layoutResourceId, parent, false);
txtViewTitle = row.FindViewById<TextView>(Resource.Id.textViewTitleItem);
txtViewPrice = row.FindViewById<TextView>(Resource.Id.textViewPriceItem);
imgViewItem = row.FindViewById<ImageView>(Resource.Id.imageViewItem);
} else
txtViewTitle = row.FindViewById<TextView>(Resource.Id.textViewTitleItem);
txtViewPrice = row.FindViewById<TextView>(Resource.Id.textViewPriceItem);
imgViewItem = row.FindViewById<ImageView>(Resource.Id.imageViewItem);
txtViewTitle.Text = currentItem.Title;
txtViewPrice.Text = Convert.ToString(currentItem.Price);
return row;
}
It seems that you didn't provide orientation to your parent LinearLayout. By default it is horizontal.
That's why its showing only top layout content.
You need to set it as vertical to show the entire content in your list.
android:orientation="vertical"
I search a lot for an answer and didn't find one that suit me.
I found out that when I set my layout_heigh to 500dp -> all the item shown, but when it has normal size (layout_heigh=wrap_content) then only the first item is shown.
this is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add Task"
android:onClick="addTask" />
<ListView android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
</LinearLayout>
xml_item.xml: this is for one item to duplicate in the listView
<?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:descendantFocusability="blocksDescendants">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/itemText"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="15dp"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/doneBtn"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Done" />
</RelativeLayout>
MyListAdapter: a class in the main_activity that extends the ArrayAdapter class so I can put in one row both TextView and a button.
private class MyListAdapter extends ArrayAdapter<String> {
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout= resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainHolder= null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView= inflater.inflate(layout, parent, false);
ViewHolder viewHolder= new ViewHolder();
viewHolder.item= (TextView) convertView.findViewById(R.id.itemText);
viewHolder.btn= (Button) convertView.findViewById(R.id.doneBtn);
viewHolder.btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), itemArray.get(position), Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
}
else {
mainHolder= (ViewHolder) convertView.getTag();
mainHolder.item.setText(getItem(position));
}
return convertView;
}
public class ViewHolder {
TextView item;
Button btn;
}
}
Thanks for all! :)
but when it has normal size (layout_heigh=wrap_content)
It is a very bad idea using a ListView by giving it a height=wrap_content.
This forces ListView to measure a few children out of the adapter at layout time, to know how big it should be.
You can check it your self debugging the getView() method inside the adapter.
In your case, using a LinearLayout just add a top margin (50dp as the Button) to your listView and use layout_heigh=match_parent.
Otherwise use a RelativeLayout.
Change your listview to take up as much room as is can considering the height of any sibling views (in this case your button). Here's how to do that with the android:layout_weight attribute
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Add Task"
android:onClick="addTask" />
<ListView android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
Working on custom spinner for the first time. I have a spinner with background using nine patch image. Spinnermode is in dialog mode. Now I set custom layout for spinner and spinner item. Run the code but I did not the desired solution. There are corners seeing on the spinner item's background even I have set the background of spinner item . Searched alot but did not find the exact solution. I used code from this site sourcecode form this site their result is perfect but when I tried there whole code did not get the desired result seeing white background in the spinner items.
main.xml
<Spinner
android:id="#+id/spinner1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="#+id/scrollView"
android:layout_marginLeft="20dp"
android:background="#drawable/dropdown_button"
android:paddingLeft="2dp"
android:paddingRight="13dp"
android:paddingTop="5dp"
android:spinnerMode="dialog" />
</RelativeLayout>
spinner_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinneritem"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:paddingLeft="10dp" />
spinner_item_list
<?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:background="#drawable/blank_buttons"
android:orientation="horizontal" >
<TextView
android:id="#+id/spinneritemlist"
style="#style/SpinnerText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:textColor="#android:color/black" />
</LinearLayout>
**in onCreate()**
ArrayList<CountryInfo> myCountries;
myCountries = populateList();spinner1 = (Spinner) findViewById(R.id.spinner1);
CountryAdapter myAdapter = new CountryAdapter(this, R.layout.spinner_item, myCountries);
spinner1.setAdapter(myAdapter);
function and adapter
public ArrayList<CountryInfo> populateList()
{
ArrayList<CountryInfo> myCountries = new ArrayList<CountryInfo>();
myCountries.add(new CountryInfo("USA")); // Image stored in /drawable
myCountries.add(new CountryInfo("Sweden"));
myCountries.add(new CountryInfo("Canada"));
return myCountries;
}
public class CountryAdapter extends ArrayAdapter<CountryInfo>
{
private Activity context;
ArrayList<CountryInfo> data = null;
public CountryAdapter(Activity context, int resource, ArrayList<CountryInfo> data)
{
super(context, resource, data);
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{ // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
return super.getView(position, convertView, parent);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // This view starts when we click the spinner.
View row = convertView;
if(row == null)
{
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.spinner_item_list, parent, false);
}
CountryInfo item = data.get(position);
if(item != null)
{ // Parse the data from each object and set it.
TextView myCountry = (TextView) row.findViewById(R.id.spinneritemlist);
if(myCountry != null)
myCountry.setText(item.getCountryName());
}
return row;
}
}
My resulting images:
printscreen of perfect result of sample code on their site(not from running their code)
screenshot of non perfect result of sample code(from running their code in my app)
try this
<Spinner
android:id="#+id/spinner1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="#+id/scrollView"
android:layout_marginLeft="20dp"
android:background="#drawable/dropdown_button"
android:paddingLeft="2dp"
android:paddingRight="13dp"
android:paddingTop="5dp"
android:spinnerMode="dialog"
android:popupBackground="#android:color/transparent" />//or desired color or drawable
also try this in spinner_list_item
<?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:background="#android:color/transparent"
android:orientation="horizontal" >
<TextView
android:id="#+id/spinneritemlist"
style="#style/SpinnerText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:background="#drawable/blank_buttons"
android:textColor="#android:color/black" />
</LinearLayout>
I have a custom list view which contains two text views and on button.
I have customized the button style.
In the list view each row is not fit to show my button properly.
I want to change the list item's height.
How can i do that..?
This is my list view.
<ListView android:layout_width="fill_parent"
android:scrollbars="none" android:footerDividersEnabled="false"
android:minHeight="50dp"
android:listSelector="#00000000" android:dividerHeight="0px"
android:divider="#00000000" android:layout_height="fill_parent"
android:layout_marginLeft="5dp" android:layout_marginTop="5dp"
android:layout_marginRight="5dp" android:id="#+id/list_view"
android:cacheColorHint="#00000000" android:background="#00000000" />
my custom view xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#808080" android:text="Subtitle 2" android:id="#+id/text1"
android:layout_marginLeft="5dp"
android:singleLine="true"/>
<TextView android:layout_width="60dp"
android:layout_height="wrap_content"
android:textColor="#808080" android:text="$ 00.00" android:id="#+id/text2"
android:singleLine="true"/>
<Button android:layout_width="80dp" android:layout_height="25dp"
android:background="#drawable/type10_subbg" android:text="Add"
android:textSize="20dp" android:textStyle="bold" android:textColor="#153E7E"
android:id="#+id/add" />
</LinearLayout>
Here is my getView.
#SuppressWarnings("unchecked")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
pos = position;
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.type10_sub, null);
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
holder.add = (Button) convertView.findViewById(R.id.add);
holder.add.setTag(String.valueOf(position));
holder.add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button)v;
int tag = Integer.valueOf((String)b.getTag());
System.out.println("Added at "+tag);
System.out.println("Data = "+((HashMap<String,Object>)list.get(tag)).get("text1").toString());
System.out.println("Price = "+((HashMap<String,Object>)list.get(tag)).get("text2").toString()); }
}); holder.text1.setText(((HashMap<String,Object>)list.get(position)).get("text1").toString());
holder.text2.setText(((HashMap<String,Object>)list.get(position)).get("text2").toString());
convertView.setTag(holder);
return convertView;
}
Thanks in advance....!
convertedview.setlayoutparams(new Listview.setlayoutparams(width, height));
First put LinearLayout instead of ListView
and then edit that to ListView otherwise it will not work. This is the trick.
set minHeight in R.layout.type10_sub elements
Its very easy to hardcode in a new height for the custom view simply specify the layout_height for the highest level item, in this case the LinearLayout. Below I've shown how to set it to 100dp
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="100dp">
....
</LinearLayout>
In linear layout give android:layout_height="100dp" instead of wrap_content
use:
if(convertView == null){
convertView = inflater.inflate(R.layout.type10_sub, parent);
}
im trying to display 3 TextViews as a custom row design in a ListView. Unfortunatly, only 2 TextViews ( row_title and row_postcode_city) are visible...
Maybe somebody has an idea?
Regards,
float
Code of my ArrayAdapter:
private class PartnerAdapter extends ArrayAdapter<Partner> {
private ArrayList<Partner> items;
public PartnerAdapter(Context context, int textViewResourceId, ArrayList<Partner> 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)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.partner_suche_ergebnis_row, parent, false);
}
Partner o = items.get(position);
if (o != null) {
TextView title = (TextView) v.findViewById(R.id.row_title);
TextView subtitle = (TextView) v.findViewById(R.id.row_subtitle);
TextView postcode_city = (TextView) v.findViewById(R.id.row_postcode_city);
if (title != null) {
title.setText(o.getTitle());
}
if(subtitle != null){
subtitle.setText(o.getSubtitle());
}
if(postcode_city != null){
subtitle.setText(o.getPostcode() + " " + o.getCity());
}
}
return v;
}
}
XML of the custom row design:
<?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:padding="0dip"><!--
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
--><LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Try to use the following xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/row_title"
android:text="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:text="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="3"
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Check it now.
Change your LinearLayout to "wrap_content" on layout_height
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
Also, I don't know why you have a LinearLayout inside a LinearLayout. Since this xml is only for individual rows. Each row will be a horizontal LinearLayout, the second one being unnecessary as ListView automatically lists rows vertically.
Finally, the root LinearLayout should use minHeight for the default Item Height, not layout_height.
android:minHeight="?android:attr/listPreferredItemHeight"
This should be your xml file (unless you really need the second LinearLayout for some odd reason)
<?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:minHeight="?android:attr/listPreferredItemHeight"
android:padding="0dip">
<TextView
android:id="#+id/row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="bold"/>
<TextView
android:id="#+id/row_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/row_postcode_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Sorry for offtopic, but I here some useful tips from me
You have a LOT of needless (and flat out wrong) stuff going on here:
Partner o = items.get(position);
if (o != null) {
You should get something for every position, if item retuns null that means you are doing something wrong. In your case, if you get null you just return reused old, or brand new row, which will confuse the user by displaying the old data, or no data at all.
if (title != null) {
You have 2 ways of getting View object of your row, by inflating, or getting it from recycler (convertView). And recycler will ALLWAYS return the right type of View, so those check are useless since your components will always be found.