在我的活动中,我正在调用MovieTask
AsyncTask
。
我从doInBackground
MovieTask
方法中成功解析的服务器获取Json响应。
现在,我的活动中有一个列表视图,我想使用扩展MovieListAdapter
的{{1}}填充列表视图。
对于每个视图(即一行),我想填充3 ArrayAdapter
s。我正在覆盖TextView
getView
方法,以填补相同的内容。
但我不明白如何将数据从我的活动发送到MovieListAdapter
方法到getView
方法以填充文本视图?
getView
MovieListAdapter.java
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
}
}
答案 0 :(得分:1)
setAdapter()
AdapterView
上的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;
}
}
,如:
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));
}
最后在for循环之后设置适配器,如:
{{1}}