mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 14:07:55 +01:00
Android: documents listing from API
This commit is contained in:
parent
04752cab0c
commit
76d195a344
@ -8,13 +8,16 @@ import android.widget.TextView;
|
|||||||
|
|
||||||
import com.sismics.docs.R;
|
import com.sismics.docs.R;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapter of documents.
|
* Adapter of documents.
|
||||||
*
|
*
|
||||||
* @author bgamard
|
* @author bgamard
|
||||||
*/
|
*/
|
||||||
public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHolder> {
|
public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHolder> {
|
||||||
private String[] mDataset;
|
private JSONArray documents;
|
||||||
|
|
||||||
// Provide a reference to the views for each data item
|
// Provide a reference to the views for each data item
|
||||||
// Complex data items may need more than one view per item, and
|
// Complex data items may need more than one view per item, and
|
||||||
@ -25,12 +28,18 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
|
|||||||
public TextView subtitleTextView;
|
public TextView subtitleTextView;
|
||||||
public ViewHolder(View v) {
|
public ViewHolder(View v) {
|
||||||
super(v);
|
super(v);
|
||||||
|
titleTextView = (TextView) v.findViewById(R.id.titleTextView);
|
||||||
|
subtitleTextView = (TextView) v.findViewById(R.id.subtitleTextView);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provide a suitable constructor (depends on the kind of dataset)
|
// Provide a suitable constructor (depends on the kind of dataset)
|
||||||
public DocListAdapter(String[] myDataset) {
|
public DocListAdapter() {
|
||||||
mDataset = myDataset;
|
}
|
||||||
|
|
||||||
|
public void setDocuments(JSONArray documents) {
|
||||||
|
this.documents = documents;
|
||||||
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new views (invoked by the layout manager)
|
// Create new views (invoked by the layout manager)
|
||||||
@ -49,12 +58,17 @@ public class DocListAdapter extends RecyclerView.Adapter<DocListAdapter.ViewHold
|
|||||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||||
// - get element from your dataset at this position
|
// - get element from your dataset at this position
|
||||||
// - replace the contents of the view with that element
|
// - replace the contents of the view with that element
|
||||||
// holder.mTextView.setText(mDataset[position]);
|
JSONObject document = documents.optJSONObject(position);
|
||||||
|
holder.titleTextView.setText(document.optString("title"));
|
||||||
|
holder.subtitleTextView.setText(document.optString("description"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the size of your dataset (invoked by the layout manager)
|
// Return the size of your dataset (invoked by the layout manager)
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
return mDataset.length;
|
if (documents == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return documents.length();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,18 +9,26 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.loopj.android.http.JsonHttpResponseHandler;
|
||||||
import com.sismics.docs.DividerItemDecoration;
|
import com.sismics.docs.DividerItemDecoration;
|
||||||
import com.sismics.docs.R;
|
import com.sismics.docs.R;
|
||||||
import com.sismics.docs.adapter.DocListAdapter;
|
import com.sismics.docs.adapter.DocListAdapter;
|
||||||
import com.sismics.docs.listener.RecyclerItemClickListener;
|
import com.sismics.docs.listener.RecyclerItemClickListener;
|
||||||
|
import com.sismics.docs.resource.DocumentResource;
|
||||||
|
|
||||||
|
import org.apache.http.Header;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author bgamard.
|
* @author bgamard.
|
||||||
*/
|
*/
|
||||||
public class DocListFragment extends Fragment {
|
public class DocListFragment extends Fragment {
|
||||||
|
|
||||||
|
DocListAdapter adapter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
|
// Initialize the view
|
||||||
View view = inflater.inflate(R.layout.doc_list_fragment, container, false);
|
View view = inflater.inflate(R.layout.doc_list_fragment, container, false);
|
||||||
|
|
||||||
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.docList);
|
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.docList);
|
||||||
@ -30,7 +38,7 @@ public class DocListFragment extends Fragment {
|
|||||||
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
|
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
|
||||||
recyclerView.setLayoutManager(layoutManager);
|
recyclerView.setLayoutManager(layoutManager);
|
||||||
|
|
||||||
RecyclerView.Adapter adapter = new DocListAdapter(new String[] { "Doc 1", "Doc 2", "Doc 3"});
|
adapter = new DocListAdapter();
|
||||||
recyclerView.setAdapter(adapter);
|
recyclerView.setAdapter(adapter);
|
||||||
|
|
||||||
recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.abc_list_divider_mtrl_alpha)));
|
recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.abc_list_divider_mtrl_alpha)));
|
||||||
@ -42,6 +50,21 @@ public class DocListFragment extends Fragment {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Grab the documents
|
||||||
|
refreshDocuments();
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh the document list.
|
||||||
|
*/
|
||||||
|
private void refreshDocuments() {
|
||||||
|
DocumentResource.list(getActivity(), new JsonHttpResponseHandler() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||||
|
adapter.setDocuments(response.optJSONArray("documents"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.sismics.docs.resource;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.loopj.android.http.JsonHttpResponseHandler;
|
||||||
|
import com.loopj.android.http.RequestParams;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access to /document API.
|
||||||
|
*
|
||||||
|
* @author bgamard
|
||||||
|
*/
|
||||||
|
public class DocumentResource extends BaseResource {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GET /document/list.
|
||||||
|
*
|
||||||
|
* @param context Context
|
||||||
|
* @param responseHandler Callback
|
||||||
|
*/
|
||||||
|
public static void list(Context context, JsonHttpResponseHandler responseHandler) {
|
||||||
|
init(context);
|
||||||
|
|
||||||
|
RequestParams params = new RequestParams();
|
||||||
|
client.get(getApiUrl(context) + "/document/list", params, responseHandler);
|
||||||
|
}
|
||||||
|
}
|
@ -7,19 +7,19 @@
|
|||||||
android:padding="12dp">
|
android:padding="12dp">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
android:id="@+id/titleTextView"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:fontFamily="sans-serif-light"
|
android:fontFamily="sans-serif-light"
|
||||||
android:text="Facture Amazon"
|
|
||||||
android:textColor="#212121"
|
android:textColor="#212121"
|
||||||
android:textSize="16sp"/>
|
android:textSize="16sp"/>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
android:id="@+id/subtitleTextView"
|
||||||
android:layout_marginTop="4dp"
|
android:layout_marginTop="4dp"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:fontFamily="sans-serif-light"
|
android:fontFamily="sans-serif-light"
|
||||||
android:text="Appartement, Caluire"
|
|
||||||
android:textColor="#777777"
|
android:textColor="#777777"
|
||||||
android:textSize="16sp"/>
|
android:textSize="16sp"/>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user