How can I create List with dynamically number of items with Android Studio

Viewed 280

I created list using example: Material design suggestions for lists with avatar, text and icon

I want to have list with upcoming movies, so I need list with dynamically number of items. I dont understand and I dont know how I should do it.

There is my code for one item list:

public class FragmentOne extends Fragment {

    TextView textView;
    TextView textView2;
    ImageView imageView;

    public static FragmentOne newInstance() {
        FragmentOne fragment = new FragmentOne();
        return fragment;
    }

    public class MovieTask extends AsyncTask<Void, Void, MovieDb>{
        @Override
        protected MovieDb doInBackground(Void... voids) {
            TmdbMovies movies = new TmdbApi("my_api_key").getMovies();
            MovieDb movie = movies.getMovie(5335, "en");

            return movie;
        }

        @Override
        protected void onPostExecute(MovieDb movieDb) {
            textView.setText(movieDb.getOriginalTitle());
            textView2.setText(movieDb.getReleaseDate());
            Glide.with(imageView).load("https://image.tmdb.org/t/p/w500" + movieDb.getPosterPath()).into(imageView);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        MovieTask mt = new MovieTask();
        mt.execute();
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View returnView = inflater.inflate(R.layout.fragment_one, container, false);
        textView = (TextView) returnView.findViewById(R.id.text);
        textView2 = (TextView) returnView.findViewById(R.id.text2);
        imageView = (ImageView) returnView.findViewById(R.id.list_icon);


        return returnView;
    }
}

and xml for that class

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    tools:context=".FragmentOne">

    <ImageView
        android:id="@+id/list_icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="16dp" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_toRightOf="@+id/list_icon"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true">

        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingRight="16dp" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingRight="16dp" />

    </LinearLayout>

</RelativeLayout>
1 Answers
Related