Created
September 17, 2015 03:29
-
-
Save mr5z/62171b3e7fad318dce2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FragmentListCommandDialog extends DialogFragment implements OnItemClickListener { | |
public interface CommandClickListener { | |
public void onCommandClick(CommandRow command); | |
} | |
/** | |
* You can found this file in the assets folder | |
*/ | |
private static final String BC_COMMAND_LIST_FILENAME = "bccommands.json"; | |
private Debug mDebug = new Debug(FragmentListCommandDialog.class); | |
private ListView mListView; | |
private CommandAdapter mAdapter; | |
private ArrayList<CommandRow> mCommands = new ArrayList<>(); | |
private CommandClickListener mCommandClickListener; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
ViewGroup rootView = (ViewGroup) inflater.inflate( | |
R.layout.test, container, false); | |
initializeUi(rootView); | |
mListView.setOnItemClickListener(this); | |
// load the file asynchronously | |
new AsyncTask<String, String, String>() { | |
@Override | |
protected String doInBackground(String... params) { | |
try { | |
StringBuffer contents = loadCommands(); | |
configureCommands(contents); | |
} catch(Exception e) { | |
mDebug.logToFile("Error occurred while opening '%s': %s", | |
BC_COMMAND_LIST_FILENAME, | |
e.getMessage()); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
// no need to call Adapter#notifyDataSetChanged | |
mListView.setAdapter(mAdapter); | |
} | |
}.execute(""); | |
return rootView; | |
} | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
if ( mCommandClickListener != null ) { | |
CommandRow command = mCommands.get(position); | |
mCommandClickListener.onCommandClick(command); | |
} | |
} | |
private void initializeUi(ViewGroup v) { | |
mListView = (ListView) v.findViewById(R.id.category_list_view); | |
mAdapter = new CommandAdapter(getActivity(), mCommands); | |
} | |
private StringBuffer loadCommands() throws IOException, JSONException { | |
StringBuffer content = new StringBuffer(128); | |
InputStream inputStream = getActivity() | |
.getAssets() | |
.open(BC_COMMAND_LIST_FILENAME); | |
content.append(IO.convertStreamToString(inputStream)); | |
return content; | |
} | |
private void configureCommands(StringBuffer content) throws JSONException { | |
JSONArray array = new JSONArray(content.toString()); | |
mCommands.clear(); | |
for(int i = 0;i < array.length(); ++i) { | |
JSONObject object = array.getJSONObject(i); | |
String cmdLabel = object.getString("cmdLabel"); | |
String cmdByte = object.getString("cmdType"); | |
String cmdAscii = object.getString("cmdAscii"); | |
String cmdParam = object.getString("cmdParam"); | |
CommandRow cmd = new CommandRow( | |
cmdLabel, | |
TypeConverter.hexStringToByte(cmdByte, 0), | |
cmdAscii, | |
cmdParam); | |
mCommands.add(cmd); | |
} | |
} | |
public void setOnCommandClickListener(CommandClickListener commandClickListener) { | |
mCommandClickListener = commandClickListener; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment