1
0
mirror of https://gitlab.com/openlp/android.git synced 2024-12-22 11:52:49 +00:00

we have dropdown

This commit is contained in:
Tim Bentley 2015-11-21 12:42:14 +00:00
parent bb67f7a737
commit c9f8413f16
2 changed files with 149 additions and 83 deletions

View File

@ -1,25 +1,53 @@
package org.openlp.android2.fragments; package org.openlp.android2.fragments;
import android.net.Uri; import android.app.Fragment;
import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.util.Log; import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openlp.android2.R; import org.openlp.android2.R;
import org.openlp.android2.api.Api; import org.openlp.android2.api.Api;
import org.openlp.android2.common.JsonHelpers; import org.openlp.android2.common.JsonHelpers;
import org.openlp.android2.common.OpenLPFragment;
import org.openlp.android2.common.OpenLPHttpClient; import org.openlp.android2.common.OpenLPHttpClient;
import java.util.ArrayList;
import java.util.List;
/** /**
*/ */
public class SearchFragment extends OpenLPFragment { public class SearchFragment extends Fragment {
private final String LOG_TAG = SearchFragment.class.getName(); private final String LOG_TAG = SearchFragment.class.getName();
private Spinner spinner;
private static AsyncHttpClient client = new AsyncHttpClient();
public Context context;
protected String calledURL;
protected OpenLPHttpClient httpClient;
protected String updateUrl;
public SearchFragment() { public SearchFragment() {
Log.d(LOG_TAG, "Constructor"); Log.d(LOG_TAG, "Constructor");
@ -33,21 +61,25 @@ public class SearchFragment extends OpenLPFragment {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = getActivity(); context = getActivity();
updateUrl = Api.SERVICE_LIST; updateUrl = Api.SEARCHABLE_PLUGINS;
httpClient = new OpenLPHttpClient(context); httpClient = new OpenLPHttpClient(context);
return super.onCreateView(inflater, container, savedInstanceState); View view = inflater.inflate(R.layout.fragment_search, container, false);
spinner = (Spinner)view.findViewById(R.id.search_spinner);
triggerTextRequest(Api.SEARCHABLE_PLUGINS);
//spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
return view;
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
Log.d(LOG_TAG, "Resuming..."); Log.d(LOG_TAG, "Resuming...");
triggerTextRequest(Api.SEARCHABLE_PLUGINS); //triggerTextRequest(Api.SEARCHABLE_PLUGINS);
Log.d(LOG_TAG, "Resumed..."); Log.d(LOG_TAG, "Resumed...");
} }
@ -58,15 +90,34 @@ public class SearchFragment extends OpenLPFragment {
public void manageResponse(String response, boolean notInError) { public void manageResponse(String response, boolean notInError) {
if (calledURL.equals(updateUrl)) { if (calledURL.equals(updateUrl)) {
populateTabDisplay(response); populatePluginList(response);
}else { } else {
processUpdate(response, notInError); //processUpdate(response, notInError);
} }
} }
private void populateTabDisplay(String response){ private void populatePluginList(String response) {
int a = 1; Log.i(LOG_TAG, "populatePluginList - entry");
List<String> categories = new ArrayList<String>();
try {
JSONArray items = new JSONObject(response).getJSONObject("results").getJSONArray("items");
for (int i = 0; i < items.length(); ++i) {
JSONArray item = items.getJSONArray(i);
categories.add(item.get(1).toString());
} }
} catch (JSONException e) {
Log.e(LOG_TAG, response);
e.printStackTrace();
}
ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, categories);
LTRadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(LTRadapter);
Log.i(LOG_TAG, "populatePluginList - exit");
}
public void itemClicked(int position) { public void itemClicked(int position) {
try { try {
@ -79,5 +130,27 @@ public class SearchFragment extends OpenLPFragment {
Toast.makeText(context, "Request Failed", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Request Failed", Toast.LENGTH_SHORT).show();
} }
} }
protected void triggerTextRequest(String url) {
calledURL = url;
Log.d(LOG_TAG, "Trigger Request for url " + url);
String callurl = String.format("%s%s", httpClient.getAbsoluteUrl(client), url );
client.get(callurl, null, new TextHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
// called when response HTTP status is "200 OK"
manageResponse(responseString, true);
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
if (statusCode == 401) {
Toast.makeText(context, R.string.httpreturn_unauthorised, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, R.string.unable, Toast.LENGTH_LONG).show();
}
manageResponse(responseString, false);
}
});
}
} }

View File

@ -1,13 +1,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.openlp.android2.fragments.SearchFragment"
android:id="@+id/search_layout">
<!-- TODO: Update blank fragment layout --> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout
android:orientation="vertical" android:orientation="vertical"
android:id="@+id/fragmentsearchlist" android:id="@+id/fragmentsearchlist"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -30,7 +24,7 @@
android:textAppearance="?android:attr/textAppearanceMedium" /> android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner <Spinner
android:layout_width="288dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/search_spinner" android:id="@+id/search_spinner"
android:layout_weight="0.58" /> android:layout_weight="0.58" />
@ -80,6 +74,5 @@
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</FrameLayout>