xml ListView CustomAdapter + Intent Serializable

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml ListView CustomAdapter + Intent Serializable相关的知识,希望对你有一定的参考价值。

public class CustomAdapter extends BaseAdapter {

    Activity activity;
    ArrayList<Movie> data;
    LayoutInflater layoutInflater;

    public CustomAdapter(Activity activity, ArrayList<Movie> data) {
        this.activity = activity;
        this.data = data;
        layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View root = convertView;
        if(root == null){
            root = layoutInflater.inflate(R.layout.movie_item,null);
        }
        ImageView movie_poster = root.findViewById(R.id.movie_poster);
        TextView movie_title = root.findViewById(R.id.movie_title);
        TextView movie_rating = root.findViewById(R.id.movie_rating);
        TextView movie_genres = root.findViewById(R.id.movie_genres);
        TextView movie_year = root.findViewById(R.id.movie_year);

        movie_poster.setImageResource(data.get(position).getPoster());
        movie_title.setText(data.get(position).getTitle());
        movie_rating.setText(Double.toString(data.get(position).getRating()));
        movie_year.setText(Integer.toString(data.get(position).getYear()));

        StringBuilder genres = new StringBuilder();
        ArrayList<String> genresArrayList = data.get(position).getGenres();
        for (int i=0;i<genresArrayList.size();i++){
            genres.append(genresArrayList.get(i));
            if(i+1!=genresArrayList.size()){
                genres.append(", ");
            }
        }
        movie_genres.setText(genres.toString());
        return root;
    }
}
public class Movie implements Serializable {
    private int id;
    private String title;
    private int poster;
    private double rating;
    private ArrayList<String> genres;
    private int year;
    private String description;

    public Movie(int id, String title, int poster, double rating, ArrayList<String> genres, int year, String description) {
        this.id = id;
        this.title = title;
        this.poster = poster;
        this.rating = rating;
        this.genres = genres;
        this.year = year;
        this.description = description;
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ListView movieListView = findViewById(R.id.moviewListView);
        final ArrayList<Movie> movies = new ArrayList<>();


        movies.add(new Movie(1,"The Shawshank Redemption", R.drawable.shawshank,9.3,new ArrayList<String>(Arrays.asList("Drama")),1994,"Chronicles the experiences of a formerly successful banker as a prisoner in the gloomy jailhouse of Shawshank after being found guilty of a crime he did not commit. The film portrays the man's unique way of dealing with his new, torturous life; along the way he befriends a number of fellow prisoners, most notably a wise long-term inmate named Red."));

        movies.add(new Movie(2,"The Godfather",R.drawable.godfather,9.2,new ArrayList<String>(Arrays.asList("Crime","Drama")),1972,"When the aging head of a famous crime family decides to transfer his position to one of his subalterns, a series of unfortunate events start happening to the family, and a war begins between all the well-known families leading to insolence, deportation, murder and revenge, and ends with the favorable successor being finally chosen."));

        movies.add(new Movie(1,"The Shawshank Redemption", R.drawable.shawshank,9.3,new ArrayList<String>(Arrays.asList("Drama")),1994,"Chronicles the experiences of a formerly successful banker as a prisoner in the gloomy jailhouse of Shawshank after being found guilty of a crime he did not commit. The film portrays the man's unique way of dealing with his new, torturous life; along the way he befriends a number of fellow prisoners, most notably a wise long-term inmate named Red."));

        movies.add(new Movie(2,"The Godfather",R.drawable.godfather,9.2,new ArrayList<String>(Arrays.asList("Crime","Drama")),1972,"When the aging head of a famous crime family decides to transfer his position to one of his subalterns, a series of unfortunate events start happening to the family, and a war begins between all the well-known families leading to insolence, deportation, murder and revenge, and ends with the favorable successor being finally chosen."));

        movies.add(new Movie(1,"The Shawshank Redemption", R.drawable.shawshank,9.3,new ArrayList<String>(Arrays.asList("Drama")),1994,"Chronicles the experiences of a formerly successful banker as a prisoner in the gloomy jailhouse of Shawshank after being found guilty of a crime he did not commit. The film portrays the man's unique way of dealing with his new, torturous life; along the way he befriends a number of fellow prisoners, most notably a wise long-term inmate named Red."));

        movies.add(new Movie(2,"The Godfather",R.drawable.godfather,9.2,new ArrayList<String>(Arrays.asList("Crime","Drama")),1972,"When the aging head of a famous crime family decides to transfer his position to one of his subalterns, a series of unfortunate events start happening to the family, and a war begins between all the well-known families leading to insolence, deportation, murder and revenge, and ends with the favorable successor being finally chosen."));

        movies.add(new Movie(1,"The Shawshank Redemption", R.drawable.shawshank,9.3,new ArrayList<String>(Arrays.asList("Drama")),1994,"Chronicles the experiences of a formerly successful banker as a prisoner in the gloomy jailhouse of Shawshank after being found guilty of a crime he did not commit. The film portrays the man's unique way of dealing with his new, torturous life; along the way he befriends a number of fellow prisoners, most notably a wise long-term inmate named Red."));

        movies.add(new Movie(2,"The Godfather",R.drawable.godfather,9.2,new ArrayList<String>(Arrays.asList("Crime","Drama")),1972,"When the aging head of a famous crime family decides to transfer his position to one of his subalterns, a series of unfortunate events start happening to the family, and a war begins between all the well-known families leading to insolence, deportation, murder and revenge, and ends with the favorable successor being finally chosen."));




        CustomAdapter customAdapter = new CustomAdapter(this,movies);

        movieListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this,MovieDescription.class);
                intent.putExtra("movie",movies.get(position));
                startActivity(intent);
            }
        });

        movieListView.setAdapter(customAdapter);


    }
}
public class MovieDescription extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);

        Bundle bundle = getIntent().getExtras();

        if(bundle!=null){
            Movie movie = (Movie) bundle.getSerializable("movie");

            TextView selected_movie_description = findViewById(R.id.selected_movie_description);
            TextView selected_movie_title = findViewById(R.id.selected_movie_title);
            ImageView selected_movie_poster = findViewById(R.id.selected_movie_poster);

            selected_movie_description.setText(movie.getDescription());
            selected_movie_title.setText(movie.getTitle());
            selected_movie_poster.setImageResource(movie.getPoster());
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/moviewListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>
<?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="5dp">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/movie_poster"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_margin="5dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <TextView
            android:textStyle="bold"
            android:text="Movie Title"
            android:textColor="#000"
            android:textSize="18sp"
            android:id="@+id/movie_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:text="Rating: "
            android:textColor="#000"
            android:textSize="14sp"
            android:id="@+id/movie_rating"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:textStyle="bold"
            android:text="Movie genres"
            android:textSize="12sp"
            android:id="@+id/movie_genres"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:text="Year"
            android:textSize="12sp"
            android:id="@+id/movie_year"
            android:gravity="end"
            android:layout_marginRight="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MovieDescription"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="20dp">

        <ImageView
            android:layout_margin="10dp"
            android:id="@+id/selected_movie_poster"
            android:layout_width="200dp"
            android:layout_height="200dp" />

        <TextView
            android:id="@+id/selected_movie_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            />

    </LinearLayout>




    <TextView
        android:textColor="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Movie Description:"
        android:textSize="24dp"
        android:layout_marginBottom="12dp"/>

    <TextView
        android:id="@+id/selected_movie_description"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:textSize="18dp" />


</LinearLayout>

以上是关于xml ListView CustomAdapter + Intent Serializable的主要内容,如果未能解决你的问题,请参考以下文章

android listview中的文字大小能在string.xml里设置吗

删除 ListView 分隔符(在 xml 布局文件中)[重复]

在WPF的ListView中用xml绑定了数据源,怎么在xml中的内容变化后让ListView中的视图实时改变

xml wapmass-Android的ListView控件,DetailsActivity.xml

Android XML解析listview

如何在 C# WPF 中从 ListView/XML 中完全删除一个项目?