android implement multiple click event in ListView item - android

I wanna to implement multiple button in my ListView items. But the problem is that i couldn't handle click event when button is clicked, i mean they are Play button and Image button like this
Here is my Adapter:
public class ArtistsAdapter extends BaseAdapter implements SectionIndexer {
private HashMap<String, Integer> alphaIndexer;
private String[] sections;
private LayoutInflater inflater;
private int resId;
private Context context;
private ArrayList<Artist> artistList;
public ArtistsAdapter(Context context, int resId, ArrayList<Artist> artistList) {
this.resId = resId;
this.context = context;
this.artistList = artistList;
this.inflater = LayoutInflater.from(context);
}
#Override
public Object[] getSections() {
return sections;
}
#Override
public int getPositionForSection(int section) {
return alphaIndexer.get(sections[section]);
}
#Override
public int getSectionForPosition(int position) {
return 1;
}
private static class ViewHolder {
ImageView artistImgItem;
ImageButton artistPlayItem;
TextView artistNameItem;
TextView artistAgeItem;
LinearLayout artistItem;
}
#Override
public int getCount() {
return artistList.size();
}
#Override
public Object getItem(int position) {
return artistList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Artist artist = (Artist) getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
convertView = this.inflater.inflate(resId, parent, false);
viewHolder = new ViewHolder();
viewHolder.artistImgItem = (ImageView) convertView.findViewById(R.id.artistImg);
viewHolder.artistPlayItem = (ImageButton) convertView.findViewById(R.id.artistPlay);
viewHolder.artistNameItem = (TextView) convertView.findViewById(R.id.artistName);
viewHolder.artistAgeItem = (TextView) convertView.findViewById(R.id.artistAge);
viewHolder.artistItem = (LinearLayout) convertView.findViewById(R.id.artistItem);
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();
viewHolder.artistNameItem.setText(artist.getName());
viewHolder.artistPlayItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("aaaaa", position+"ssss");
Toast.makeText(context, "Music playin " + position+1 +"", Toast.LENGTH_SHORT).show();
}
});
viewHolder.artistItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ArtistsActivity) context).showArtistView();
}
});
viewHolder.artistImgItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ArtistsActivity) context).showArtistView();
}
});
return convertView;
}
}
And my activity looks like that
public class ArtistsActivity extends Activity {
private IndexableListView artistListUI;
private ImageView maleArtist, femaleArtist, groupArtist, artistImg;
private ArtistsAdapter artistsAdapter;
private ArrayList<Artist> maleArtistList, femaleArtistList, groupArtistList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.artist_list);
artistListUI = (IndexableListView) findViewById(R.id.artistListView);
maleArtist = (ImageView) findViewById(R.id.maleArtist);
femaleArtist = (ImageView) findViewById(R.id.femaleArtist);
groupArtist = (ImageView) findViewById(R.id.groupArtist);
artistImg = (ImageView) findViewById(R.id.artistImg);
artistListUI.setFastScrollEnabled(true);
artistsAdapter = new ArtistsAdapter(ArtistsActivity.this, R.layout.artist_item, artistList);
artistListUI.setAdapter(artistsAdapter);
public void showArtistView() {
startActivity(new Intent(ArtistsActivity.this, AlbumsActivity.class));
}
}

you have to set clickable property of listview to false
because of entire listview click is fire

This is resolved in the adapter. Look at this example:
https://github.com/m-alcu/SkoltiAlert/blob/master/src/com/alcubier/skoltialert/tracks/TrackAdapter.java
In the bindView or getView method you have to define your clickhandler linked to your view:
holder.hLayout.setOnClickListener(new MentionClickhandler());
And do sour stuff in this function:
public class MentionClickhandler implements View.OnClickListener
{
public void onClick( View view ){
Track trackTag = new Track();
trackTag = (Track) view.getTag();
Log.i(TAG, "item: "+trackTag.getSearchTrack());
Bundle bundle = new Bundle();
bundle.putInt("idTrack", trackTag.getIdTrack());
bundle.putString("searchTrack", trackTag.getSearchTrack());
bundle.putInt("topTrack", trackTag.getTopTrack());
bundle.putLong("lastId", trackTag.getLastId());
bundle.putInt("count", trackTag.getCount());
bundle.putString("type", "LastMentions");
Intent i = new Intent(context, GDMentionsList.class);
i.putExtras(bundle);
context.startActivity(i);
}
}
Hope it helps...

Related

Iterate quantity for items , custom adapter

I have a listview where every items have one TextView and 2 Buttons ( + and - ). I want to iterate the quantity from TextView when I press the + Button. After this, I need to sum up all items (price * item quantity) for every item from the list and put the result into new TextView(total cost). That is my adapter and activity main. I appreciate any help.
public class cartItemsAdapter extends BaseAdapter {
private ArrayList<items> itemsList;
private Context context;
public cartItemsAdapter(ArrayList<items> list,Context cont){
this.itemsList = list;
this.context=cont;
}
#Override
public int getCount() {
return this.itemsList.size();
}
#Override
public Object getItem(int position) {
return this.itemsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
LayoutInflater inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.list_cart_item_view,null);
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.icon_item);
holder.nameView = (TextView) convertView.findViewById(R.id.name_item);
holder.priceView=(TextView)convertView.findViewById(R.id.price_item);
holder.plusButton = (Button) convertView.findViewById(R.id.cart_plus_button);
holder.minusButton = (Button) convertView.findViewById(R.id.cart_minus_button);
holder.removeButton = (Button) convertView.findViewById(R.id.cart_remove_button);
holder.quantityView = (TextView) convertView.findViewById(R.id.cart_quantity_view);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
items itm = itemsList.get(position);
holder.imageView.setImageResource(itm.getlImageId());
holder.nameView.setText(itm.getlName());
holder.priceView.setText(itm.getlPrice() + " $");
holder.quantityView.setText("1");
/*holder.plusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pquant=Integer.valueOf(quant.toString());
pquant++;
}
});
holder.minusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int mquant=Integer.valueOf(quant.toString());
mquant--;
}
});*/
return convertView;
}
private static class ViewHolder{
public ImageView imageView;
public TextView nameView;
public TextView priceView;
public Button plusButton;
public Button minusButton;
public Button removeButton;
public TextView quantityView;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart);
plusBtn = findViewById(R.id.cart_plus_button);
minusBtn = findViewById(R.id.cart_minus_button);
removeBtn = findViewById(R.id.cart_remove_button);
quantity = findViewById(R.id.cart_quantity_view);
listView = (ListView) findViewById(R.id.cart_category_listview);
favoriteDbHelper = new DatabaseHelper(this);
item = favoriteDbHelper.getAllDataCart();
myAdapter = new cartItemsAdapter(item, this);
listView.setAdapter(myAdapter);
}
Actually this code of yours is pretty close:
holder.plusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pquant=Integer.valueOf(quant.toString());
pquant++;
}
});
holder.minusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int mquant=Integer.valueOf(quant.toString());
mquant--;
}
});
All you need to do is to access itemsList inside onClick(View v). To make it simple, try this:
holder.plusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
Items item = itemsList.get(position);
//now you can get your quant by accessing this item object
}
});
The key is to get current position using getAdapterPosition().

button.setText() does not work inside onClick() method

I have a view pager where each page has a cardview with an image, some text, and a button. I need the button text of each page to change after being clicked.
So inside ViewPagerAdapter mapSize.setText() works only outside setOnClickListener(), while inside onClick it does not work.
This is my ViewPagerAdapter class
public class ViewPagerAdapter extends PagerAdapter {
private List<Model> models;
private LayoutInflater layoutInflater;
private Context context;
private Button mapSize;
private Integer size;
private static final String TAG = "ViewPagerAdapter";
public ViewPagerAdapter(Context context, List<Model> models, String playerName) {
this.models = models;
this.layoutInflater = layoutInflater;
this.context = context;
}
#Override
public int getCount() {
return models.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object o) {
return view.equals(o);
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, final int position) {
layoutInflater = LayoutInflater.from(context);
View view = null;
ImageView imageView;
TextView title, desc;
imageView = view.findViewById(R.id.gameModeImage);
title = view.findViewById(R.id.title);
desc = view.findViewById(R.id.description);
imageView.setImageResource(models.get(position).getImage());
title.setText(models.get(position).getTitle());
desc.setText(models.get(position).getDescription());
mapSize = view.findViewById(R.id.mapSizeBtn);
mapSize.setText("hello2"); //here works
mapSize.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapSize.setText("hello"); //here does not work
}
});
container.addView(view, 0);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "This page was clicked: " + position);
if (position != 1)
Toast.makeText(context, "Coming Soon.", Toast.LENGTH_SHORT).show();
else {
Intent newGameActivity = new Intent(context, com.game.project.GameActivity.class);
newGameActivity.putExtra("size", size);
context.startActivity(newGameActivity);
}
}
});
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View)object);
}
}
You reassign your button each item a new item instantiates in your adapter.
You must reffer to your button in this way:
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, final int position) {
layoutInflater = LayoutInflater.from(context);
View view = null;
ImageView imageView;
TextView title, desc;
imageView = view.findViewById(R.id.gameModeImage);
title = view.findViewById(R.id.title);
desc = view.findViewById(R.id.description);
imageView.setImageResource(models.get(position).getImage());
title.setText(models.get(position).getTitle());
desc.setText(models.get(position).getDescription());
final Button mapSize = view.findViewById(R.id.mapSizeBtn);
mapSize.setText("hello2"); //here works
mapSize.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapSize.setText("hello"); //here does not work
}
});
container.addView(view, 0);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "This page was clicked: " + position);
if (position != 1)
Toast.makeText(context, "Coming Soon.", Toast.LENGTH_SHORT).show();
else {
Intent newGameActivity = new Intent(context, com.game.project.GameActivity.class);
newGameActivity.putExtra("size", size);
context.startActivity(newGameActivity);
}
}
});
return view;
}
All items in instantiate item should be declared final to work properly.

Add an onclicklistener to every row from a listview (android)

I've fetched and parsed a JSON data from an URL and displayed it in ListView but I can't seem to add an onclicklistener on every row I just want to be able to do an action when I click on the row, with the data that the row contains to get all informations of this row in an other activity ?
when i launch code, the app stopped immediatly !!! thanks
public class DemandeAdapter extends BaseAdapter {
private Context context;
private ArrayList<DemandeModel> demandeModelArrayList;
public DemandeAdapter(Context context, ArrayList<DemandeModel> demandeModelArrayList) {
this.context = context;
this.demandeModelArrayList = demandeModelArrayList;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return demandeModelArrayList.size();
}
#Override
public Object getItem(int position) {
return demandeModelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.demande, null, true);
holder.iv = (ImageView) convertView.findViewById(R.id.iv);
holder.id = (TextView) convertView.findViewById(R.id.id);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.desc = (TextView) convertView.findViewById(R.id.desc);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.listView = (ListView) convertView.findViewById(R.id.lv);
convertView.setTag(holder);
}else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)convertView.getTag();
}
Picasso.get().load(demandeModelArrayList.get(position).getImgURL()).into(holder.iv);
holder.id.setText("ID : "+demandeModelArrayList.get(position).getId());
holder.date.setText("Date : "+demandeModelArrayList.get(position).getDate());
holder.name.setText("Name : "+demandeModelArrayList.get(position).getNom());
holder.desc.setText("Description : "+demandeModelArrayList.get(position).getDescription());
holder.listView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, demandeDetails.class);
intent.putExtra("ID",demandeModelArrayList.get(position).getId());
intent.putExtra("Date",demandeModelArrayList.get(position).getDate());
intent.putExtra("Name",demandeModelArrayList.get(position).getNom());
intent.putExtra("Description",demandeModelArrayList.get(position).getDescription());
intent.putExtra("Image",demandeModelArrayList.get(position).getImgURL());
context.startActivity(intent);
}
});
return convertView;
}
private class ViewHolder {
protected TextView id,name,desc,date;
protected ImageView iv;
private ListView listView;
}
}
i do that min my adapter class not in my activity !!!
And this is the other activity where i want to display data
public class demandeDetails extends AppCompatActivity implements Serializable {
protected TextView id,name,desc,date;
protected ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demande_details);
id = (TextView)findViewById(R.id.demande_Id);
name = (TextView)findViewById(R.id.demande_Name);
desc = (TextView)findViewById(R.id.demande_Desc);
date = (TextView)findViewById(R.id.demande_Date);
img = (ImageView)findViewById(R.id.picture_demande);
Intent intent = getIntent();
int imagee = intent.getExtras().getInt("Image");
String idd = intent.getExtras().getString("ID");
String namee = intent.getExtras().getString("Name");
String descc = intent.getExtras().getString("Description");
String datee = intent.getExtras().getString("Date");
img.setImageResource(imagee);
id.setText(idd);
name.setText(namee);
desc.setText(descc);
date.setText(datee);
}
}
You can add onItemClickListener just like below
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(this,"You selected : " + position,Toast.LENGTH_SHORT).show();
}
});
You need to set an item listener on the listview
Listview listView = findViewById(R.id.listview);
listView.setOnItemClickListener(new
OnItemClickListener() {
#Override
protected void onListItemClick(ListView l, View v, int
position, long id) {
Log.i("Hello!", "Clicked! YAY!");
});
setOnClickListener on convertView.
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, demandeDetails.class);
intent.putExtra("ID",demandeModelArrayList.get(position).getId());
intent.putExtra("Date",demandeModelArrayList.get(position).getDate());
intent.putExtra("Name",demandeModelArrayList.get(position).getNom());
intent.putExtra("Description",demandeModelArrayList.get(position).getDescription());
intent.putExtra("Image",demandeModelArrayList.get(position).getImgURL());
context.startActivity(intent);
}
});

Updating views within a single row of a ListView when clicked

I was wondering how to update views/UI (say, making a TextView invisible) within a single row of a ListView when clicking on the row because every other rows' views would be affected as well. A good example would be playing a song in Google Play Music, and the equalizer animation would be displayed accordingly. Here are my snippets:
MainActivity's onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Renders only a ListView
ListView songListView = (ListView) findViewById(R.id.list);
List<Song> songList = new ArrayList<>();
for (int i = 1; i <= 200; i++) songList.add(new Song("Song" + i, "Artist" + i));
SongAdapter songAdapter = new SongAdapter(this, songList);
songListView.setAdapter(songAdapter);
songListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
if (position == view.getId()) {
TextView titleText = (TextView) view.findViewById(R.id.song_title);
titleText.setVisibility(View.INVISIBLE);
}
}
});
}
... and my ArrayAdapter's getView method and ViewHolder class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent,
false);
holder = new ViewHolder();
holder.song = (TextView) convertView.findViewById(R.id.song_title);
holder.artist = (TextView) convertView.findViewById(R.id.song_artist);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setId(position);
Song currentSong = getItem(position);
if (currentSong != null) {
holder.song.setText(currentSong.getTitle());
holder.artist.setText(currentSong.getArtist());
}
return convertView;
}
private static class ViewHolder {
TextView song;
TextView artist;
}
Add one more variable in your song model class to make visibility for each row, please try this below code :
class Song{
private boolean isShowing =false;
public boolean isShowing() {
return isShowing;
}
public void setShowing(boolean showing) {
isShowing = showing;
}
}
SongsAdapter.class
class SongAdapter extends BaseAdapter {
private Context mContext;
private List<Song> mSongList;
public SongAdapter(Context context, List<Song> songs) {
mContext = context;
mSongList = songs;
}
#Override
public int getCount() {
return mSongList.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent,
false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Song songModel = mSongList.get(position);
holder.song.setText(songModel.getSongTitle());
holder.artist.setText(songModel.getSongArtist());
if(songModel.isShowing()){
holder.song.setVisibility(View.VISIBLE);
}else{
holder.song.setVisibility(View.GONE);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(songModel.isShowing()){
songModel.setShowing(false);
}else{
songModel.setShowing(true);
}
notifyDataSetChanged();
}
});
return convertView;
}
public class ViewHolder{
TextView song;
TextView artist;
public ViewHolder(View itemView) {
song = (TextView)itemView.findViewById(R.id.song_title);
artist = (TextView)itemView.findViewById(R.id.artist);
}
}
}
Hope this helps:
holder.song.setId(position);
Then your code looks like this:
mSongView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
Log.d(LOG_TAG, "Song item clicked");
TextView titleText = (TextView) view.findViewById(position);
titleText.setVisibility(View.INVISIBLE);
}
});
Add the below code in your adapter class
public interface OnItemClickListener {
void onClick(View view);}
private final OnItemClickListener listener;
public YourAdapter(Context mContext, OnItemClickListener listener) {
this.mContext = mContext;
this.listener = listener;}
Modify your viewholder class
class ViewHolder implements View.OnClickListener {
TextView song;
TextView artist;
ViewHolder(View itemView) {
super(itemView);
song = (TextView)itemView.findViewById(R.id.txt1);
artist = (TextView)itemView.findViewById(R.id.txt2);
itemView.setOnClickListener(this);
}
# Override
public void onClick(View v) {
listener.onClick(v);
}
And lastly implement the interface in your Activity class
class MainActivity extends AppCompatActivity implements YourAdapter.OnItemClickListener{
#Override
public void onClick(View view) {
TextView titleText = (TextView) view.findViewById(R.id.song_title);
titleText.setVisibility(View.INVISIBLE);
}
}
I finally figured this out after getting help from my mentor. Initializing an int field in the adapter to keep record of previous positions in the list did the trick as follows:
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView songListView = (ListView) findViewById(R.id.list);
List<Song> songList = new ArrayList<>();
for (int i = 1; i <= 200; i++) songList.add(new Song("Song" + i, "Artist" + i));
final SongAdapter songAdapter = new SongAdapter(this, songList);
songListView.setAdapter(songAdapter);
songListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
songAdapter.setSongPosition(position);
songAdapter.notifyDataSetChanged();
}
});
}
}
Custom Song class:
public class Song {
private String mSongTitle, mSongArtist;
public Song(String songTitle, String songArtist) {
mSongTitle = songTitle;
mSongArtist = songArtist;
}
public String getSongTitle() {
return mSongTitle;
}
public String getSongArtist() {
return mSongArtist;
}
}
Custom adapter:
public class SongAdapter extends ArrayAdapter<Song> {
private int currentlyPlaying = -1;
public SongAdapter(Context context, List<Song> songList) {
super(context, 0, songList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent,
false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Song song = getItem(position);
holder.song.setText(song.getSongTitle());
holder.artist.setText(song.getSongArtist());
// Hides the row, so both TextViews. Otherwise, sets each row visible.
if (position == currentlyPlaying) convertView.setVisibility(View.INVISIBLE);
else convertView.setVisibility(View.VISIBLE);
return convertView;
}
private class ViewHolder {
private TextView song;
private TextView artist;
private ViewHolder(View itemView) {
song = (TextView) itemView.findViewById(R.id.song_title);
artist = (TextView) itemView.findViewById(R.id.artist);
}
}
public void setSongPosition(int position) {
currentlyPlaying = position;
}
}

how to update a boolean value in server with android swipe cards?

I'm a noob in android studios..and currently in learning stage.. i copied the code from #Nirav-Kalola in this community. I need help in passing a boolean value into the server whenever left or right is swiped in android swipe cards.
Below is the code:
public class MainActivity extends AppCompatActivity implements FlingCardListener.ActionDownInterface{
public static MyAppAdapter myAppAdapter;
public static ViewHolder viewHolder;
private ArrayList<Data> al;
private SwipeFlingAdapterView flingContainer;
public static void removeBackground() {
viewHolder.background.setVisibility(View.GONE);
myAppAdapter.notifyDataSetChanged();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
al = new ArrayList<>();
al.add(new Data("http://www.drodd.com/images15/1-7.jpg", "Hello Anuj"));
al.add(new Data("http://www.drodd.com/images15/2-23.jpg", "Welcome to the final problem"));
al.add(new Data("http://www.drodd.com/images15/3-12.jpg","\t here goes the name of the person "));
al.add(new Data("http://www.drodd.com/images15/4-7.jpg", " buhahahahahahahaha"));
al.add(new Data("http://www.drodd.com/images15/5-18.jpg", "miss me?"));
myAppAdapter = new MyAppAdapter(al, MainActivity.this);
flingContainer.setAdapter(myAppAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
}
#Override
public void onLeftCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
}
#Override
public void onRightCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
}
#Override
public void onScroll(float scrollProgressPercent) {
View view = flingContainer.getSelectedView();
view.findViewById(R.id.background).setAlpha(0);
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
});
// Optionally add an OnItemClickListener
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
#Override
public void onItemClicked(int itemPosition, Object dataObject) {
View view = flingContainer.getSelectedView();
view.findViewById(R.id.background).setAlpha(0);
myAppAdapter.notifyDataSetChanged();
}
});
}
#Override
public void onActionDownPerform() {
Log.e("action", "bingo");
}
public static class ViewHolder {
public static FrameLayout background;
public TextView DataText;
public ImageView cardImage;
}
public class MyAppAdapter extends BaseAdapter {
public List<Data> parkingList;
public Context context;
private MyAppAdapter(List<Data> 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 = getLayoutInflater();
rowView = inflater.inflate(R.layout.item, parent, false);
// 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);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.DataText.setText(parkingList.get(position).getDescription() + "");
Glide.with(MainActivity.this).load(parkingList.get(position).getImagePath()).into(viewHolder.cardImage);
return rowView;
}
}
}
Just do your network call in these two methods
#Override
public void onLeftCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
}
#Override
public void onRightCardExit(Object dataObject) {
al.remove(0);
myAppAdapter.notifyDataSetChanged();
}

Categories

Resources