How to implement onClick() method on ImageView - android

I have dynamically populated ImageViews and Textviews.
Now I have small problem with implementing onClick method on ImageView. On some ImageView click it should switch to fragment and for some click onto activity.
here is my code - my main fragment:
public class MainFragment extends Fragment {
public static String[] gridViewStrings = {
"string1",
"string2",
"string3",
"string4",
"string5",
"string6"
};
public static int[] gridViewImages = {
R.drawable.delivery,
R.drawable.shipping_logs,
R.drawable.meassurement,
R.drawable.takeovers,
R.drawable.settings,
R.drawable.download_data
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_fragment, parent, false);
gridView = (GridView) view.findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(getActivity(), gridViewStrings, gridViewImages));
imageView = (ImageView) view.findViewById(R.id.gridview_image);
}
my custom gridview adapter:
public class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#Override
public View getView(final int p, final View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = (View) convertView;
}
return grid;
}
}
QUESTION: How to implement onClick() on ImageView and switch to fragment and activity?

public DlgOutletListAdapter(Context context, List<WrapperOutlet> wrapperOutletList, View.OnClickListener listener) {
this.context = context;
this.wrapperOutletList = wrapperOutletList;
this.listener = listener; //---> pass in listener....
}
#Override
public int getCount() {
return wrapperOutletList.size();
}
#Override
public Object getItem(int position) {
return wrapperOutletList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_dlg_outlet_list, parent, false);
viewHolder = new ViewHolder(convertView);
viewHolder.ll_dlg_outlet_list_container.setOnClickListener(listener);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (!wrapperOutletList.isEmpty()) {
WrapperOutlet wrapperOutlet = wrapperOutletList.get(position);
Outlet outlet = wrapperOutlet.getOutlet();
if (outlet != null) {
if (TextUtils.isEmpty(outlet.getTel())){
viewHolder.ivCall.setImageResource(R.drawable.icon_call2);
viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.medium_gray));
viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(null);
} else {
viewHolder.ivCall.setImageResource(R.drawable.icon_call);
viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.fresh_teal));
viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(listener);
}
}
}
return convertView;
}
Pass in listener. Give you an example above. code below is where you call the adapter in fragment. My fragment implements View.OnClickListener
lv_dlg_outlet_list.setAdapter(new DlgOutletListAdapter(activity, wrapperOutletList, this));

Interface Callback:
public interface LaunchFragmentWithImage{
void launchFragmentWithImage(int imageID);
}
Give the adapter a reference of this class below so it can call the method. Wouldn't recommend giving the adapter itself a reference to the class because its not the adapters responsibility to know who recieves the image, thus violating the Single responsibility principle of OOD, but for the sake of learning it's okay.
public class ClassToLaunchNewFragment extends AppCompatActivity implements
LaunchFragmentWithImage{
#Override //Method from interface we've defined.
public void launchFragmentWithImage(int imageID){
FragmentToLaunch fragmentToLaunch = FragmentToLaunch.newInstance();
Bundle imageData = new Bundle();
imageData.putInt(imageID, "key");
fragmentToLaunch.setArguments(imageData);
getSupportFragmentManager().beginTransaction() //Call replace not add
.replace(containerID, fragmentToLaunch)
.commit();
}
}
In AdapterClass
imageViewVar.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
fragmentToLaunchInstanceThatWasPassedIn.launchFragmentWithImage(theImageResource);
}
});

grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
why are you pointing the grid variable to two separate View objects?

if (convertView == null) {
grid = inflater.inflate(R.layout.grid, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(choosedPosition){
case 0:do something;break;
case 1:do something;break;
...
}
}
});
}
It's just an example and can't running.

Related

Start Activity from GridView

I create a GridView and I want to start some activities from each button.
The gridView is in a class called Status, the code as follows:
public class Status extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
public static String[] gridViewStrings = {
"Stato",
"Allarmi",
"Impostazioni",
"Numero SIM",
"Dati Impianto",
"Esci",
};
public static int[] gridViewImages = {
R.drawable.chart,
R.drawable.alert,
R.drawable.set,
R.drawable.sim_status,
R.drawable.impianto2,
R.drawable.close,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Intent i = new Intent(getApplicationContext(), Status.class);
i.putExtra("id", position);
startActivity(i);
break;
case 1:
finish();
break;
}
}
});
initInstances();
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("ESA MOBILE App");
}
}
And my Adapter class as follows : I want to start activity from first button and to close the activity from second one, but doesn't work.
class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#SuppressLint("InflateParams")
#Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = convertView;
}
return grid;
}
}
I think you should update your getView() to set the correct click listener. It also seems you have a bug when you recycle views as you don't update them in the adapter (BTW, using a the View Holder pattern would make this code more efficient):
#SuppressLint("InflateParams")
#Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
} else {
grid = convertView;
}
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
// Here you can set your click listener.
// Another option is to have a OnClickListener array (like you have for the strings and images and use it in a similar way).
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (p) {
case 0:
// Start activity
break;
case 1:
// Close
break;
default:
// Do something else
break;
}
}
});
return grid;
}
I am not sure but i think you are requesting click on the item(not on the ImageView inside the item)
so implement click in adapter after get the id of your parent or you can implement the click on your image in adapter like:
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast here
}});

get swiped image position in swipe cards [Tinder Swipe]

I am working on tinder swipe cards library.So I referred this Sample.
My issue is, while swiping image I need to get the swiped image position.So far I tried like int idGet = flingContainer.getSelectedItemPosition(); But it is not working for me.
Below I have posted relevant code :
Logcat:
E:/ idGet : -1
CardsFragment.java:
public class CardsFragment extends Fragment {
private SwipeFlingAdapterView flingContainer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
flingContainer = (SwipeFlingAdapterView) rootView.findViewById(R.id.frame);
likeImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
likeNope();
flipMethodRight(80.00f);
}
});
}
}
void flipMethodRight(float scrollProgressPercent) {
flingContainer.getTopCardListener().selectRight();
View view = flingContainer.getSelectedView();
view.findViewById(R.id.background).setAlpha(0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
public class MyAppAdapter extends BaseAdapter {
public List<GetImageData> parkingList;
public Context context;
private MyAppAdapter(List<GetImageData> apps, Context context) {
this.parkingList = apps;
this.context = context;
}
#Override
public int getCount() {
return parkingList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater =getActivity(). getLayoutInflater();
rowView = inflater.inflate(R.layout.adapt_card_frag, parent, false);
Log.e("AdapterCalling", "AdapterCalling");
// configure view holder
viewHolder = new ViewHolder();
viewHolder.DataText = (TextView) rowView.findViewById(R.id.bookText);
viewHolder.background = (FrameLayout) rowView.findViewById(R.id.background);
viewHolder.cardImage = (ImageView) rowView.findViewById(R.id.cardImage);
Log.e("ParkingList", "" +parkingList.get(position).getDescription() + "");
int idGet = flingContainer.getSelectedItemPosition();
Log.e("idGet", ""+idGet);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.DataText.setText(parkingList.get(position).getDescription() + "");
Glide.with(CardsFragment.this).load(parkingList.get(position).getImagePath()).into(viewHolder.cardImage);
return rowView;
}
}
I solved this issue with the help of below code:
String getImageFlingStr, GET_PRESENT_IMAGE;
getImageFlingStr = nameImgArrList.get(0).getImagePath();
presentImageShowing = getImageFlingStr;
GET_PRESENT_IMAGE = presentImageShowing;
Log.e("presentImageShowing", ""+presentImageShowing);

Android: NPE while loading Alertdialog with Gridview

Hello and thanks for your help!
Upon clicking the button, the alertdialog should open up with a gridview inside.
This is my code:
loginWith.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setTitle(getResources().getString(R.string.loginWith));
final AlertDialog dialog = builder.create();
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_loginwith, (ViewGroup) findViewById(R.id.layout_loginwith));
builder.setView(layout);
GridView gridView = (GridView) layout.findViewById(R.id.gvLoginwith);
gridView.setAdapter(new LoginWithAlertDialogImageAdapter(context));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
loginWithFacebook();
break;
case 1:
loginWithGoogle();
break;
case 2:
loginWithLinkedIn();
break;
case 3:
loginWithTwitter();
break;
}
dialog.dismiss();
}
});
dialog.show();
}
});
But it ends up leaving the NullPointerException at gridView.setAdapter() part above and the context.getResources().getString() methods in the following class.
public class LoginWithAlertDialogImageAdapter extends BaseAdapter {
private Context context;
private LayoutInflater mLayoutInflater;
public LoginWithAlertDialogImageAdapter(Context context) {
this.context = context;
mLayoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return 0;
}
#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) {
ViewHolder holder;
if(convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.categorycontent_log_in_with, null);
convertView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.loginwith_title);
holder.icon = (ImageView) convertView.findViewById(R.id.loginwith_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setAdjustViewBounds(true);
holder.icon.setScaleType(ImageView.ScaleType.FIT_XY);
holder.icon.setPadding(8, 8, 8, 8);
holder.title.setText(items[position]);
holder.icon.setImageResource(icons[position]);
return convertView;
}
class ViewHolder {
TextView title;
ImageView icon;
}
private Integer[] icons = {
R.drawable.ic_facebook,
R.drawable.ic_google,
R.drawable.ic_linkedin,
R.drawable.ic_twitter
};
private String[] items = {
context.getResources().getString(R.string.facebook),
context.getResources().getString(R.string.google),
context.getResources().getString(R.string.linkedin),
context.getResources().getString(R.string.twitter)
};
}
How should this be overcome?
Added I corrected the adapter code following the suggestion as follows.
public class LoginWithAlertDialogImageAdapter extends BaseAdapter {
private Context context;
private LayoutInflater mLayoutInflater;
private String items[];
public LoginWithAlertDialogImageAdapter(Context context) {
this.context = context;
mLayoutInflater = LayoutInflater.from(context);
items = new String[] {
context.getResources().getString(R.string.facebook),
context.getResources().getString(R.string.google),
context.getResources().getString(R.string.linkedin),
context.getResources().getString(R.string.twitter)
};
}
#Override
public int getCount() {
return 0;
}
#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) {
ViewHolder holder;
if(convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.categorycontent_log_in_with, null);
convertView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.loginwith_title);
holder.icon = (ImageView) convertView.findViewById(R.id.loginwith_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setAdjustViewBounds(true);
holder.icon.setScaleType(ImageView.ScaleType.FIT_XY);
holder.icon.setPadding(8, 8, 8, 8);
holder.title.setText(items[position]);
holder.icon.setImageResource(icons[position]);
return convertView;
}
class ViewHolder {
TextView title;
ImageView icon;
}
private Integer[] icons = {
R.drawable.ic_facebook,
R.drawable.ic_google,
R.drawable.ic_linkedin,
R.drawable.ic_twitter
};
}
Now the NullPointerException is not thrown anymore, but the thing is the icons and the icon titles and the GridView itself are not shown, but only the title of the AlertDialog is shown. Do you figure out the next issue?
Here:
context.getResources().getString(R.string.facebook)
context is null .
Why?
items Array is declared and initialized as a class level variable, during adding items in items Array, using context for accessing getResources method which is null because context is initialized inside constructor of LoginWithAlertDialogImageAdapter class.
Means context is accessed before executing constructor of LoginWithAlertDialogImageAdapter class.
So, to fix this issue declare items Array as class level variable but add item in it after initializing context :
private String[] items;
public LoginWithAlertDialogImageAdapter(Context context) {
this.context = context;
...
items = {
context.getResources().getString(R.string.facebook),
...
};
}

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"

Android - Pull to refresh on custom adapter

I'm developing an Android Application and I have a custom adapter for my ListView. I want to use a Pull to refresh library, such as this one. This works fine with an ArrayAdapter, but not with a custom adapter.
How can I get my pull to refresh working with a custom adapter?
Here my adapter code:
public class AdvertListAdapter extends BaseAdapter {
private AdvertListActivity activity;
private int currentFirstVisibleItem=0;
private int currentLastVisibleItem=8;
public AdvertListAdapter(AdvertListActivity activity) {
this.activity = activity;
}
public void setFirstVisibleItem(int currentFirstVisibleItem) {
this.currentFirstVisibleItem=currentFirstVisibleItem;
}
public void setLastVisibleItem(int currentLastVisibleItem) {
this.currentLastVisibleItem=currentLastVisibleItem;
}
#Override
public int getCount() {
return activity.getAdvertList().size();
}
#Override
public Advert getItem(int pos) {
return activity.getAdvertList().get(pos);
}
#Override
public long getItemId(int pos) {
return activity.getAdvertList().get(pos).getId();
}
private class ViewHolder {
public ImageView image;
public TextView title;
public TextView price;
public TextView size;
public TextView city;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.listview_advertlist, null);
view.image = (ImageView) convertView.findViewById(R.id.advertImage);
view.title = (TextView) convertView.findViewById(R.id.advertTitleText);
view.price = (TextView) convertView.findViewById(R.id.advertPriceText);
view.size = (TextView) convertView.findViewById(R.id.advertSizeText);
view.city = (TextView) convertView.findViewById(R.id.advertCityText);
convertView.setTag(view);
}else {
view = (ViewHolder) convertView.getTag();
}
Advert advert = activity.getAdvertList().get(position);
view.title.setText(advert.getTitle());
view.price.setText(String.valueOf(advert.getPrice()+"€"));
view.size.setText("Taille: "+advert.getSize());
view.city.setText(advert.getCity());
if(position <= currentFirstVisibleItem || position >= currentLastVisibleItem) {
view.image.setImageResource(R.drawable.defaultadvertimage);
ServiceHelper.loadAsyncImage(view.image, activity.getApplicationContext(), String.valueOf(advert.getId()+"-mini"), "advert");
}
return convertView;
}
}
Here my activity code:
list = (PullToRefreshListView) findViewById(R.id.advertList);
advertList = getAdvertList();
((PullToRefreshListView) list).setOnRefreshListener(new OnRefreshListener() {
#Override
public void onRefresh() {
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
list.setAdapter(adapter);

Categories

Resources