Gridview Taking Limited Values to Display - android

Am having a custom Gridview to display a list of operators with their icon and names of the provider.my problem is the Gridview displaying only Limited providers (12 only).But am having more than 16 values to display.the Remaining Values is getting Repeated in Gridview while Scrolling GridView.
my source code is Here
public class ImageAdapter extends BaseAdapter {
public int getCount() {
return providerlist1.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v;
ImageView imageView;
if (convertView == null) {
LayoutInflater layoutInflater = getLayoutInflater();
v = layoutInflater.inflate(R.layout.icon_with_text, null);
TextView txt = (TextView) v.findViewById(R.id.icon_text);
txt.invalidate();
txt.setTypeface(localTypeface1);
Collections.sort(providerlist1);
Log.v("COLLECTIONS", ""+providerlist1);
txt.setText("" + providerlist1.toArray()[position]);
String string = (String) txt.getText();
icon = string.toLowerCase();
System.out.println(icon);
int resid = getResources().getIdentifier("" + icon, "drawable",
"com.digient.yeldi.app");
imageView = (ImageView) v.findViewById(R.id.icon_image);
imageView.setImageResource(resid);
} else {
v = convertView;
}
return v;
}
}
providerlist1 is an arraylist in which am having more than 16 operators values.

Use Below Code for that, may be it will help you.
public class ImageAdapter extends BaseAdapter {
LayoutInflater layoutInflater;
Context ctx;
public ImageAdapter(Context context) {
// TODO Auto-generated constructor stub
layoutInflater=LayoutInflater.from(context);
this.ctx=context;
}
public int getCount() {
return providerlist1.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v;
ImageView imageView;
if (convertView == null) {
v = layoutInflater.inflate(R.layout.icon_with_text, null);
} else {
v = convertView;
}
TextView txt = (TextView) v.findViewById(R.id.icon_text);
txt.invalidate();
txt.setTypeface(localTypeface1);
Collections.sort(providerlist1);
Log.v("COLLECTIONS", ""+providerlist1);
txt.setText("" + providerlist1.toArray()[position]);
String string = (String) txt.getText();
icon = string.toLowerCase();
System.out.println(icon);
int resid = getResources().getIdentifier("" + icon, "drawable", "com.digient.yeldi.app");
imageView = (ImageView) v.findViewById(R.id.icon_image);
imageView.setImageResource(resid);
return v;
}
}

Related

CheckBox unhide on GridView

I did set my Checkbox Visibility to "Gone" , in my xml file , and I want to unhide it when I go for OnItemLongListnere , on my GridView , with other words , if I have 8 pics in my gridView , I want to unhide this CheckBox for each photo! Thanks
My GridViewAdapter
public class GridViewAdapter extends BaseAdapter {
// Declare variables
ImageView image;
private Activity activity;
private String[] filepath;
private String[] filename;
private static LayoutInflater inflater = null;
public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
activity = a;
filepath = fpath;
filename = fname;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return filepath.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.gridview_item, null);
// Locate the TextView in gridview_item.xml
TextView text = (TextView) vi.findViewById(R.id.text);
// Locate the ImageView in gridview_item.xml
image = (ImageView) vi.findViewById(R.id.grid_image);
// Set file name to the TextView followed by the position
File file = new File(filepath[position]);
Picasso.with(activity).load(file).placeholder(R.drawable.rtrt).fit().centerCrop().into(image);
// Decode the filepath with BitmapFactory followed by the position
// Set the decoded bitmap into ImageView
// image.setImageBitmap(bmp);
return vi;
}
}
Maybe this can help you.
public class GridViewAdapter extends BaseAdapter {
// Declare variables
private Activity activity;
private String[] filepath;
private String[] filename;
private static LayoutInflater inflater = null;
public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
activity = a;
filepath = fpath;
filename = fname;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return filepath.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
if (convertView == null) {
holder = new Holder();
convertView = inflater.inflate(R.layout.gridview_item, null);
holder.image = (ImageView) convertView.findViewById(R.id.grid_image);
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.ck = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
// Set file name to the TextView followed by the position
File file = new File(filepath[position]);
holder.ck.setVisibility(View.GONE);
Picasso.with(activity).load(file).placeholder(R.drawable.rtrt).fit().centerCrop().into(holder.image);
return convertView;
}
class Holder {
ImageView image;
TextView text;
CheckBox ck;
}
}
and on GridView LongClick
gridview.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
CheckBox ck = (CheckBox)arg1.findViewById(R.id.checkbox);
ck.setVisibility(View.VISIBLE);
return true;
}
});
get the checkbox reference from your gridview_item layout in adapter and set its visibility View.VISIBLE.
Set onlongclicklistener on parent layout of your gridview_item layout.

Dynamically change custom list item background

I am creating a custom list view with favorite functionality, but I don't know how to change favorite image background on click. When I simply change the background of favorite icon than it automatically change background of another favorite image background at the time of scrolling. Please check below code :
public class CustomArrayAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater = null;
ArrayList<Customlist> list;
DecimalFormat formatter = new DecimalFormat("#,##,###");
public CustomArrayAdapter(Activity a, ArrayList<Customlist> list) {
activity = a;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.list = list;
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
TextView txt_unit, txt_state, txt_price, term_left, customr;
TextView install_date;
final ImageView fav;
View view = convertView;
if (convertView == null)
view = inflater.inflate(R.layout.list_item, null);
customr = (TextView) view.findViewById(R.id.customr);
txt_state = (TextView) view.findViewById(R.id.txt_state);
install_date = (TextView) view.findViewById(R.id.install_date);
term_left = (TextView) view.findViewById(R.id.term_left);
txt_price = (TextView) view.findViewById(R.id.txt_price);
fav = (ImageView) view.findViewById(R.id.fav);
txt_unit = (TextView) view.findViewById(R.id.txt_unit);
fav.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// fav = (ImageView)v.findViewById(R)
fav.setBackgroundResource(R.drawable.favourite_select);
Toast.makeText(activity, "click", 1).show();
}
});
// set values
customr.setText(list.get(position).getCUSTOMER());
txt_state.setText(list.get(position).getSTATE_NAME());
install_date.setText(list.get(position).getINSTALL_DATE());
term_left.setText(list.get(position).getTREM_LEFT());
String price = formatter.format(Integer.parseInt(list.get(position)
.getRUPEES()));
return view;
}
}
First, you need to implement the adapter on ViewHolder pattern:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHoldler holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(ctx).inflate(
R.layout.frag_home_gridview_item, null, false);
holder = new ViewHoldler();
holder.iv = (ImageView) convertView
.findViewById(R.id.gridview_item_label);
holder.tv = (TextView) convertView
.findViewById(R.id.gridview_item_name);
convertView.setTag(holder);
} else {
holder = (ViewHoldler) convertView.getTag();
}
holder.tv.setText(getItem(position));
holder.iv.setImageResource(this.ids[position]);
return convertView;
}
private class ViewHoldler {
ImageView iv;
TextView tv;
}
Second, use partial refreshment mechanism to change the target View's background:
private void refreshPartially(int position){
int firstVisiblePosition = listview.getFirstVisiblePosition();
int lastVisiblePosition = listview.getLastVisiblePosition();
if(position>=firstVisiblePosition && position<=lastVisiblePosition){
View view = listview.getChildAt(position - firstVisiblePosition);
if(view.getTag() instanceof ViewHolder){
ViewHolder vh = (ViewHolder)view.getTag();
//holder.play.setBackgroundResource(resId);//Do something here.
...
}
}
}
Third, add AdapterView.OnItemClickListener to your ListView:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
refreshPartially(position);
}
});
You need to implement the adapter on ViewHolder pattern:
http://www.vogella.com/tutorials/AndroidListView/article.html

How to make spinner show full items in Android

I have a problem with Android Spinner. I customised an adapter with a BaseAdapter. But when it show in the first time, it not show all items (I have only 6 items). Below is the screenshot
http://pik.vn/2014999186b9-448d-44ed-bd9b-c37484b368c6.png
And when I tap to the Spinner second time it show normally. Below is the screenshot. How can I show full item like that at the first time?
http://pik.vn/201454ed3e5b-4c67-4e9c-b8c6-feaf90c5dfb4.png
Update code:
Below is the adapter:
public class CategoryAdapter extends BaseAdapter implements SpinnerAdapter {
Context mContext;
List<CategoryModel> data;
int selectedCate;
boolean isSpinner;
Spinner spn;
public CategoryAdapter(Context context, List<CategoryModel> data, boolean isSpinner, Spinner spn) {
mContext = context;
selectedCate = 0;
this.data = data;
this.isSpinner=isSpinner;
this.spn = spn;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
if (position < data.size()) {
return data.get(position);
} else {
return null;
}
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category_spinner, parent, false);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_category_spinner_tv);
tvName.setSelected(spn.isSelected());
tvName.setText(data.get(position).getName());
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.inflater_category, parent, false);
ImageView img = (ImageView) convertView.findViewById(R.id.inflater_cate_image);
if (!data.get(position).getName().equals(parent.getContext().getString(R.string.browse))) {
Log.d("postion", "" + position);
UrlImageViewHelper.setUrlDrawable(img, data.get(position).getImage().getUrl());
} else {
img.setImageResource(R.drawable.ic_everything);
}
TextView tvName = (TextView) convertView.findViewById(R.id.inflater_cate_tv_name);
tvName.setText(data.get(position).getName());
return convertView;
}
public void swapView(int position) {
selectedCate = position;
}
}
and below is the code init Spinner in Activity
categoryAdapter = new CategoryAdapter(HomeActivity.this, result,true, mSpnActionbarCenterTitle);
mSpnActionbarCenterTitle.setAdapter(categoryAdapter);
The problem comes from the height of ImageView, it is wrap content and load via lazy loader, and the height of spinner not change when image loaded. My solution is fix the size of ImageView.

songs getting mixed up in listview

i want to get the list of all the music files in android device so i used the following code
public class MusicAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater l_Inflater;
public MusicAdapter(Context c) {
mContext = c;
l_Inflater = LayoutInflater.from(c);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext());
String id = null;
TextView txt_itemName = null;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.menu, null);
txt_itemName = (TextView) convertView.findViewById(R.id.textView1);
ImageView itemImage = (ImageView) convertView.findViewById(R.id.imageView1);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
txt_itemName.setText(id);
} else {}
return convertView;
}
}
everything works fine But when I scroll up and down, the list is mixed up. I tap on an title but it plays the wrong one. Each time I go back to the top of the list, a different song is displayed as the first song. what do i do.. ?? how do i solve this
This is what is wrong:
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
In getItem you need to return an instance that represents your object, maybe something like a Music object. And in the getItemId you need return an unique value for that instance.
Let's pretend that you load an array with Music instances, something like this:
public class MusicAdapter extends BaseAdapter {
public class Music {
private Long id;
private String pathToSd;
private Long lenght;
}
private List<Music> mMusicList;
private LayoutInflater mInflater;
public MusicAdapter(Context context, List<Music> music) {
mMusicList = music;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mMusicList.size();
}
#Override
public Object getItem(int position) {
return mMusicList.get(position);
}
#Override
public long getItemId(int position) {
return mMusicList.get(position).id; //If you aren't saving your list of music in some place like a db
//and thus, you don't have an id, you can return -1.
}
#Override
public View getView(int position, View convertedView, ViewGroup parent) {
if (convertedView == null) {
convertedView = mInflater.inflate(R.layout.your_layout, null);
}
//Perform your operations over the view here...
}
}
Anyways, I Strongly reccomend you save this list in a db.
Hope it helps :]
This code solved the problem
public class MusicAdapter extends BaseAdapter {
private LayoutInflater l_Inflater;
public MusicAdapter(Context c) {
l_Inflater = LayoutInflater.from(c);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
String id = null;
TextView txt_itemName = null;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.menu, null);
}
txt_itemName = (TextView) convertView.findViewById(R.id.textView1);
convertView.findViewById(R.id.imageView1);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
txt_itemName.setText(id);
return convertView;
}
}

android setOnItemClickListener not working on one row but ok in other row, using 2 row layouts

Im using a listView that have 2 layouts for the rows on one row the
setOnItemClickListener
but on the other row it doesnt recognize the taps,
public void initItemTable()
{
listViewItem = (ListView) getView().findViewById(R.id.listViewItem);
listViewItem.setAdapter(new PhoneItemAdapter(new ItemPhoneDataSource().getItems()));
listViewItem.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("mensa", "item index :"+arg2);
}
});
}
//
private class PhoneItemAdapter extends BaseAdapter {
final List<RowPhone> rows;//row
//data source, style
PhoneItemAdapter(List<ItemPhone> animals) {
rows = new ArrayList<RowPhone>();//member variable
//choose cell! iterator
for (ItemPhone item : animals) {
if (item.getType().equals("item")) {
rows.add(new RowItemPhone(LayoutInflater.from(getActivity()), item));
} else {
rows.add(new RowFolderPhone(LayoutInflater.from(getActivity()), item)); //imageRow!
}
}
}
//delegate
#Override
public int getViewTypeCount() {
return RowTypePhone.values().length;
}
#Override
public int getItemViewType(int position) {
//con cast
return ((RowPhone) rows.get(position)).getViewType();
}
public int getCount() {
return rows.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
//cambiado con cast!
return ((RowPhone) rows.get(position)).getView(convertView);
}
}
So the tap is detected when "item.getType()" is folder but not in item,
. Shall I include the code for RowItemPhone.java and RowFolderPhone.java?
so how to fix this tap problem?
thanks!
edit 1. RowPhone.java
public interface RowPhone {
public View getView(View convertView);
public int getViewType();
}
edit 2. RowFolder.java .. the one that have the tap detecting fine:
public class RowItemPhone implements RowPhone{
private final ItemPhone item;
private final LayoutInflater inflater;
public RowItemPhone(LayoutInflater inflater, ItemPhone animal) {
this.item = animal;
this.inflater = inflater;
}
//text doble
public View getView(View convertView) {
ViewHolder holder;
View view = null;
if (convertView == null) {
//ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_item_phone, null);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_item_phone, (ViewGroup) view, false);
holder = new ViewHolder(
(ImageView)viewGroup.findViewById(R.id.image_item),
(TextView)viewGroup.findViewById(R.id.title),
(TextView)viewGroup.findViewById(R.id.description));
viewGroup.setTag(holder); //pa q y como usa tag!
view = viewGroup;
} else {
view = convertView;
holder = (ViewHolder)convertView.getTag();
}
//setup
holder.imageView.setImageResource(item.getImageId());
holder.descriptionView.setText(item.getDescription());
holder.titleView.setText(item.getName());
return view;
}
public int getViewType() {
return RowTypePhone.DESCRIPTION_ROW.ordinal();
}
private static class ViewHolder {
final ImageView imageView;
final TextView titleView;
final TextView descriptionView;
private ViewHolder(ImageView imageView, TextView titleView, TextView descriptionView) {
this.imageView = imageView;
this.titleView = titleView;
this.descriptionView = descriptionView;
}
}
}
And here the RowFolderPhone.java ... showing fine, but not detecting taps:
public class RowFolderPhone implements RowPhone {
private final ItemPhone item;
private final LayoutInflater inflater;
public RowFolderPhone(LayoutInflater inflater, ItemPhone animal) {
this.item = animal;
this.inflater = inflater;
}
public View getView(View convertView) {
ViewHolder holder;
View view = null;
//we have a don't have a converView so we'll have to create a new one
if (convertView == null) {
// ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_folder_phone, null);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_folder_phone, (ViewGroup) view, false);
//use the view holder pattern to save of already looked up subview
holder = new ViewHolder((ImageView)viewGroup.findViewById(R.id.image),
(TextView)viewGroup.findViewById(R.id.title));
viewGroup.setTag(holder);
view = viewGroup;
} else {
//get the holder back out
holder = (ViewHolder)convertView.getTag();
view = convertView;
}
//actually setup the view
holder.imageView.setImageResource(item.getImageId());
holder.titleView.setText(item.getName());
return view;
}
public int getViewType() {
return RowTypePhone.IMAGE_ROW.ordinal();
}
private static class ViewHolder {
final ImageView imageView;
final TextView titleView;
private ViewHolder(ImageView imageView, TextView titleView) {
this.imageView = imageView;
this.titleView = titleView;
}
}
}
There are something wrong with your getItem method on PhoneItemAdapter. getItem method should return list item not position. So you should replace your code to
public RowPhone getItem(int position) {
return rows.get(position);
}
the problem was solved by setting descendantFocusability
in : row_item_phone.xml
<RelativeLayout ...
android:descendantFocusability="blocksDescendants"

Categories

Resources