Java Android Mysql mit Fragments

  • Java

    Java Android Mysql mit Fragments

    Hallo zusammen.

    Ich würde gerne von meinem Android Gerät auf eine Externe Datenbank zugreifen,
    Spetzifikationen:

    -Aufbau der App mit Fragmente
    -Listview mit ("item und Sub item")

    Kann mir jemand helfen?

    Gibt es für das andwenden von MySql und Fragmenten ein Tutorial? :S

    Edit:

    Ich habe nun daran gearbeitet und bin auf folgendes gekommen (bis jetzt)

    Spoiler anzeigen

    Java-Quellcode

    1. ​import android.content.Context;
    2. import android.os.AsyncTask;
    3. import android.os.Bundle;
    4. import android.support.annotation.Nullable;
    5. import android.support.v4.app.Fragment;
    6. import android.view.LayoutInflater;
    7. import android.view.Menu;
    8. import android.view.View;
    9. import android.view.ViewGroup;
    10. import android.widget.ListView;
    11. import android.widget.SimpleAdapter;
    12. import android.widget.Toast;
    13. import org.apache.http.HttpResponse;
    14. import org.apache.http.client.ClientProtocolException;
    15. import org.apache.http.client.HttpClient;
    16. import org.apache.http.client.methods.HttpPost;
    17. import org.apache.http.impl.client.DefaultHttpClient;
    18. import org.json.JSONArray;
    19. import org.json.JSONException;
    20. import org.json.JSONObject;
    21. import java.io.BufferedReader;
    22. import java.io.IOException;
    23. import java.io.InputStream;
    24. import java.io.InputStreamReader;
    25. import java.util.ArrayList;
    26. import java.util.HashMap;
    27. import java.util.List;
    28. import java.util.Map;
    29. /**
    30. * Created by Roger on 11.03.2015.
    31. */
    32. public class menu4_Fragment extends Fragment {
    33. View rootview;
    34. private String jsonResult;
    35. private String url = "Link Zur PHP Datei";
    36. private ListView listView;
    37. @Nullable
    38. @Override
    39. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    40. rootview = inflater.inflate(R.layout.menu4_layout, container, false);
    41. final ListView listview = (ListView) rootview.findViewById(R.id.listView1);
    42. accessWebService();
    43. return rootview;
    44. }
    45. // Async Task to access the web
    46. private class JsonReadTask extends AsyncTask<String, Void, String> {
    47. @Override
    48. protected String doInBackground(String... params) {
    49. HttpClient httpclient = new DefaultHttpClient();
    50. HttpPost httppost = new HttpPost(params[0]);
    51. try {
    52. HttpResponse response = httpclient.execute(httppost);
    53. jsonResult = inputStreamToString(
    54. response.getEntity().getContent()).toString();
    55. }
    56. catch (ClientProtocolException e) {
    57. e.printStackTrace();
    58. } catch (IOException e) {
    59. e.printStackTrace();
    60. }
    61. return null;
    62. }
    63. private StringBuilder inputStreamToString(InputStream is) {
    64. String rLine = "";
    65. StringBuilder answer = new StringBuilder();
    66. BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    67. try {
    68. while ((rLine = rd.readLine()) != null) {
    69. answer.append(rLine);
    70. }
    71. }
    72. catch (IOException e) {
    73. // e.printStackTrace();
    74. }
    75. return answer;
    76. }
    77. @Override
    78. protected void onPostExecute(String result) {
    79. ListDrwaer();
    80. }
    81. }// end async task
    82. public void accessWebService() {
    83. JsonReadTask task = new JsonReadTask();
    84. // passes values for the urls string array
    85. task.execute(new String[] { url });
    86. }
    87. // build hash set for list view
    88. public void ListDrwaer() {
    89. List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
    90. try {
    91. JSONObject jsonResponse = new JSONObject(jsonResult);
    92. JSONArray jsonMainNode = jsonResponse.optJSONArray("emp_info");
    93. for (int i = 0; i < jsonMainNode.length(); i++) {
    94. JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
    95. String name = jsonChildNode.optString("employee name");
    96. String number = jsonChildNode.optString("employee no");
    97. String outPut = name + "-" + number;
    98. employeeList.add(createEmployee("employees", outPut));
    99. }
    100. } catch (JSONException e) {
    101. }
    102. SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), mylist,
    103. android.R.layout.simple_list_item_1,
    104. new String[] { "employees" }, new int[] { android.R.id.text1 });
    105. listView.setAdapter(simpleAdapter);
    106. }
    107. private HashMap<String, String> createEmployee(String name, String number) {
    108. HashMap<String, String> employeeNameNo = new HashMap<String, String>();
    109. employeeNameNo.put(name, number);
    110. return employeeNameNo;
    111. }
    112. }
    [/spoiler ]

    Jedoch erhalte ich hier:

    Quellcode

    1. ​ SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), mylist,
    2. android.R.layout.simple_list_item_1,
    3. new String[] { "employees" }, new int[] { android.R.id.text1 });
    4. listView.setAdapter(simpleAdapter);
    5. }

    Einen Fehler Kann mir da jemand helfen?

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „schnibli“ ()