所以我想将数据库中的电影列表打印到ListView
并使用ArrayAdapter
,但是当我运行该应用时,它会给我这个。< / p>
我认为问题出在ArrayAdapter
或ListView
:
代码:
我的模特课;
public class Movie
{
private int id;
private String name;
private String genre;
private int year;
public long getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getGenre()
{
return genre;
}
public void setGenre(String genre)
{
this.genre = genre;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
}
在我的DBHandle中,我有;
private Movie cursorToMovie (Cursor cursor)
{
Movie movie = new Movie();
movie.setId(cursor.getInt(0));
movie.setName(cursor.getString(1));
movie.setGenre(cursor.getString(2));
movie.setYear(cursor.getInt(3));
return movie;
}
public List<Movie> getAllMovies()
{
open();
List<Movie> movieList = new ArrayList<>();
Cursor cursor = db.query(Movie.TABLE_NAME, MovieAllColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
Movie movie = cursorToMovie(cursor);
movieList.add(movie);
cursor.moveToNext();
}
cursor.close();
close();
return movieList;
}
在我的MainActivity中,我(我认为,在GetAllMovies
方法中)
public class MainActivity extends AppCompatActivity
{
MoviesRepository moviesRepository;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetAllMovies();
}
public void GetAllMovies()
{
moviesRepository = new MoviesRepository(this);
moviesRepository.open();
List<Movie> movieList = moviesRepository.getAllMovies(); // Gets all Movies from the database
ArrayAdapter<Movie> adapter = new ArrayAdapter<Movie>(this, android.R.layout.simple_list_item_1, movieList);
ListView listView = (ListView) findViewById(R.id.ListViewMovie);
listView.setAdapter(adapter);
}
}
旁注,当我调试它时,我可以看到movieList
正在填充moviesRepository.getAllMovies()
返回值,我可以看到所有电影&#39;姓名,流派和年份。
答案 0 :(得分:2)
通用ArrayAdapter和android.R.layout.simple_list_item_1用于字符串列表而不是电影列表。看起来它正在拍摄每个Movie对象并调用.toString()并将其放在列表视图中。
您有几个选择,具体取决于您需要做什么。
希望这有帮助!
答案 1 :(得分:2)
如果您使用String列表作为示例,那么使用默认ArrayAdapter不是问题。 但是如果你有一个复杂的对象,你必须创建自定义ArrayAdapter并覆盖getView方法。 看看我的例子,这将解决你的问题。
public class MovieAdapter extends ArrayAdapter<Movie> {
private List<Movie> moviesList;
public MovieAdapter(Context context, int resource, List<Movie> moviesList) {
super(context, resource);
this.moviesList = moviesList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Movie movie = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.your_row_layout, parent, false);
}
TextView movieTitle = (TextView) convertView.findViewById(R.id.tvTitle);
TextView movieSubTitle = (TextView) convertView.findViewById(R.id.tvSubTitle);
movieTitle.setText(movie.getTitle);
movieSubTitle.setText(movie.getSubTitle);
return convertView;
}
@Override
public int getCount() {
return moviesList.size();
}
}
希望它有所帮助!
答案 2 :(得分:1)
您需要为ListView的每一行创建一个新布局。例如,在layouts文件夹中创建一个名为movie_item.xml的布局文件,然后在
中创建视图时对其进行充气。# WickedPDF Global Configuration
#
# Use this to set up shared configuration options for your entire application.
# Any of the configuration options shown here can also be applied to single
# models by passing arguments to the `render :pdf` call.
#
# To learn more, check out the README:
#
# https://github.com/mileszs/wicked_pdf/blob/master/README.md
WickedPdf.config = {
# Path to the wkhtmltopdf executable: This usually isn't needed if using
# one of the wkhtmltopdf-binary family of gems.
# exe_path: 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
}
基本上,您需要一个自定义布局和适配器。在这种情况下,一个简单的ArrayAdapter(仅用于字符串)不会帮助你。
答案 3 :(得分:0)
你的解决方案绝对是一种方法,但我认为让你拥有自己的arrayAdapter是很麻烦的。我找到的解决方案是你必须让你拥有电影类中的字符串。
public String toString()
{
return name + ", " + genre + ", " + year;
}
这有效:)我现在开心