I have a problem with ListView. Here the code of adapter :
public class AdapterListViewEncyclopedia extends ArrayAdapter<Apple> {
private int layoutResourceId;
private Context context;
private List<Apple> data;
private CellHolder holder;
public AdapterListViewEncyclopedia(Context context, int layoutResourceId, List<Apple> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
static class CellHolder
{
ImageView imageApple;
TextView tvCellAppleName;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CellHolder();
holder.imageApple = (ImageView) row.findViewById(R.id.imageApple);
holder.tvCellAppleName = (TextView)row.findViewById(R.id.tvAppleName);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(InterfaceConfig.serverURL+data.get(position).getImageURL(), holder.imageApple);
holder.tvCellAppleName.setText(data.get(position).getName());
System.out.println(position);
row.setTag(holder);
}
else
{
holder = (CellHolder)row.getTag();
}
return row;
}
}
The Main Problem :
1.I have 10 data in ArrayAdapter<Apple> and When i open page with ListView and i print position code with System.out.println, the code show 0,1,2,3,4,5,0
2. When i scroll the ListView , Top and Bottom which hidden is random position.
Anyone can help me to solve my problem?
I have update the code pls try this. You have to set the data after the if-else block other wise you cannot get the correct position in the listview while binding the data. Please refer the android view holder properties
ViewHolder
public class AdapterListViewEncyclopedia extends ArrayAdapter<Apple> {
private int layoutResourceId;
private Context context;
private List<Apple> data;
private CellHolder holder;
public AdapterListViewEncyclopedia(Context context, int layoutResourceId, List<Apple> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
static class CellHolder
{
ImageView imageApple;
TextView tvCellAppleName;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CellHolder();
holder.imageApple = (ImageView) row.findViewById(R.id.imageApple);
holder.tvCellAppleName = (TextView)row.findViewById(R.id.tvAppleName);
row.setTag(holder);
}
else
{
holder = (CellHolder)row.getTag();
}
// you can set the data here
setData(position,holder);
return row;
}
private void setData(int position, CellHolder holder)
{
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(InterfaceConfig.serverURL+data.get(position).getImageURL(), holder.imageApple);
holder.tvCellAppleName.setText(data.get(position).getName());
}
}
Related
I intially made all my checkbox of gridview arrayadapter as invisible. After longclick of griditem i want to make all checkbox visible.
My adapter class
public class GridViewAdapter extends ArrayAdapter<ImageItem> {
private Context context;
private int layoutResourceId;
private ArrayList<ImageItem> data = new ArrayList<ImageItem>();
private File[] imageFiles;
public GridViewAdapter(Context context, int layoutResourceId, ArrayList<ImageItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;//id of grid_item_layout
this.context = context;
this.imageFiles = imageFiles;
this.data = data; }
public View getView(final int position, final View convertView, #NonNull ViewGroup parent) {
row = convertView;
final ViewHolder holder;
//existing views recycled
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
//find resource...
holder.image = (ImageView) row.findViewById(R.id.image);
holder.check = (CheckBox) row.findViewById(R.id.checkbox);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.check.setOnCheckedChangeListener(null);
holder.check.setFocusable(false);
holder.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (data.get(position).isSelected()) {
data.get(position).setSelected(false);
} else {
data.get(position).setSelected(true);
}
}
});
holder.check.setChecked(data.get(position).isSelected());
holder.image.setImageBitmap(data.get(position).getImage());
return row;
}
static class ViewHolder {
ImageView image;
CheckBox check;
}
}
This is method in mainactivity.
gridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout,
getData());
gridView.setAdapter(gridAdapter);
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
for(int index = 0; index < adapterView.getChildCount(); index++) {
View nextchild = (adapterView.getChildAt(index));
CheckBox checkBox = (CheckBox)
nextchild.findViewById(R.id.checkbox);
checkBox.setVisibility(View.VISIBLE);
}
return true;
});
This makes all the checkbox visible except the checkbox of one row.Refer the image.How to make all the checkbox visible?Any help is appreciated.
I have a special class, a custom adapter for my ListView and I need to get some data from another Activity. But my implementation of the method GetIntent()GetExtras() isn't working. What is wrong?
Here is my custom adapter code:
public class CustomAdapter extends ArrayAdapter<String> {
int myColor,myWidth;
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// return super.getView(position, convertView, parent);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.ColorTextButton);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageViewIcon);
TextView textView1 = (TextView) rowView.findViewById(R.id.HelpButton);
textView.setText(values[position]);
String s = values[position];
System.out.println(s);
if (s.equals("Monday")) {
imageView.setImageResource(R.drawable.arrow2);
textView.setBackgroundColor(Color.YELLOW);
} else if (s.equals("Wednesday")) {
imageView.setImageResource(R.drawable.arrow2);
textView1.setBackgroundColor(Color.GREEN);
} else if (s.equals("Friday")) {
imageView.setImageResource(R.drawable.arrow2);
} else {
imageView.setImageResource(R.drawable.arrow);
}
return rowView;
}
}
If you want the data to be accessable From a few activities you should think about use a singleton data Class. so in stead of trying to get pass the data from your adapter to a second activity both the adaper and the Activity use the same method to get the data class
public class DataProvider {
private static DataProvider instance;
public static DataProvider getInstance()
{
if(null == instance){instance = new DataProvider();}
return instnace;
}
public String[] getObjects(){
return this.myStringArray;
}
// add more methods in here to retrieve and count your data as you need
}
then in your Activity you can
// somewhere
myStingArrayInMyActivity = DataProvider.getInstance().getObjects()
also in you adapter
you can do
public class CustomAdapter extends ArrayAdapter<String> {
int myColor,myWidth;
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// your code
textView.setText(DataProvider.getInstance.getObjects[position]);
String s = DataProvider.getInstance.getObjects[position];
/// your code
return rowView;
}
}
Sorry for my english. I use SlideMenu libruary and i want use new font for textview, it old version use:
((ListView) ((Activity) context).findViewById(R.id.sidemenulistobject)).setAdapter(
new ArrayAdapter<Object>(
context,
R.layout.sidemenu_item,
R.id.textSlide,
items
)
);
But this i cant get my textSlide and set new font. Now i add array adapter and set this in listView. This is my all code:
menu = new SlidingMenu(context);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
menu.setShadowWidth(15);
menu.setFadeDegree(1.0f);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.attachToActivity((Activity) context, SlidingMenu.SLIDING_WINDOW);
menu.setBehindWidth(400);
menu.setMenu(R.layout.sidemenu);
//add item in list view
ArrayList<String> itemsObj = new ArrayList<String>();
itemsObj.add("Новости");
itemsObj.add("События");
itemsObj.add("Наше меню");
itemsObj.add("Фотографии");
itemsObj.add("Видеозаписи");
itemsObj.add("Контакты");
itemsObj.add("Мой профиль");
//get sidemenulistobject
ListView lv = ((ListView) ((Activity) context).findViewById(R.id.sidemenulistobject));
//add adapter
SlideAdapter adapter = new SlideAdapter((Activity) context, R.layout.sidemenu_item, itemsObj);
lv.setAdapter(adapter);
This is my SlideAdapter
public class SlideAdapter extends ArrayAdapter<MenuCategoryObject>{
ArrayList<String> listItems;
int Resourse;
Context context;
LayoutInflater vi;
private ImageLoader imageLoader;
public SlideAdapter(Context context, int resource, ArrayList<String> listItems) {
super(context, resource);
this.listItems = listItems;
Resourse = resource;
this.context = context;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = vi.inflate(Resourse, null);
holder = new ViewHolder();
Typeface face=Typeface.createFromAsset(context.getAssets(), "font/AvenirNext-Medium.ttf");
holder.textSlide = (TextView) convertView.findViewById(R.id.textSlide);
holder.textSlide.setTypeface(face);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textSlide.setText(listItems.get(position));
return convertView;
}
static class ViewHolder {
public TextView textSlide;
}
}
But my item list view dont show. I dont know why. Please help
the problem is with your adapter. Since you are not providing the dataset to the super constructor, you have to override getCount and return its size. Add
public int getCount() {
return listItems.size();
}
to your adapter
I have to replace the white image in the clicked position of GridView with green one to indicate its selected in onItemClickListener method. I have used custom adapter to preset the images in first look in my grid. What should i do to change the image of selected image only.
gridView.setAdapter(new CustomGridViewAdapter(ctx, R.layout.seatrow_grid, seat_list));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
Seat_Availabe st_av = (Seat_Availabe) arg0.getItemAtPosition(position);
if ((st_av.getAvailable().equals("true")) && (st_av.getLadiesSeat().equals("false"))) {
}
else{
Toast.makeText(getApplicationContext(), "Already Booked", Toast.LENGTH_LONG).show();
}
}
});
my adapter class to set images in first look
public class CustomGridViewAdapter extends ArrayAdapter<Seat_Availabe>
{
Context context;
int layoutResourceId;
List<Seat_Availabe> data = new ArrayList<Seat_Availabe>();
public CustomGridViewAdapter(Context context, int layoutResourceId, List<Seat_Availabe> data)
{
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
RecordHolder holder = null;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
}
else
{
holder = (RecordHolder) row.getTag();
}
Seat_Availabe seat = data.get(position);
String avail = seat.getAvailable();
String ladies = seat.getLadiesSeat();
if ((seat.getAvailable().equals("true")) && (seat.getLadiesSeat().equals("false"))) {
holder.imageItem.setImageResource(R.drawable.white);
}
else if((seat.getAvailable().equals("false")) && (seat.getLadiesSeat().equals("true")))
{
holder.imageItem.setImageResource(R.drawable.pink);
}
else if(seat.getAvailable().equals("false")){
holder.imageItem.setImageResource(R.drawable.gray);
}
else if(seat.getAvailable().equals("NA")){
holder.imageItem.setImageResource(R.drawable.whitenew);
}
else{
holder.imageItem.setImageResource(R.drawable.gray);
}
return row;
}
public static class RecordHolder
{
public ImageView imageItem;
}
}
I suppose you get the view object in the variable v of the click event, you could set the image of the view by:
v.setBackgroundResource(R.drawable.filename);//add your filename here
I am using a ListView with images, also I have an image loader. The problem is that when there is a fast scrolling the images appears on wrong rows, after short time when the scrolling stops every thing gets back in place.
Here is my adapter:
public class MyAdapter extends ArrayAdapter<MyObj>
{
int layoutResourceId;
List<MyObj> data;
Activity activity;
int rowViewId;
public MyAdapter(Activity activity, int layoutResourceId, List<MyObj> data)
{
super(activity, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.activity = activity;
this.data = data;
rowViewId = R.layout.my_layout.xml;
}
public static class ViewHolder
{
public TextView title;
public ImageView image;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
ViewHolder holder;
if (row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(rowViewId, null);
holder = new ViewHolder();
holder.title = (TextView) row.findViewById(R.id.title);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
}
else
{
holder = (ViewHolder) row.getTag();
}
final MyObj obj = data.get(position);
if (event != null)
{
holder.title.setText(event.getTitle());
ImageLoader imageLoader = new ImageLoader();
imageLoader.loadImageFromAppServer(obj.getImageName(), holder.image);
}
return row;
}
}
Is there any thing I can do to prevent this unwanted behaviour?
Your image is being loaded asynchronously by loadImageFromAppServer. When it arrives from the server, your holder.image object is already reused by another row that doesn't represent the same data it did when you asked your ImageLoader object to download the image. You need to implement a way to tell the ImageLoader object to cancel the previous download request.