-4

Let me see if I can explain my problem properly... The thing that I would do is : I'm going to ask an API to give me some Images and TextViews, but I don't know how much will it returns. So the question is if is there any way to create something to create as ImageView + textView as the API returns to me. The best example I can say is an APP from offers of a SuperMarket, you don't know how many sales will you get on your App so, I'm wondering if exists anyway easily to do that. By the way, I'm using fragments... the examples that i've seen they are made on Activities.

More or less the example that I'm trying to explain is this :

enter image description here

Thanks.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • make a query that will get first 10 records. And on scroll of listview fetch next 10 record in background and show them – Pavya Jan 21 '15 at 09:02
  • This idea looks good, but do you know any example code to guide me? Thanks. – Skizo-ozᴉʞS ツ Jan 21 '15 at 09:03
  • yes, Its depend on which Db you are using. According to that pass start index and limit so you will get exact that amount of data. – Pavya Jan 21 '15 at 09:05

4 Answers4

3

What you are looking for is a Listview adapter

public class ExampleAdapter extends BaseAdapter {
    List<Product> products;
    private Activity context;

    @Override
    public int getCount() {
        return products.size();
    }

    public ExampleAdapter(List<Product> products, Activity context) {
        this.products = products;
        this.context = context;
    }

    @Override
    public Object getItem(int position) {
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Product product = products.get(position);

        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.row_shopping_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.getProductTextView().setText(product.getName());
        holder.getProductImageView().setText(product.getPicture());

        return convertView;
    }

    private class ViewHolder {
        private final TextView productTextView;
        private final ImageView productImageView;

        private ViewHolder(View wrapperView) {
            productTextView = (TextView) wrapperView.findViewById(R.id.tvName);
            productImageView = (TextView) wrapperView.findViewById(R.id.ivPicture);

        }

        public TextView getProductTextView() {
            return productTextView;
        }

        public TextView getProductImageView() {
            return productImageView;
        }

    }
}

This adapter needs a layout to define what the rows will look like. This will be something like this

<?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">

    <ImageView
        android:layout_gravity="center_vertical"
        android:layout_width="50dp"
        android:id="@+id/ivPicture"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/tvName"
        android:text="Product 1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Lastly in your activity where you want to display the overview you will need to use a listview and set the adapter

final ExampleAdapter adapter = new ExampleAdapter(list, this);
listview.setAdapter(adapter);
MrJM
  • 1,214
  • 1
  • 12
  • 26
2

Use List Fragment or List inside Fragment and set Adapter to Listview which returns size that will be size of your data

For reference:

ListFragment: http://developer.android.com/reference/android/app/ListFragment.html

ListView Wtih Image and Text: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

Anjali
  • 1
  • 1
  • 13
  • 20
2

Every ListView in android uses an adapter. An adapter provides the listview with the the views that should be displayed in the listview.

The adapter uses a data source such as an ArrayList to store the data that should be used to generate these views. Now, an ArrayList does not have a fixed size so you can add/remove/modify items in the ArrayList when you fetch new data from your API and notify the adapter. The adapter will redraw its views accordingly.

The answer provided by MrJM gives an example of how you can write an adapter.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
2

1) If you dont want to hit Db

  • then add your result into list, take first 30 items and display list.
  • then on scroll of listview get next 30 items and call notifydatasetchanged

2) fire query get first 30 items , display list then on scroll fire query take another 30 items and call notifydatasetchanged

in this case just check items are coming from Db is not null.

Pavya
  • 6,015
  • 4
  • 29
  • 42