2016-05-30 1 views
0

にJSONをロードするためにLazyAdapterを使用するにはどうすればListViewコントロールに私のJSONデータをロードするためにLazyAdapterを使用しようとしています。 LazyAdapterを変更するにはどうすればいいですか?からデータをロードする方法を教えてください。私はVolleyを使用して取得されたJSONデータを使用しています。これは、返された主なJSONです:リストビュー

{ 
    "success": "1", 
    "message": "Users refreshed", 
    "data": 
    [ 
     { 
      "id": "3", 
      "main_user": "1", 
      "name": "Name 02", 
      "photo_link": "http://example.com/folder/photos/1.jpg" 
     }, 
     { 
      "id": "4", 
      "main_user": "1", 
      "name": "Name 03", 
      "photo_link": "http://example.com/folder/photos/2.png" 
     }, 
     { 
      "id": "5", 
      "main_user": "1", 
      "name": "Name 04", 
      "photo_link": "http://example.com/folder/photos/3.png" 
     }, 
     { 
      "id": "6", 
      "main_user": "1", 
      "name": "Name 05", 
      "photo_link": "http://example.com/folder/photos/4.jpeg" 
     } 
    ] 
} 

を私はデータのみを抽出するために、この行を使用:

JSONObject jsonObject = new JSONObject(response); 
JSONArray data = jsonObject.getJSONArray("data"); 

これは私が現在持っているLazyAdapterです:

public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private String[] data; 
    private static LayoutInflater inflater = null; 
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, String[] d) { 
     activity = a; 
     data = d; 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader = new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (convertView == null) { 
      view = inflater.inflate(R.layout.row_listview_item, null); 
     } 
     TextView text = (TextView) view.findViewById(R.id.text); 
     ImageView image = (ImageView) view.findViewById(R.id.image); 
     text.setText(position); 
     imageLoader.DisplayImage(data[position], image); 
     return view; 
    } 
} 

これが私ですrow_listview_item.xmlファイル:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="8dp" 
    android:id="@+id/listItemLayout" 
    android:descendantFocusability="blocksDescendants"> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="60dp" 
     android:layout_height="60dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:src="@mipmap/ic_launcher" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" 
     android:text="Loading..." 
     android:typeface="sans" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@id/image" 
     android:layout_toEndOf="@id/image" 
     android:layout_marginLeft="10dp" 
     android:layout_marginStart="10dp" 
     android:maxLines="1" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="0" 
     android:visibility="invisible" 
     android:layout_toRightOf="@id/image" 
     android:layout_toEndOf="@id/image" 
     android:layout_alignParentBottom="true" /> 

</RelativeLayout> 

} 

答えて

0

私はそれを理解した!私は他の人々がそれから恩恵を受けるために私の答えを掲示します。

さて、私はLazyAdapterを少し変更しました。これは、新しいLazyAdapterファイルです:

public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private JSONArray data; 
    private static LayoutInflater inflater = null; 
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity activity, JSONArray data) { 
     this.activity = activity; 
     this.data = data; 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader = new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.length(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (convertView == null) { 
      view = inflater.inflate(R.layout.list_row, parent, false); 
     } 
     TextView id = (TextView) view.findViewById(R.id.employeeId); 
     TextView name = (TextView) view.findViewById(R.id.employeeName); 
     ImageView image = (ImageView) view.findViewById(R.id.employeeImage); 
     try { 
      id.setText(data.getJSONObject(position).getString("id")); 
      name.setText(data.getJSONObject(position).getString("name")); 
      imageLoader.DisplayImage(data.getJSONObject(position).getString("photo_link"), image); 
     } catch (JSONException ex) { 
      ex.printStackTrace(); 
     } 
     return view; 
    } 

} 

そして、これは私が私の断片でそれを呼ばれる方法です:

adapter = new LazyAdapter(getActivity(), data); 
//If calling from activity, just use 'this' instead of 'getActivity()'. 'data' is just a JSONArray as you see in the question. 
list.setAdapter(adapter);