1
0
mirror of https://gitlab.com/openlp/android.git synced 2024-12-22 03:42:48 +00:00

fix search

This commit is contained in:
Tim Bentley 2015-11-22 14:56:47 +00:00
parent 51a9a216d9
commit 0c8f675589
2 changed files with 83 additions and 32 deletions

View File

@ -45,20 +45,6 @@ public class JsonHelpers {
}
}
public static String createSearchJSON(String key, String value) throws JSONHandlerException {
try {
String responseJSON;
responseJSON = new JSONStringer().object().key(key).value(value)
.endObject().toString();
responseJSON = URLEncoder.encode(responseJSON, "UTF-8");
return responseJSON;
} catch (JSONException e) {
throw new JSONHandlerException(e);
} catch (UnsupportedEncodingException e) {
throw new JSONHandlerException(e);
}
}
public static class JSONHandlerException extends Exception {
private static final long serialVersionUID = -6772307308404816615L;

View File

@ -2,7 +2,9 @@ package org.openlp.android2.fragments;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
@ -20,6 +22,7 @@ import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
@ -39,6 +42,7 @@ import org.openlp.android2.common.JsonHelpers;
import org.openlp.android2.common.OpenLPHttpClient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
@ -109,31 +113,33 @@ public class SearchFragment extends Fragment {
public void manageResponse(String response, boolean notInError) {
if (calledURL.equals(updateUrl)) {
populatePluginList(response);
populatePluginList(response, notInError);
} else {
//processUpdate(response, notInError);
populateListDisplay(response, notInError);
}
}
private void populatePluginList(String response) {
private void populatePluginList(String response, Boolean notInError) {
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());
if (notInError) {
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();
}
} catch (JSONException e) {
Log.e(LOG_TAG, response);
e.printStackTrace();
ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(getActivity(),
R.layout.spinner_list_item, categories);
LTRadapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(LTRadapter);
Log.i(LOG_TAG, "populatePluginList - exit");
}
ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(getActivity(),
R.layout.spinner_list_item, categories);
LTRadapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(LTRadapter);
Log.i(LOG_TAG, "populatePluginList - exit");
}
public void itemClicked(int position) {
@ -173,8 +179,8 @@ public class SearchFragment extends Fragment {
public void requestSearch(String plugin, String text) {
updateUrl = Api.SEARCH_PLUGIN_FORMATTED;
try {
String request = JsonHelpers.createSearchJSON("data", text);
String url = String.format(Api.SEARCH_PLUGIN_FORMATTED, plugin);
String request = JsonHelpers.createRequestJSON("text", text);
String url = String.format(Api.SEARCH_PLUGIN_FORMATTED, plugin.toLowerCase());
triggerTextRequest(String.format("%s%s", url, request));
Log.d(LOG_TAG, String.format("Search request. apiBase(%s), text(%s)", plugin, text));
} catch (JsonHelpers.JSONHandlerException e) {
@ -183,4 +189,63 @@ public class SearchFragment extends Fragment {
}
}
public void populateListDisplay(String json, boolean notInError) {
Log.i(LOG_TAG, "populateListDisplay - entry");
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
if (notInError) {
try {
JSONArray items = new JSONObject(json).getJSONObject("results").getJSONArray("items");
for (int i = 0; i < items.length(); ++i) {
JSONArray item = items.getJSONArray(i);
HashMap<String, String> hm = new HashMap<String, String>();
//hm.put("tag", item.getString("tag"));
//hm.put("liveListNormal", Html.fromHtml(item.getString("html")).toString());
aList.add(hm);
}
} catch (JSONException e) {
Log.e(LOG_TAG,json);
e.printStackTrace();
}
}
// Keys used in Hashmap
String[] from = {"tag", "liveListNormal", "liveListSelected"};
// Ids of views in live_list_fragment
int[] to = {R.id.tag, R.id.liveListNormal, R.id.liveListSelected};
ListView list = (ListView)getActivity().findViewById(R.id.searchresultsdetails);
int a = 1;
/* ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(getActivity(),
R.layout.spinner_list_item, categories);
// Instantiating an adapter to store each items
ListAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList,
R.layout.fragment_s, from, to) {
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(R.id.tag);
text1.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
TextView text2 = (TextView) view.findViewById(R.id.liveListNormal);
text2.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
if (selected == position) {
text2.setTextColor(Color.parseColor("#000000"));
text2.setTypeface(null, Typeface.BOLD_ITALIC);
} else{
text2.setTypeface(null, Typeface.NORMAL);
}
return view;
}
};
setListAdapter(adapter);*/
Log.i(LOG_TAG, "populateListDisplay - exit");
}
}