覆盖ArrayAdapter的getView以填充3个Textviews [英] Overriding getView of ArrayAdapter to fill 3 Textviews
问题描述
在我的活动中,我正在调用扩展了 AsyncTask
的 MovieTask
.我从服务器获取了Json响应,该响应已在 MovieTask
的 doInBackground
方法内成功解析.
From my Activity, I am calling my MovieTask
that extendsAsyncTask
.
I get Json response from the server which I have successfully parsed inside the doInBackground
method of MovieTask
.
现在,我在活动中有一个列表视图,我想使用扩展了 ArrayAdapter
的 MovieListAdapter
填充列表视图.
Now, I have a listview in my activity and I want to populate the listview using MovieListAdapter
that extends ArrayAdapter
.
对于每个视图(即一行),我要填充3个 TextView
.我覆盖了 MovieListAdapter
的 getView
方法以填充相同内容.但是我不明白如何将数据从我的活动发送到 getView
方法到 getView
方法以填充文本视图?
For every view (i.e. a row), I want to fill 3 TextView
s. I am overriding getView
method of MovieListAdapter
to fill the same.
But I don't understand how to send the data to getView
method from my activity to getView
method to populate the textviews?
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
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
中.
When you setAdapter()
on an AdapterView
for example ListView
, getView()
is called internally and the views returned from the same are populated in the AdapterView
.
您可能想阅读并了解 AdapterView
和 Adapter
的工作方式.继续阅读这里的一些文档:
You might want to read and learn about how AdapterView
s and Adapter
s work. Go ahead and read some docs here:
您需要做的是:
-
为您的数据创建模型,例如:
Create a model for your data like:
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;
}
}
创建布局以显示电影详细信息,例如:
Create a layout to show movie details like:
<?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>
更改适配器以处理电影
,而不是 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循环之后设置适配器:
Finally set the adapter after your for loop like:
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));
}
这篇关于覆盖ArrayAdapter的getView以填充3个Textviews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!