覆盖 ArrayAdapter 的 getView 以填充 3 个 Textview
Overriding getView of ArrayAdapter to fill 3 Textviews
从我的 Activity,我正在调用我的 MovieTask
扩展 AsyncTask
。
我在 MovieTask
的 doInBackground
方法中成功解析了来自服务器的 Json 响应。
现在,我的 activity 中有一个列表视图,我想使用扩展 ArrayAdapter
.
的 MovieListAdapter
填充列表视图
对于每个视图(即一行),我想填充 3 TextView
s。我正在重写 MovieListAdapter
的 getView
方法来填充相同的内容。
但是我不明白如何将数据从我的 activity 发送到 getView
方法到 getView
方法来填充文本视图?
public class MovieTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
//I HAVE GOT THE JSON DATA THAT CONTAINS AN ARRAY..
//EACH ENTRY IN ARRAY SHOULD BE FILLED IN A SINGLE ROW IN LISTVIEW
getMovieDataFromJson(result);
}
private void getMovieDataFromJson(String JsonString) throws JSONException {
JSONObject jsonObject = new JSONObject(JsonString);
JSONArray results = jsonObject.getJSONArray("results");
for (int i=0; i<results.length(); i++){
String title = results.getJSONObject(i).getString("original_title");
String date = results.getJSONObject(i).getString("release_date");
long id = results.getJSONObject(i).getLong("id");
double vote = results.getJSONObject(i).getDouble("vote_average");
//HERE I NEED TO CALL THE GETVIEW METHOD SO THAT IT FILLS THE ROW OF THE LISTVIEW WITH THESE VALUES - title, date and vote
}
}
MovieListAdapter.java
public class MovieListAdapter extends ArrayAdapter<String > {
public MovieListAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// I NEED TO PREPARE EACH ROW OF LISTVIEW HERE
//EVERY ROW CONTAINS 3 TEXTVIEWS WHICH I NEED TO FILL
//BUT HOW DO I SEND DATA TO FILL THE TEXTVIEW ?
}
}
当您在 AdapterView
上 setAdapter()
例如 ListView
时,getView()
会在内部调用,并且从中返回的视图会填充到 AdapterView
.
您可能想阅读并了解 AdapterView
和 Adapter
的工作原理。继续阅读这里的一些文档:
您需要做的是:
为您的数据创建一个模型,例如:
public class Movie {
public long id;
public String date;
public String title;
public double vote;
public Movie(long id, String date, String title, double vote) {
this.id = id;
this.date = date;
this.title = title;
this.vote = vote;
}
}
创建布局以显示电影详细信息,例如:
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/lbl_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lbl_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lbl_vote"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
更改您的适配器以处理 Movie
而不是 String
,例如:
public class MovieListAdapter extends ArrayAdapter<Movie > {
public MovieListAdapter(Context context, List<Movie> objects) {
super(context, 0, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.movie_item, parent, false);
}
((TextView) convertView.findViewById(R.id.lbl_date))
.setText(getItem(position).date);
((TextView) convertView.findViewById(R.id.lbl_title))
.setText(getItem(position).title);
((TextView) convertView.findViewById(R.id.lbl_vote))
.setText(String.valueOf(getItem(position).vote));
return convertView;
}
}
最后在 for 循环之后设置适配器,例如:
private void getMovieDataFromJson(String JsonString) throws JSONException {
JSONObject jsonObject = new JSONObject(JsonString);
JSONArray results = jsonObject.getJSONArray("results");
ArrayList<Movie> movies = new ArrayList<>();
for (int i=0; i<results.length(); i++){
String title = results.getJSONObject(i).getString("original_title");
String date = results.getJSONObject(i).getString("release_date");
long id = results.getJSONObject(i).getLong("id");
double vote = results.getJSONObject(i).getDouble("vote_average");
movies.add(new Movie(id, date, title, vote));
}
myMoviesListView.setAdapter(new MovieListAdapter(MainActivity.this, movies));
}
从我的 Activity,我正在调用我的 MovieTask
扩展 AsyncTask
。
我在 MovieTask
的 doInBackground
方法中成功解析了来自服务器的 Json 响应。
现在,我的 activity 中有一个列表视图,我想使用扩展 ArrayAdapter
.
MovieListAdapter
填充列表视图
对于每个视图(即一行),我想填充 3 TextView
s。我正在重写 MovieListAdapter
的 getView
方法来填充相同的内容。
但是我不明白如何将数据从我的 activity 发送到 getView
方法到 getView
方法来填充文本视图?
public class MovieTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
//I HAVE GOT THE JSON DATA THAT CONTAINS AN ARRAY..
//EACH ENTRY IN ARRAY SHOULD BE FILLED IN A SINGLE ROW IN LISTVIEW
getMovieDataFromJson(result);
}
private void getMovieDataFromJson(String JsonString) throws JSONException {
JSONObject jsonObject = new JSONObject(JsonString);
JSONArray results = jsonObject.getJSONArray("results");
for (int i=0; i<results.length(); i++){
String title = results.getJSONObject(i).getString("original_title");
String date = results.getJSONObject(i).getString("release_date");
long id = results.getJSONObject(i).getLong("id");
double vote = results.getJSONObject(i).getDouble("vote_average");
//HERE I NEED TO CALL THE GETVIEW METHOD SO THAT IT FILLS THE ROW OF THE LISTVIEW WITH THESE VALUES - title, date and vote
}
}
MovieListAdapter.java
public class MovieListAdapter extends ArrayAdapter<String > {
public MovieListAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// I NEED TO PREPARE EACH ROW OF LISTVIEW HERE
//EVERY ROW CONTAINS 3 TEXTVIEWS WHICH I NEED TO FILL
//BUT HOW DO I SEND DATA TO FILL THE TEXTVIEW ?
}
}
当您在 AdapterView
上 setAdapter()
例如 ListView
时,getView()
会在内部调用,并且从中返回的视图会填充到 AdapterView
.
您可能想阅读并了解 AdapterView
和 Adapter
的工作原理。继续阅读这里的一些文档:
您需要做的是:
为您的数据创建一个模型,例如:
public class Movie { public long id; public String date; public String title; public double vote; public Movie(long id, String date, String title, double vote) { this.id = id; this.date = date; this.title = title; this.vote = vote; } }
创建布局以显示电影详细信息,例如:
<?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="match_parent" android:orientation="vertical"> <TextView android:id="@+id/lbl_date" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/lbl_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/lbl_vote" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
更改您的适配器以处理
Movie
而不是String
,例如:public class MovieListAdapter extends ArrayAdapter<Movie > { public MovieListAdapter(Context context, List<Movie> objects) { super(context, 0, objects); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.movie_item, parent, false); } ((TextView) convertView.findViewById(R.id.lbl_date)) .setText(getItem(position).date); ((TextView) convertView.findViewById(R.id.lbl_title)) .setText(getItem(position).title); ((TextView) convertView.findViewById(R.id.lbl_vote)) .setText(String.valueOf(getItem(position).vote)); return convertView; } }
最后在 for 循环之后设置适配器,例如:
private void getMovieDataFromJson(String JsonString) throws JSONException { JSONObject jsonObject = new JSONObject(JsonString); JSONArray results = jsonObject.getJSONArray("results"); ArrayList<Movie> movies = new ArrayList<>(); for (int i=0; i<results.length(); i++){ String title = results.getJSONObject(i).getString("original_title"); String date = results.getJSONObject(i).getString("release_date"); long id = results.getJSONObject(i).getLong("id"); double vote = results.getJSONObject(i).getDouble("vote_average"); movies.add(new Movie(id, date, title, vote)); } myMoviesListView.setAdapter(new MovieListAdapter(MainActivity.this, movies)); }