How to do Multiple Choice Using GridView android? - android

This is how I do my Gridview with adapter, can someone tell me how can I modify these code to get the multiple choice effect ? Following is my code, kindly advise and thousand of thanks provided.
//privateList<Condiment> CondimentList;
CondimentList = getCondimentDescription(productcode);
condimentGrid.setAdapter(new condimentlist());
public class condimentlist extends BaseAdapter {
LayoutInflater mInflater;
public condimentlist() {
mInflater = LayoutInflater.from(cart_list.this);
}
#Override
public int getCount() {
return CondimentList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.condiment_item, null);
}
Condiment myObj = CondimentList.get(position);
Button CondimentItem = (Button) convertView.findViewById(R.id.condimentItem);
CondimentItem.setText("" +myObj.getCondimentDescription());
return convertView;
}
}

Just little addition in your code provide selected value in your Condiment class model and use like this.
//privateList<Condiment> CondimentList;
CondimentList = getCondimentDescription(productcode);
condimentGrid.setAdapter(new condimentlist());
public class condimentlist extends BaseAdapter {
LayoutInflater mInflater;
public condimentlist() {
mInflater = LayoutInflater.from(cart_list.this);
}
#Override
public int getCount() {
return CondimentList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.condiment_item, null);
}
final Condiment myObj = CondimentList.get(position);
Button CondimentItem = (Button) convertView.findViewById(R.id.condimentItem);
CondimentItem.setText("" +myObj.getCondimentDescription());
CondimentItem.setSelected(myObj.isSelected());
CondimentItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myObj.setSelected(!myObj.isSelected());
v.setSelected(myObj.isSelected());
}
});
return convertView;
}
}

Related

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 FlipView Scrolling Back and Front Page with Button

I want to flip between two fragment with button click.and stop touch action.
using
android-flip,
issue link.
Sample of my code
public class FlipFragmeent extends Fragment{
private int currentFragmentIndex;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
flipView = new FlipViewController(inflater.getContext(),
FlipViewController.HORIZONTAL);*emphasized text*
//flipView.setFlipByTouchEnabled(false);
flipView.setAdapter(new BaseAdapter() {
#Override
public int getCount() {
return 4;
}
#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) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) battleActivity
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(
R.layout.fragment_next, null);
((Button)convertView.findViewById(R.id.next)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
flipView.refreshPage(currentFragmentIndex);
currentFragmentIndex++;
if(currentFragmentIndex == 4)
currentFragmentIndex = 0;
}
}));
return convertView;
}
});
return flipView;
}
BUT that does not work any help?

Gridview custom adapter button onclick

My gridview custom item is a image button.I want to change image button image when the image button is clicked.You can see below my custom adapter class getView method.But image button image does not change.
ImageButton btn1;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.myItem, null);
btn1 = (ImageButton) view.findViewById(R.id.btn1);
}
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn1.setImageResource(R.id.image1);
}
});
return view;
}
Try this way,hope this will help you to solve your problem.
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String,Integer>> imagesList;
public CustomGridAdapter(Context context,ArrayList<HashMap<String,Integer>> imagesLis) {
this.context =context;
this.imagesList =imagesLis;
}
#Override
public int getCount() {
return imagesList.size();
}
#Override
public Object getItem(int position) {
return imagesList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.myItem, null);
holder.btn1 = (TextView) convertView.findViewById(R.id.btn1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btn1.setImageResource(imagesList.get(position).get("normalImage"));
holder.btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ImageButton)v).setImageResource(imagesList.get(position).get("selectedImage"));
}
});
return convertView;
}
static class ViewHolder {
ImageButton btn1;
}
}

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;
}
}

Optimize listview performance Android

I have a listView (vertical) and every list item has a horizontal list view (horizontal).
But the problem is when i scroll the horizontal scrollview in the row the vertical list is also calling getView()...
So, there is a huge performance hit..
So , can any one tell me a better solution to it ..
public class GridViewAdapter extends BaseAdapter {
List<List<Hotel>> gridDatasource;
Context mContext;
public GridViewAdapter(List<List<Hotel>> gridDatasource, Context context) {
this.gridDatasource = gridDatasource;
this.mContext = context;
}
public void setGridDatasource(List<List<Hotel>> gridDatasource) {
this.gridDatasource = gridDatasource;
}
#Override
public int getCount() {
if (gridDatasource == null) {
return 0;
}
return gridDatasource.size();
}
#Override
public Object getItem(int position) {
return gridDatasource.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_table_cell,
null);
holder = new GridViewHolder();
holder.headerView = (TextView) convertView
.findViewById(R.id.gridViewRowHeader);
holder.listView = (HorizontalListView) convertView
.findViewById(R.id.gridViewHorizontalListView);
convertView.setTag(holder);
} else {
holder = (GridViewHolder) convertView.getTag();
Log.d("TAG", "Reaching Here");
}
holder.headerView.setText("Hello From Sandeep");
HorizontalListViewAdapter adapter = new HorizontalListViewAdapter(
mContext, gridDatasource.get(position));
holder.listView.setAdapter(adapter);
return convertView;
}
}
static class GridViewHolder {
TextView headerView;
HorizontalListView listView;
}
public class HorizontalListViewAdapter extends BaseAdapter {
Context mContext;
List<Hotel> mHotels;
public HorizontalListViewAdapter(Context context, List<Hotel> hotels) {
this.mContext = context;
this.mHotels = hotels;
}
#Override
public int getCount() {
if (mHotels == null) {
return 0;
}
return mHotels.size();
}
#Override
public Object getItem(int position) {
return mHotels.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HotelCell cell = (HotelCell) convertView;
if (cell == null) {
cell = new HotelCell(mContext);
} else {
Log.d("TAG", "Reached here 2");
}
cell.setHotel(mHotels.get(position));
cell.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext,
HotelDetailActivity.class);
intent.putExtra("DATA", ((HotelCell) v).getHotel());
startActivity(intent);
}
});
cell.setPadding(0, 0, 10, 0);
return cell;
}
}
Dear i suggest to try My this Code
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder holder;
if (v == null)
{
v = inflater.inflate(R.layout.custom_image_layout, null);
holder = new ViewHolder();
holder.txtFileName = (TextView) v.findViewById(R.id.txtFileName);
holder.imageView = (ImageView) v.findViewById(R.id.imgView);
v.setTag(holder);
} else
{
holder = (ViewHolder) v.getTag();
}
holder.imageView.setImageBitmap(bm);
holder.txtFileName.setText(""+nameoffile);
return v;
}
static class ViewHolder
{
public ImageView imageView;
public TextView txtFileName;
}
Use The Holder Class

Categories

Resources