Hi I am working with android . I had created a custom grid view with an imageview to add this item as favorite and when click on that item its background chaged as selected.Now the problem is that I can select all these favorite button and when I scroll down its selector position is changed and when I click on one item other item is automatically get selected. How can i solve this problem please help me. Thanks in advance :)
here is my adapter class
![public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String\[\] web1;
private final int\[\] Imageid;
public CustomGrid(Context c,String\[\] web,int\[\] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web1 = web;
}
#Override
public int getCount() {
return web1.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView button01;
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
v = inflater.inflate(R.layout.grid_single, parent, false);
} else {
v = convertView;
}
TextView text = (TextView)v.findViewById(R.id.grid_text);
text.setText(web1\[position\]);
ImageView image = (ImageView)v.findViewById(R.id.grid_image);
image.setBackgroundResource(Imageid\[position\]);
button01 = (ImageView)v.findViewById(R.id.star);
button01.setOnClickListener(new OnClickListener() {
int button01pos = 0;
public void onClick(View v) {
if (button01pos == 0) {
button01.setImageResource(R.drawable.star);
button01pos = 1;
} else if (button01pos == 1) {
button01.setImageResource(R.drawable.startclicked);
button01pos = 0;
}
}
});
return v;
}
}
Related
In my app i am showing some items in list view, each row have one name and one tick image. Tick image only shows up when item is selected(checked), The selection is working fine but the problem is when a new item is selected tick of previously selected item doesn't goes invisible. How this can be fixed? Any help will be appreciated
here is my code
Adapter
public class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
Context context;
private ArrayList<String> responseList;
private LinearLayout parent_choice_list_container;
private TextView item_name;
private ImageView iv_choice_list_item_selected;
public ListAdapter (Context context, ArrayList<String> responseList) {
this.context = context;
this.responseList = responseList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setSelectedIndex(int ind) {
selectedIndex = ind;
notifyDataSetChanged();
}
#Override
public int getCount() {
return responseList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.menu_list_item, null);
}
parent_choice_list_container = (LinearLayout) view.findViewById(R.id.parent_choice_list_container);
item_name = (TextView) view.findViewById(R.id.item_name);
iv_choice_list_item_selected = (ImageView) view.findViewById(R.id.iv_choice_list_item_selected);
iv_logo = (ImageView) view.findViewById(R.id.iv_blog_category_logo);
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
} list_item_name.setText(responseList.get(position).getName());
return view;
}
}
FragmentPart (Item Click Listener)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
response.get(position).setSelected(true);
ListAdapter.notifyDataSetChanged();
}
It sounds like you're using a CheckBox.
Have you tried conditionally updating the Views visibility depending on whether the item isChecked (and not isSelected)?
you should set tag for each image to keep track of its state (0 = not selected/invisible , 1 = selected/visible) , e.g:
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setTag(1);
}
and you can add this :
if (iv_choice_list_item_selected.getTag() == 1) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
}
this is my Adapter class
public class GridViewCustomAdapter extends BaseAdapter {
private List<ProductParameterBO> availList;
private LayoutInflater inflater;
Context context;
public GridViewCustomAdapter(Context ctx,List<ProductParameterBO> list){
this.context = ctx;
this.availList = list;
}
#Override
public int getCount() {
return availList.size();
}
#Override
public Object getItem(int position) {
return availList.get(position);
}
#Override
public long getItemId(int position) {
ProductParameterBO c = availList.get(position);
// long id = c.getTimeId();
return 0;
}
#Override
public View getView(final int position,View convertView,final ViewGroup parent) {
View row = convertView;
final TeeTimeHolder holder;
if (row == null){
inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.grid_row,parent,false);
holder = new TeeTimeHolder();
holder.myImage =(ImageView)row.findViewById(R.id.imageView10);
holder.imageflip =(ImageView)row.findViewById(R.id.img_flp);
holder.name =(TextView)row.findViewById(R.id.textView47);
holder.edit =(TextView)row.findViewById(R.id.textView49);
holder.rl =(RelativeLayout)row.findViewById(R.id.grid_img_ovrly);
// holder.row =(RelativeLayout)row.findViewById(R.id.row);
row.setTag(holder);
}
else {
holder =(TeeTimeHolder)row.getTag();
}
holder.name.setText(availList.get(position).getParameterName());
holder.myImage.setImageResource(R.drawable.toi);
holder.rl.setVisibility(View.GONE);
return row;
}
static class TeeTimeHolder {
ImageView myImage,imageflip;
TextView name,edit;
RelativeLayout rl;
}
}
i just want to show Relativelayout r1 when row is selected and when i select the next row ,previous row must hide Relativelayout r1 layout and show on row which is selected.. Now in my case when i select the next row ,then its not hiding from previous row ..layout still showing on all row which i select.
Please help me to sort out this problem..any help would be appreciated in advanced..
int priviousPosition=0;
ArrayList<View> viewList= new ArrayList<View>();
holder.rl.setTag(position);
viewList.add(holder.rl);
row.findViewById(R.id.grid_img_ovrly).setOnClickListener(new OnClickListener() {
public void onClick(View v1) {
View v=viewList.get(priviousPosition);
v.setVisibility(View.GONE);
int postition=(Integer) v1.getTag();
priviousPosition=postition;
notifyDataSetChanged();
}
});
Hi I am working with android custom grid view.I have a custom grid item with an Image View,Text View.When I click on image view it changes its color.It works perfectly.But I have a problem
Image view changes state on scroll of grid view
when I click an image view another image view also change its state.
I used the code
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web1;
private final int[] Imageid;
public CustomGrid(Context c,String[] web,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web1 = web;
}
#Override
public int getCount() {
return web1.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView button01;
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
v = inflater.inflate(R.layout.grid_single, parent, false);
} else {
v = convertView;
}
TextView text = (TextView)v.findViewById(R.id.grid_text);
text.setText(web1[position]);
ImageView image = (ImageView)v.findViewById(R.id.grid_image);
image.setBackgroundResource(Imageid[position]);
button01 = (ImageView)v.findViewById(R.id.star);
button01.setOnClickListener(new OnClickListener() {
int button01pos = 0;
public void onClick(View v) {
if (button01pos == 0) {
button01.setImageResource(R.drawable.star);
button01pos = 1;
} else if (button01pos == 1) {
button01.setImageResource(R.drawable.startclicked);
button01pos = 0;
}
}
});
return v;
}
}
try it this way...
Create another array to store the values of button01pos with initial values {0,0,0,0...};
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web1;
private final int[] Imageid;
private final int[] button01pos;
public CustomGrid(Context c,String[] web,int[] Imageid, int[] button01pos ) {
mContext = c;
this.Imageid = Imageid;
this.web1 = web;
this.button01pos=button01pos;
}
then in getview() method, Add this,
if (button01pos[position] == 0) {
button01.setImageResource(R.drawable.star);
} else if (button01pos == 1) {
button01.setImageResource(R.drawable.startclicked);
}
button01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (button01pos[position] == 0) {
button01.setImageResource(R.drawable.star);
button01pos[position] = 1;
} else if (button01pos[position] == 1) {
button01.setImageResource(R.drawable.startclicked);
button01pos[position] = 0;
}
}
});
I'm building a view with a custom ButtonAdapter, which loads a StringArray and puts out a GridView with Buttons.
Until the buttons are out of the Screen everything loads fine, but the last few Buttons which I only can see when I scroll down, are wrong.
public class ButtonAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
public ButtonAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.button, null);
Button button = (Button) gridView
.findViewById(R.id.button);
button.setText(mobileValues[position]);
Log.d("tag1", "value: " + mobileValues[position] +" position: "+ position);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v.findViewById(R.id.button);
String mobile = b.getText().toString();
Log.d("tag1", "button: " + mobile);
if (mobile.equals("Bäm 1")){
play(R.raw.bam1);
} else if (mobile.equals("Buz")){
play(R.raw.buz);
} else if (mobile.equals("Bäm 2")){
play(R.raw.bam2);
} else if (mobile.equals("Wir nehmen das Geld")){
play(R.raw.gold2);
}
}
});
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return mobileValues.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
Currently I'm passing in a string array with 26 Items. When it comes to the 20th button to draw, the position drops to 0 and the Button displays a wrong Text.
How do I fix this?
The problem with your code is that you are not setting the test for the views outside the screen. The correct code should be something like this
public class ButtonAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
private View.OnClickListener onClListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v.findViewById(R.id.button);
String mobile = b.getText().toString();
Log.d("tag1", "button: " + mobile);
if (mobile.equals("Bäm 1")){
play(R.raw.bam1);
} else if (mobile.equals("Buz")){
play(R.raw.buz);
} else if (mobile.equals("Bäm 2")){
play(R.raw.bam2);
} else if (mobile.equals("Wir nehmen das Geld")){
play(R.raw.gold2);
}
}
};
public ButtonAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.button, parent, false);
}
Button button = (Button) convertView
.findViewById(R.id.button);
button.setText(mobileValues[position]);
button.setOnClickListener(onClListener);
return convertView;
}
#Override
public int getCount() {
return mobileValues != null ? mobileValues.length : 0;
}
#Override
public Object getItem(int position) {
return mobileValues[position];
}
#Override
public long getItemId(int position) {
return position;
}
}
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.