Customised listview of objects in Android - android

I am beginner of Android programming and need little help.
I have an array of objects that should be shown in listview. Here is a code of class:
public class Car {
public String img;
public String thumbnail;
public String manufacturer;
public String model;
public int price;
public String description;
}
Thumbnail is a string variable that contains URL of thumbnail to be shown.
I also have array of a few Car objects called "cars" declared in my DataStorage class.
I want only 2 things of every object to be shown in my customized listview: thumbnail (from internet) and model.
I created layout item.xml, which should represent one item of listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#android:color/holo_blue_dark">
<ImageView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:text="left"
android:id="#+id/image_tmb"
android:layout_gravity="left|center_vertical"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:paddingBottom="10dp"
android:id="#+id/name"
android:layout_weight="1"
android:textSize="23dp"/>
</LinearLayout>
</LinearLayout>
TextView should be filled with model and Imageview should be filled with thumbnail.
I also created class MyAdapter which should fill listview:
public class MyAdapter extends BaseAdapter {
private Context myContext;
public MyAdapter(Context context) {
this.myContext = context;
}
#Override
public int getCount() {
return DataStorage.cars.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
//Here I should customize my view but don't know how.
}
I created listview element in activity_main.xml and delcared it in MainActivity:
ListView lv=(ListView) findViewById(R.id.lv);
So, how would I do my task?

A simple pattern would look something like this
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
// Inflate the view if it doesn't already exist
if (view == null) {
LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
}
// Get your current Car object
Car c = getItem(i);
// Initialize the views
ImageView image = (ImageView) view.findViewById(R.id.image_tmb);
TextView name = (TextView) view.findViewById(R.id.name);
// Fill the views according to the car's properties
/* NOTE: I'm not going to go in depth to explain how to set
an ImageView image from a URL, as it is an entirely different question,
so I'll put a link to this library below */
Picasso.with(myContext).load(c.thumbnail).into(image);
name.setText(c.model);
}
You'll need to modify getItem to actually get the item, instead of returning null..
#Override
public Object getItem(int position) {
return DataStorage.cars[position];
}
Then, set up your adapter in your Activity/Fragment/Whatever, like this
MyAdapter mAdapter = new MyAdapter(this);
lv.setAdapter(mAdapter);
If you're going to be doing a lot with images and don't want to write your own implementation, Picasso is a really easy library to use.

Related

Custom Listview containing checkedtextview

I implement custom listview containing a checked textview. My wish is to change the state of the checkbox on click, but it seems not to be simple, as I thought it would be. Additionally I would like to check the checkboxes of items, that are stored in database, how could it be done? At the moment I have an activity which shows the elements, handles the click on the checkbox (not the list item!), but canĀ“t change the checkbox status by items stored in database.
This is my custom list item:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<CheckedTextView
android:id="#+id/name"
android:layout_width="409dp"
android:layout_height="30dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingStart="5dp"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/type"
android:layout_width="409dp"
android:layout_height="34dp"
android:paddingStart="5dp"
android:textSize="18sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/name" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is my adapter:
public class CustomAdapter extends BaseAdapter
{
private List<CustomElement> elems;
private LayoutInflater inflater;
public HueBulbAdapter(Context ctx, List<CustomElement> elems)
{
this.elems = elems;
inflater = LayoutInflater.from(ctx);
}
#Override
public int getCount()
{
return elems.size();
}
#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)
{
ConstraintLayout result = (ConstraintLayout) inflater.inflate(R.layout.custom_elems, parent, false);
CheckedTextView name = result.findViewById(R.id.name);
TextView type = result.findViewById(R.id.type);
CustomElement elem = elems.get(position);
name.setText(elem.getName());
type.setText(elem.getType());
result.setTag(position);
toggle(name);
return result;
}
private void toggle(CheckedTextView ctv) {
ctv.setOnClickListener(v -> {
ctv.setChecked(!ctv.isChecked());
});
}
}
And this is my activity:
[...]
elemsView.setOnItemClickListener((parent, view, position, id) ->
{
if (!selected.containsKey(elemList.get(position).getName()))
{
selected.put(elemList.get(position).getName(), elemList.get(position));
} else
{
selected.remove(elemList.get(position).getName());
}
});
[...]
Maybe I am using wrong components to reach my target? Any Ideas how to do it this way or in a better way?
Thanks for your help!
Few suggestions:
Add a boolean variable to the CustomElement model to track if item is checked or not checked.
Add private boolean isChecked; and generate getter and setter for it.
In adapter class, use public Object getItem(int position) to return item in list, and not null.
Change to return elems.get(position);
In adapter class, replace toggle(name) with:
name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
elem.setChecked(!elem.isChecked()); // toggle
name.setChecked(elem.isChecked());
}
});
In your activity class, to access the updated list, use this:
for (int i = 0; i < mAdapter.getCount(); i++) {
CustomElement element = (CustomElement) mAdapter.getItem(i);
if (element.isChecked()) {...} else {...}
}
Optional. Search and implement ViewHolder Pattern in adapter class to improve the loading speed of ListView items.

getView() method of custom adapter never gets called

I've read various SO threads on the topic but none of them seem to apply to my code.
I'm trying to populate a fragment with a ListView with my custom NearbyAdapter. However, my getView() method never gets called (as demonstrated by my logs not showing up). The view itself seems to be appropriately attached to my fragment, as demonstrated by the button in the view showing up, but not the ListView.
Relevant NearbyListFragment.java code:
public class NearbyListFragment extends ListFragment {
private int mImageSize;
private boolean mItemClicked;
private NearbyAdapter mAdapter;
private List<Place> places;
private LatLng mLatestLocation;
private static final String TAG = "NearbyListFragment";
public NearbyListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "NearbyListFragment created");
View view = inflater.inflate(R.layout.fragment_nearby, container, false);
return view;
}
//TODO: Do asynchronously?
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//Load from data source (NearbyPlaces.java)
mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation();
//FIXME: Hardcoding mLatestLocation to Michigan for testing
//mLatestLocation = new LatLng(44.182205, -84.506836);
places = loadAttractionsFromLocation(mLatestLocation);
mAdapter = new NearbyAdapter(getActivity(), places);
ListView listview = (ListView) view.findViewById(R.id.listview);
//setListAdapter(mAdapter);
listview.setAdapter(mAdapter);
Log.d(TAG, "Adapter set to ListView");
mAdapter.notifyDataSetChanged();
}
private class NearbyAdapter extends ArrayAdapter {
public List<Place> placesList;
private Context mContext;
public NearbyAdapter(Context context, List<Place> places) {
super(context, R.layout.item_place);
mContext = context;
placesList = places;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Place place = (Place) getItem(position);
//FIXME: This never gets called
Log.d(TAG, "Place " + place.name);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_place, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvDesc = (TextView) convertView.findViewById(R.id.tvDesc);
// Populate the data into the template view using the data object
tvName.setText(place.name);
tvDesc.setText(place.description);
// Return the completed view to render on screen
return convertView;
}
#Override
public long getItemId(int position) {
return position;
}
The layout file of the fragment, fragment_nearby.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/btn_New">
</ListView>
<Button
android:id="#+id/btn_New"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:text="Button"
android:width="170dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
And the layout file of the item, item_place.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="#+id/tvDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Desc" />
</LinearLayout>
Edit: Does anyone want to actually include a reason for the downvote? Especially when something like Custom Adapter for List View has 129 upvotes?
The issue is that ArrayAdapter does not know about List places:
Use this to fix it:
private static class NearbyAdapter extends ArrayAdapter<Place> {
public NearbyAdapter(Context context, List<Place> places) {
super(context, R.layout.item_place, places);
mContext = context;
placesList = places;
}
}
P/s: in this case, I think you need more control to set your place data to your view. Consider using BaseAdapter instead of ArrayAdapter.
Add following to your adapter:
#Override
public int getCount() {
return placesList.size();
}
After this you most likely encounter error with getItem so you will need to override that as well to return your object from the list.
You have to override getCount() method in ArrayAdapter to initialized listview like this:
#Override
public int getCount() {
return placesList.size();
}

Image slider using View Pager issue

I have an activity which contains a listView. Each item of the listView contains a swipeable set of images (which I have unsuccessfully tried to implement using a ViewPager).
My issue is this: when I try to implement a simple image slider using a view pager (i.e. Activity contains a ViewPager, and the View Pager's adapter supplies the images), the output is as expected, but if I try doing what I have mentioned in the previous paragraph (i.e. The Activity contains a listView and each item of the listView is a ViewPager which displays a swipeable set of images), I get a blank output. Please help me out! I have posted some code below:
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.stylistDisplay);
//To keep things simple I am sending only one item of the list to the adapter
list.setAdapter(new StylistAdapter(Cart.getList().get(0), this));
}
static class StylistAdapter extends BaseAdapter {
Stylist stylistObj;
Context context;
LayoutInflater inflater;
public StylistAdapter(Stylist obj, Context context) {
this.stylistObj = obj;
this.context = context;
inflater = ((Activity)this.context).getLayoutInflater();
}
#Override
public int getCount() {
return 1;
}
#Override
public Object getItem(int position) {
return stylistObj;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewItem item;
if (convertView == null) {
convertView = inflater.inflate(R.layout.stylist_photos_view_pager, null);
item = new ViewItem();
item.photosViewPager = (ViewPager) convertView.findViewById(R.id.photos_view_pager);
convertView.setTag(item);
} else {
item = (ViewItem) convertView.getTag();
}
PhotosAdapter adapter = new PhotosAdapter(context);
item.photosViewPager.setAdapter(adapter);
return convertView;
}
private class ViewItem {
ViewPager photosViewPager;
}
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.temp.customer.MainActivity">
<ListView
android:id="#+id/stylistDisplay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:background="#color/backGround"
android:padding="13.3dp"
android:clickable="true"
android:dividerHeight="5dp" />
</FrameLayout>
stylist_photos_view_pager
<RelativeLayout xmlns:android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/photos_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
PhotosAdapter.java
public class PhotosAdapter extends PagerAdapter {
private Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
public PhotosAdapter(Context context) {
this.context = context;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View viewItem = inflater.inflate(R.layout.stylist_individual_details, container, false);
ImageView imageView = (ImageView) viewItem.findViewById(R.id.iv1);
imageView.setImageResource(GalImages[position]);
TextView textView1 = (TextView) viewItem.findViewById(R.id.tv1);
textView1.setText("hello world");
((ViewPager)container).addView(viewItem);
return viewItem;
}
#Override
public int getCount() {
return 3;
}
#Override
public boolean isViewFromObject(View view, Object object) {
boolean temp = view == ((LinearLayout) object);
return temp;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((LinearLayout) object);
}
}
I've experienced similar issue, and have to say that it is a bad idea to use ViewPager as a listItem, since ViewPager is supposed to be used for fragments primarily.
I guess that your functionality should be similar to some horizontal pictures, on the main feed which is vertical. You can think of using horizontal scroll views which is a bit of a pain, but still more realistic to do that. You might also end up managing your own scrolling behavior, which might already be implemented by some libraries.
BUT I MIGHT BE WRONG!
This thread has a similar issue:
Placing ViewPager as a row in ListView
After reading around for a while I tried explicitly setting the height of the viewPager and its parent. This worked for me.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="200dip" >
</android.support.v4.view.ViewPager>
</LinearLayout>
I don't know if there is a better solution, but if you do get a better solution please comment!

Problems with the method setChoiceMode () in my ListView

Please note, this is really weird.
For some reason, the method setChoiceMode (ListView.CHOICE_MODE_SINGLE) no results.
I use it like this:
list = (ListView) findViewById(R.id.list);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setOnItemClickListener(OnItemClickListenerObject);
list.setAdapter(adapter);
The fact is that after I put the method setChoiceMode (), nothing has changed, RadioButtons not appeared.
I'm using a custom adapter and I have no problems with it? But I do not understand why Radiobuttons not shown.
Any ideas? (If you need additional code, ask and I'll post it.)
My adapter code:
public class ContactAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<Contact> contacts;
private View view;
public ContactAdapter(Context context, ArrayList<Contact> contacts) {
this.contacts = contacts;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return contacts.size();
}
#Override
public Object getItem(int position) {
return contacts.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private Contact getContact(int position) {
return (Contact) getItem(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.contact_item, parent, false);
}
Contact c = getContact(position);
((TextView) view.findViewById(R.id.lv_name)).setText(c.getName() + " " + c.getSurname());
((ImageView) view.findViewById(R.id.lv_img)).setImageBitmap(c.getPhoto());
return view;
}
}
Below shows the layout that I use for ListView item.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
<ImageView
android:id="#+id/lv_img"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#drawable/default_user"
android:layout_weight="0"/>
<TextView
android:id="#+id/lv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="16dp"
android:layout_gravity="center_vertical"
android:textSize="24sp"/>
</LinearLayout>
Setting the choice mode does not automatically make radio buttons appear. All it does is add behavior which toggles a ListView row's Checkable or activation state. It's still up to the row's View to decide how to react to that. You'll need to create your own layout to inflate for a row that supports the activation state or the Checkable interface.
Android does provide some simple pre-made views that you can use. Here's just a couple you can choose from:
android.R.layout.simple_list_item_activated_1.xml
android.R.layout.simple_list_item_checked.xml

Horizontal scrolling in android gridview

I have a grid view in my application and i need to scroll it horizontally.I have tried changing the gridview to gallery.But then only one row is available,but i need different rows as in a grid view.So basically what i need is a gridview that can be scrolled horizontally.Is there any efficient way to do this?Thanks in advance.
Regards
Anu
Hi,Thanks for the reply.i have tried using a Gallery and implement an adapter that provides a multirow view in its getView method.
My java file is:
public class Gridview extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new GridAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(Gridview.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class GridAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
R.drawable.icon,
};
public GridAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null)
{
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
}
else
{
v = convertView;
}
return v;
}
}
}
main.xml is :
<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
icon.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/icon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</ImageView>
</LinearLayout>
The output i got is : http://www.4shared.com/photo/MzUcmzel/device.html
But this is not i need actually.i want the icons in different rows.Any help is appreciated.
I think your best bet is to use a Gallery and implement an adapter that provides a multirow view in its getView method. See Hello Gallery and look at the ImageAdapter implementation within.
Instead of the ImageView that getView returns in that example, you can, for example, inflate your own custom layout, for example a LinearLayout with vertical orientation.
You might consider a TableLayout inside a HorizontalScrollView, but the disadvantage there is that everything will be in memory at once, making it difficult to scale to lots of items. The Gallery recycles Views and offers some resource advantages.
I have already posted this answer here and here, but these questions are
identical...
There is a nice solution in Android from now on : HorizontalGridView.
1. Gradle dependency
dependencies {
compile 'com.android.support:leanback-v17:23.1.0'
}
2. Add it in your layout
your_activity.xml
<!-- your stuff before... -->
<android.support.v17.leanback.widget.HorizontalGridView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:id="#+id/gridView"
/>
<!-- your stuff after... -->
3. Layout grid element
Create a layout for your grid element ( grid_element.xml ). I have created a simple one with only one button in it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
</LinearLayout>
4. Create an adapter
Highly inspired by this link : https://gist.github.com/gabrielemariotti/4c189fb1124df4556058
public class GridElementAdapter extends RecyclerView.Adapter<GridElementAdapter.SimpleViewHolder>{
private Context context;
private List<String> elements;
public GridElementAdapter(Context context){
this.context = context;
this.elements = new ArrayList<String>();
// Fill dummy list
for(int i = 0; i < 40 ; i++){
this.elements.add(i, "Position : " + i);
}
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
public final Button button;
public SimpleViewHolder(View view) {
super(view);
button = (Button) view.findViewById(R.id.button);
}
}
#Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(this.context).inflate(R.layout.grid_element, parent, false);
return new SimpleViewHolder(view);
}
#Override
public void onBindViewHolder(SimpleViewHolder holder, final int position) {
holder.button.setText(elements.get(position));
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "Position =" + position, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemCount() {
return this.elements.size();
}
}
5. Initialize it in your activity :
private HorizontalGridView horizontalGridView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
horizontalGridView = (HorizontalGridView) findViewById(R.id.gridView);
GridElementAdapter adapter = new GridElementAdapter(this);
horizontalGridView.setAdapter(adapter);
}
True about using a Gallery or ListView re: re-using views. But if your list is not too long, you can try a TableLayout with a ViewFlipper. Here's a summary of possible options/solutions.
This post might get help you out what you wanted to achieve
Scroll like Shelf View
how about using a viewPager :
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
?
Try to wrap it in HorizontalScrollView

Categories

Resources