How to set margins to autocompletetextview adapter - android

I have and autocompletetextview set inside a toolbar. when user provides search query i have an adapter(with some dummy data added for testing) that shows the results based on search query.
The problem is with adapter width as it is set to autocompletetextview width.
But here i want the adapter width to set to toolbar width and a margin between toolbar and adapter.
This is the code i have written:
searchView = (AutoCompleteTextView) findViewById(R.id.searchView);
SearchAdapter searchAdapter = new SearchAdapter(NavigationDrawerActivity.this,searchResults,searchView);
searchView.setAdapter(searchAdapter);
Here is my adapter:
public class SearchAdapter extends ArrayAdapter<SearchResult> {
private boolean mAnimate;
private AutoCompleteTextView mSearch;
public SearchAdapter(Context context, List<SearchResult> options, AutoCompleteTextView search) {
super(context, 0, options);
mSearch = search;
}
int count = 0;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SearchResult option = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.search_option, parent, false);
/* if (true) {
Animation anim = AnimationUtils.loadAnimation(getContext(),
R.anim.anim_down);
anim.setDuration(400);
convertView.startAnimation(anim);
if (count == this.getCount()) {
mAnimate = false;
}
count++;
}*/
}
View border = convertView.findViewById(R.id.border);
if (position == 0) {
border.setVisibility(View.VISIBLE);
} else {
border.setVisibility(View.GONE);
}
final TextView title = (TextView) convertView
.findViewById(R.id.title);
title.setText(option.title);
ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
icon.setImageDrawable(option.icon);
ImageView up = (ImageView) convertView.findViewById(R.id.up);
up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mSearch.setText(title.getText().toString());
mSearch.setSelection(mSearch.getText().length());
}
});
return convertView;
}
}
Here is my adapter row layout:
<?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="48dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="#android:color/white"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:paddingLeft="3dp"
android:adjustViewBounds="true"
android:cropToPadding="true" />
<TextView
android:id="#+id/title"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#212121"
android:layout_centerVertical="true"
android:layout_marginLeft="62dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/up"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/up"
android:layout_alignParentRight="true"
android:layout_width="32dp"
android:layout_margin="10dp"
android:layout_marginRight="24dp"
android:layout_gravity="center|right"
android:layout_height="32dp"
android:src="#drawable/ic_up" />
<View
android:id="#+id/border"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#E8E8E8" />
</RelativeLayout>
With the above code i am getting like this:
Need the adapter aligned to toolbar width
I have tried setting adapter width like this:
Rect screenSize = new Rect();
getWindowManager().getDefaultDisplay().getRectSize(screenSize);
// screen width
int screenWidth = screenSize.width();
int adapterMargin = (int)getResources().getDimension(R.dimen.adapter_margin);
searchView.setDropDownWidth(screenWidth - adapterMargin);
But the problem here is i'm getting not getting right margin as shown below:
screen with adapter width set
Please help me on how to tackle this problem.

It looks like you want to set adapter width to be set to toolbar width add the following line to your code it will work:
searchView.setDropDownAnchor(toolbar.getId());
Comment below if you have any further doubts

Modify this line
searchView.setDropDownWidth(screenWidth - adapterMargin);
to the following, may be this will change.
searchView.setDropDownWidth(screenWidth - adapterMargin * 2);

Related

How to add a white line below every ListView Item in xml?

My Problem is --> I put a white line as a view in the Fragment item xml but it doesn't appear when i debug it. How can i get the changes i make in the fragment item xml file. Because i tried some little changes like alpha property change but it didnt change is it because of this is a listview and has itself design? Please help me about that problem.
The xml code and adapter code are below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="5dp"
>
<ImageView
android:id="#+id/menulogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/infoicon"
android:layout_gravity="center"
android:paddingTop="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/txtMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Company Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1" />
</LinearLayout>
<View
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="#+id/txtMenu"
android:background="#FFFFFF"
/>
</LinearLayout>
My listview adapter codes right down below.
public class MenuListAdapter extends ArrayAdapter<Menu_Item> {
private final ArrayList<Menu_Item> list;
private final Activity context;
private int[] colors = new int[]{0x34a4a4a4, 0x34fafafa};
public MenuListAdapter(Activity context, ArrayList<Menu_Item> list) {
super(context, R.layout.menu_item, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
public ImageView menulogo;
public TextView txtMenu;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.menu_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.menulogo = (ImageView) view.findViewById(R.id.menulogo);
viewHolder.txtMenu = (TextView) view.findViewById(R.id.txtMenu);
view.setTag(viewHolder);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtMenu.setText(list.get(position).getMenuName());
//holder.menulogo.setImageBitmap(list.get(position).getMenuImage());
String menuName=list.get(position).getMenuName();
if (menuName.equals("Info")){
holder.menulogo.setImageResource(R.drawable.infoicon);
}
if (menuName.equals("Odalar")){
holder.menulogo.setImageResource(R.drawable.accomodation);
}
if (menuName.equals("Galeri")){
holder.menulogo.setImageResource(R.drawable.galeryicon);
}
if (menuName.equals("Aktiviteler")){
holder.menulogo.setImageResource(R.drawable.activitiesicon);
}
if (menuName.equals("Robin's Kids Club")){
holder.menulogo.setImageResource(R.drawable.kidsclub);
}
if (menuName.equals("Restaurants")){
holder.menulogo.setImageResource(R.drawable.restaurantsicon);
}
view.setId(list.get(position).getMenu_id());
return view;
}
#Override
public int getViewTypeCount() {
if (getCount() != 0)
return getCount();
return 1;
}
}
I found an answer to my question and wanted to share it with you.
I only used these 2 properties below of ListView
android:divider="#FFFFFF"
android:dividerHeight="1dip"
Like this...
<ListView
android:id="#+id/menuList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:divider="#FFFFFF"
android:dividerHeight="1dip"
/>
In ListView XML add these two lines
<ListView
android:divider="#android:color/white"
android:dividerHeight="5dip"
.
.
</ListView>
Try to change the View with id line for a Space component. It is an extension of View, and works better for what you want.
Here is a description of the widget:
https://developer.android.com/reference/android/widget/Space.html

ImageView's size can't adapt with image from net in GridView

I have a question , it make me tired whole day.
Why ImageView's background is show when load net picture into ImageView in GridView? ImageView's size can't wrap_content.
How could I fix it? Make ImageView's size adapt picture size (don't change the piacture size)
thanks advance.
A .Main XML
<ListView
android:id="#+id/gridview_listview"
android:layout_width="100dp"
android:background="#00000000"
android:scrollbars="none"
android:dividerHeight="5dp"
android:divider="#color/colorAlpha"
android:listSelector="#drawable/list_item_selected"
android:layout_height="match_parent" />
<GridView
android:id="#+id/gridview"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_width="0dp"
android:scrollbars="none"
android:listSelector="#drawable/gridview_item_selected"
android:layout_height="wrap_content">
</GridView>
B.GridView Item XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="1dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_gridview"
android:padding="0dp"
android:background="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv_gridview"
android:text="test"
android:textColor="#00ffff"
android:textSize="15sp"
android:gravity="center"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
C.GridView adapter extends BaseAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder gridViewHolder = new GridViewHolder();
ImageView imageView;
TextView textView;
if (convertView == null ) {
convertView = layoutInflater.inflate(R.layout.gridview_item , null) ;
imageView = (ImageView) convertView.findViewById(R.id.iv_gridview);
textView = (TextView) convertView.findViewById(R.id.tv_gridview);
gridViewHolder.setImageView(imageView);
gridViewHolder.setTextView(textView);
convertView.setTag(gridViewHolder);
}else {
gridViewHolder = (GridViewHolder) convertView.getTag();
}
Picasso.with(context).load("http://img1.cache.netease.com/catchpic/A/A8/A86B03AE11945B98D0AE0A98480D28CE.jpg").into(gridViewHolder.getImageView());
gridViewHolder.getTextView().setText(dataList.get(position).getName());
return convertView;
}
D.Activity code
dataList = GetData.getData();
GridViewAdapter gridViewAdapter = new GridViewAdapter(GridView.this , dataList);
gridView.setAdapter(gridViewAdapter);
gridView.setNumColumns(3);
gridView.setHorizontalSpacing(5);
gridView.setVerticalSpacing(10);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url = "this is " + position ;
startActivity(new Intent(GridView.this , ClickActivity.class).putExtra("url" ,url));
}
});
Try setting the android:adjustViewBounds attribute on your ImageView to true in your layout XML.
<ImageView
android:id="#+id/iv_gridview"
android:padding="0dp"
android:background="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
From the documentation on android:adjustViewBounds:
Set this to true if you want the ImageView to adjust its bounds to
preserve the aspect ratio of its drawable.

Row in listview not preserve the makeover and appears repeated

I have a list that when you click on the row on the right side the layout with the icon is changed by other. This makes it well but when I scrolling in the list, I notice there are rows with the same icon without do click. Also, if I scroll quickly these same icons are lost and are built differently. I think it's on the reuse of cells but I can not find the solution. What I want is that the image stays active only in the row where clicka.
Thanks!!
Adapter class:
public class ListadoVotantesArrayAdapter extends ArrayAdapter<Votante> {
private Context context;
private LayoutInflater inflater;
private ArrayList<Votante> listaVotantes;
private int rowLayout;
private View mConverView;
public ListadoVotantesArrayAdapter(Context context, int resource, ArrayList<Votante> objects) {
super(context, resource,objects);
this.context = context;
this.inflater = LayoutInflater.from(context);
this.listaVotantes = objects;
this.rowLayout = resource;
}
public int getCount(){
return this.listaVotantes.size();
}
public Votante getVotante(int position){
return this.listaVotantes.get(position);
}
private static class ViewHolder {
public TextView lblName;
public TextView lblLastName;
public TextView lblDni;
public TextView lblVoterNumber;
public RelativeLayout lytVoted;
public RelativeLayout lytCanVote;
public RelativeLayout lytUndo;
}
public View getView(int position, View converView, ViewGroup parent) {
final ViewHolder viewHolder;
mConverView = converView;
if (mConverView == null){
viewHolder = new ViewHolder();
this.mConverView = inflater.inflate(this.rowLayout, parent, false);
if (mConverView != null){
viewHolder.lblName = (TextView) mConverView.findViewById(R.id.lblVoterName);
viewHolder.lblLastName = (TextView) mConverView.findViewById(R.id.lblVoterLastName);
viewHolder.lblDni = (TextView) mConverView.findViewById(R.id.lblDni);
viewHolder.lblVoterNumber = (TextView) mConverView.findViewById(R.id.lblNumber);
viewHolder.lytVoted = (RelativeLayout) mConverView.findViewById(R.id.lytVoted);
viewHolder.lytCanVote = (RelativeLayout) mConverView.findViewById(R.id.lytCanVote);
viewHolder.lytUndo = (RelativeLayout) mConverView.findViewById(R.id.lytUndo);
mConverView.setTag(viewHolder);
}
}else{
viewHolder = (ViewHolder) mConverView.getTag();
}
Votante votante = getVotante(position);
viewHolder.lblName.setText(votante.getNombre());
viewHolder.lblLastName.setText(votante.getApellidos());
viewHolder.lblDni.setText(votante.getDni());
viewHolder.lblVoterNumber.setText(""+votante.getNumVotante());
if (votante.getVotado()){
viewHolder.lytVoted.setVisibility(View.VISIBLE);
viewHolder.lytCanVote.setVisibility(View.GONE);
}else{
viewHolder.lytVoted.setVisibility(View.GONE);
viewHolder.lytCanVote.setVisibility(View.VISIBLE);
}
mConverView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.lytUndo.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
});
return mConverView;
}
}
Layout Row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lyt_voter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp">
<RelativeLayout
android:id="#+id/lytVoterNumber"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:background="#color/lightBlueItemList">
<TextView
android:id="#+id/lblNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="1999"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lytVoterData"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/lytCanVote"
android:layout_toRightOf="#+id/lytVoterNumber"
android:layout_toStartOf="#+id/lytCanVote"
android:background="#color/whiteItemList"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/lblVoterLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Suarez Garcia de la Serna"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblVoterName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="José Carlos"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblDni"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="44950962S"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<RelativeLayout
android:id="#+id/lytCanVote"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:background="#color/yellowItemList"
android:minHeight="30dp"
android:minWidth="30dp">
<ImageView
android:id="#+id/imgVote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/flecha" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/lytVoted"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:background="#color/grrenAcceptList"
android:minHeight="30dp"
android:minWidth="30dp"
android:visibility="gone">
<ImageView
android:id="#+id/imgAccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/aceptar"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="70dp"
android:layout_height="70dp"
android:minHeight="30dp"
android:minWidth="30dp"
android:background="#color/redD3"
android:id="#+id/lytUndo"
android:layout_alignParentRight="true"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgUndo"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#drawable/undo"/>
</RelativeLayout>
</RelativeLayout>
You need to follow below Idea
1) this line of code mConverView = converView; doesn't makes sense. directly use converView(View from getView param). Remove mConvertView from the adapter.
2) Add some mechanism which can tell you which row is clicked and save that variable.
Example:- Have an boolean Array of size Rows.size . and set them in onClick.
Boolean[] array = new Boolean[getSize()];
in onClick that you already have in your getview set this.
public void onClick(View v) {
array[position] = !array[position];
viewHolder.lytUndo.setVisibility(View.VISIBLE);
//You do not need to call notifyDatasetChange.
3) in getView(), when you are setting data in to row items/or just before onClick. have a check like below.
if(array[position])// this will tell you if you need to show or hid lytUndo thing
viewHolder.lytUndo.setVisibility(View.VISIBLE); // Show it
else
viewHolder.lytUndo.setVisibility(View.GONE); // hide it or do whatever you want
You might need to set some params as final
I have written this as simple as possible, comment back with your result.

multiple elements within same drawable

I'm trying to add several items within a single Drawable which would be used in a GridView:
- text aligned at the center of the drawable
- image at the background
- a programmatically variable number (0-3) of images (stars) ALIGNED horizontally at the bottom of the drawable.
How could I accomplish this please?
At the moment I'm trying to use a custom ImageView and set the background image using setImageResource. An alternative would be to use a TextView, however I still have no clue about the stars at the bottom.
Any help would be appreciated!
George.
You don't mean Drawable, you mean to say a View. You simply have to find a decent tutorial about making custom views for AdapterView (ListView, GridView, ...).
One such tutorial to get you started is http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html
Quick dump of the code:
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
grid_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewMainActivity.java
public class CustomGridViewMainActivity extends Activity
{
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
}
GridViewCustomAdapter.java
public class GridViewCustomAdapter extends ArrayAdapter
{
Context context;
public GridViewCustomAdapter(Context context)
{
super(context, 0);
this.context=context;
}
public int getCount()
{
return 24;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);
if(position%2==0)
{
textViewTitle.setText("Facebook");
imageViewIte.setImageResource(R.drawable.facebook);
}
else
{
textViewTitle.setText("Twitter");
imageViewIte.setImageResource(R.drawable.twitter);
}
}
return row;
}
}

Progress bar inside of each list view item is not displayed well

I created a ListView, each list item contains a progress bar. When scrolling this list view all progress bars are displayed and scrolled well, EXCEPT very first item with progress bar, which stays always on top. The visual effect is following here: when scrolling down, all progress bars are scrolled well, but additionally new progress bar is always drawn at the fixed position where the first progress bar has been originally displayed. Does anybody know how to get rid of this?
Layout planner_main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<include layout="#layout/planner_row"/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<include layout="#layout/parent_view"/>
<TextView
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/no_entries"
android:textSize="38sp"
android:visibility="gone"/>
</RelativeLayout>
Layout planner_row.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_layout"
android:layout_width="wrap_content"
android:layout_height="50dp">
<TextView
android:id="#+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:textColor="#color/black"
android:textSize="18sp">
</TextView>
<TextView
android:id="#+id/category_spent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/category_name"
android:layout_centerVertical="true"
android:textColor="#color/black"
android:textSize="18sp" >
</TextView>
<ProgressBar
android:id="#+id/category_bar"
android:layout_width="200dp"
android:layout_height="15dp"
android:layout_below="#id/category_name"
style="#android:style/Widget.ProgressBar.Horizontal"
android:max="100" />
<TextView
android:id="#+id/category_limit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/category_bar"
android:layout_below="#id/category_spent"
android:layout_centerVertical="true"
android:textColor="#color/black"
android:textSize="18sp" >
</TextView>
</RelativeLayout>
Adapter's main part:
public class PlannerListAdapter extends SimpleCursorAdapter {
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final PlannerViewHolder holder;
// create a new row view
if (convertView == null) {
convertView = inflater.inflate(R.layout.planner_row, parent, false);
// find children views
TextView tvName = (TextView) convertView.findViewById(R.id.category_name);
TextView tvLimit = (TextView) convertView.findViewById(R.id.category_limit);
TextView tvSpent = (TextView) convertView.findViewById(R.id.category_spent);
ProgressBar bar = (ProgressBar) convertView.findViewById(R.id.category_bar);
holder = new PlannerViewHolder(tvName, tvLimit, tvSpent, bar);
// tag the row with it's children views
convertView.setTag(holder);
} else { // reuse existing row view
holder = (PlannerViewHolder)convertView.getTag();
}
if (dataCursor.moveToPosition(position)) {
holder.tvCategory.setText(dataCursor.getString(1));
holder.bar.setProgress(50);
}
return convertView;
}
...
public static class PlannerViewHolder {
private ProgressBar bar;
private TextView tvCategory, tvLimit, tvSpent;
public PlannerViewHolder(TextView tv1, TextView tv2, TextView tv3, ProgressBar b) {
tvCategory = tv1;
tvLimit = tv2;
tvSpent = tv3;
bar = b;
}
}
}
Activity's code which uses adapter:
#Override
public void onCreate(Bundle savedInstanceState) {
setLayout(R.layout.planner_main);
super.onCreate(savedInstanceState);
...
listView = (ListView)findViewById(android.R.id.list);
Cursor c = db.getCategoriesData();
adapter = new PlannerListAdapter(context, c, new String[] { Data.Categories.NAME, Data.Categories.LIMIT }, new int[] { R.id.category_name, R.id.category_limit });
listView.setAdapter(adapter);

Categories

Resources