2016-12-18 3 views
-1

私のアクティビティから、私はMovieTaskAsyncTaskまで拡張して呼び出しています。 doInBackgroundのメソッドMovieTaskの中で正常に解析されたサーバーからJsonの応答を取得します。ArrayAdapterのgetViewをオーバーライドして3つのTextviewを埋めます

私は自分のアクティビティでリストビューを持っており、ArrayAdapterを拡張するMovieListAdapterを使用してリストビューを作成したいとします。

すべてのビュー(行)について、3 TextViewを入力したいと思います。私はの方法をオーバーライドして、MovieListAdapterを埋めています。 しかし、データをgetViewメソッドに送信する方法を自分のアクティビティからgetViewに変換する方法を理解できません。たとえばListViewためAdapterView

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 ? 
    } 

} 

答えて

1

ときsetAdapter()getView()は内部で呼び出され、同じから返されたビューはAdapterViewに移入されています。

AdapterViewAdapterがどのように機能するのかを知りたい場合があります。先に行くと、ここでいくつかのドキュメントをお読みください。

何をする必要がある:

  • のようなあなたのデータのためのモデルを作成します。 :

    以下のようなMovieのではなく、String秒処理するために、あなたのアダプタを変更

    <?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> 
    
  • 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; 
        } 
    } 
    
  • のような映画の詳細を表示するレイアウトを作成した後

    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)); 
    } 
    
+0

ありがとうございました!ありがとうございました! –