Alexander R.
- Forum-Beiträge: 1.148
05.12.2010, 17:31:38 via Website
05.12.2010 17:31:38 via Website
Hallo,
ich habe eine ListView anzeige:
diese möchte ich nun über meine MenuActivityMain aktualisieren über den "Aktualisieren"-Button:
Wie könnte man das am besten umsetzen??
Hoffe ihr wisst was ich meine sonst fragt nach
ich habe eine ListView anzeige:
1public void getData() {
2 pd = ProgressDialog.show(this, "Laden...", "Hole Daten...", true, true);
3
4 Thread thread = new Thread(this);
5 thread.start();
6 }
7
8 public void run() {
9 String result = "";
10 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
11
12 try {
13 HttpClient httpclient = new DefaultHttpClient();
14 HttpPost httppost = new HttpPost(
15 "urlzuphp");
16 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
17 HttpResponse response = httpclient.execute(httppost);
18 HttpEntity entity = response.getEntity();
19 is = entity.getContent();
20 } catch (Exception e) {
21 Log.e("log_tag", "Fehler in http-Connection " + e.toString(), e);
22 }
23
24 try {
25 BufferedReader reader = new BufferedReader(new InputStreamReader(
26 is, "iso-8859-1"), 8);
27 StringBuilder sb = new StringBuilder();
28 String line = null;
29 while ((line = reader.readLine()) != null) {
30 sb.append(line + "n");
31 }
32 is.close();
33 result = sb.toString();
34 } catch (Exception e) {
35 Log.e("log_tag", "Fehler bei Result-Konvertierung " + e.toString(),
36 e);
37 }
38
39 try {
40 JSONArray jArray = new JSONArray(result);
41 for (int i = 0; i < jArray.length(); i++) {
42 json_data = jArray.getJSONObject(i);
43 results.add((String) json_data.get("title") + " "
44 + json_data.get("message"));
45 }
46 } catch (JSONException e) {
47 Log.e("log_tag", "Fehler beim Daten holen " + e.toString(), e);
48 }
49 handler.sendEmptyMessage(0);
50 }
51
52 private Handler handler = new Handler() {
53 @Override
54 public void handleMessage(Message msg) {
55 pd.dismiss();
56 filllist();
57 }
58 };
59
60 protected void filllist() {
61 try {
62 ListView lv = (ListView) findViewById(R.id.lv);
63 lv.setBackgroundColor(Color.LTGRAY);
64 lv.setCacheColorHint(Color.LTGRAY);
65 lv.setAdapter(new ArrayAdapter<String>(this,
66 android.R.layout.simple_list_item_1, results));
67 } catch (Exception e) {
68 Log.e("log_tag", "Fehler bei Erstellung der Filelist "
69 + e.toString(), e);
70 }
71 }
72}
2 pd = ProgressDialog.show(this, "Laden...", "Hole Daten...", true, true);
3
4 Thread thread = new Thread(this);
5 thread.start();
6 }
7
8 public void run() {
9 String result = "";
10 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
11
12 try {
13 HttpClient httpclient = new DefaultHttpClient();
14 HttpPost httppost = new HttpPost(
15 "urlzuphp");
16 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
17 HttpResponse response = httpclient.execute(httppost);
18 HttpEntity entity = response.getEntity();
19 is = entity.getContent();
20 } catch (Exception e) {
21 Log.e("log_tag", "Fehler in http-Connection " + e.toString(), e);
22 }
23
24 try {
25 BufferedReader reader = new BufferedReader(new InputStreamReader(
26 is, "iso-8859-1"), 8);
27 StringBuilder sb = new StringBuilder();
28 String line = null;
29 while ((line = reader.readLine()) != null) {
30 sb.append(line + "n");
31 }
32 is.close();
33 result = sb.toString();
34 } catch (Exception e) {
35 Log.e("log_tag", "Fehler bei Result-Konvertierung " + e.toString(),
36 e);
37 }
38
39 try {
40 JSONArray jArray = new JSONArray(result);
41 for (int i = 0; i < jArray.length(); i++) {
42 json_data = jArray.getJSONObject(i);
43 results.add((String) json_data.get("title") + " "
44 + json_data.get("message"));
45 }
46 } catch (JSONException e) {
47 Log.e("log_tag", "Fehler beim Daten holen " + e.toString(), e);
48 }
49 handler.sendEmptyMessage(0);
50 }
51
52 private Handler handler = new Handler() {
53 @Override
54 public void handleMessage(Message msg) {
55 pd.dismiss();
56 filllist();
57 }
58 };
59
60 protected void filllist() {
61 try {
62 ListView lv = (ListView) findViewById(R.id.lv);
63 lv.setBackgroundColor(Color.LTGRAY);
64 lv.setCacheColorHint(Color.LTGRAY);
65 lv.setAdapter(new ArrayAdapter<String>(this,
66 android.R.layout.simple_list_item_1, results));
67 } catch (Exception e) {
68 Log.e("log_tag", "Fehler bei Erstellung der Filelist "
69 + e.toString(), e);
70 }
71 }
72}
diese möchte ich nun über meine MenuActivityMain aktualisieren über den "Aktualisieren"-Button:
1public class MenuActivityMain extends Activity {
2
3 // Menu Optionen
4 private static final int MENU_AKTUALISIEREN = 0;
5 private static final int MENU_EINTRAG = 1;
6 private static final int MENU_BEENDEN = 2;
7 private static final int MENU_PREF = 3;
8
9 // Beschriftungen der Menuoptionen
10 private static final String MENU_OPTION_AKTUALISIEREN = "Aktualisieren";
11 private static final String MENU_OPTION_EINTRAG = "Neuer Eintrag";
12 private static final String MENU_OPTION_BEENDEN = "Beenden";
13 private static final String MENU_OPTION_PREF = "Einstellungen";
14
15 /* Menu Items werden erstellt */
16 public boolean onCreateOptionsMenu(Menu menu) {
17 menu.add(0, MENU_AKTUALISIEREN, 0, MENU_OPTION_AKTUALISIEREN).setIcon(
18 R.drawable.ic_menu_refresh);
19 menu.add(0, MENU_EINTRAG, 0, MENU_OPTION_EINTRAG).setIcon(
20 android.R.drawable.ic_menu_add);
21 menu.add(0, MENU_PREF, 0, MENU_OPTION_PREF).setIcon(
22 android.R.drawable.ic_menu_preferences);
23 menu.add(0, MENU_BEENDEN, 0, MENU_OPTION_BEENDEN).setIcon(
24 android.R.drawable.ic_menu_close_clear_cancel);
25 return true;
26 }
27
28 public boolean onOptionsItemSelected(MenuItem item) {
29 switch (item.getItemId()) {
30 case MENU_AKTUALISIEREN:
31 Toast.makeText(getApplicationContext(),
32 "Feature noch nicht implementiert", Toast.LENGTH_LONG)
33 .show();
34 HIER SOLL DIE LISTVIEW AKTUALISIERT WERDEN
35 break;
36 case MENU_EINTRAG:
37 startActivity(new Intent(this, Eintrag.class));
38 break;
39 case MENU_BEENDEN:
40 AlertDialog.Builder builder = new AlertDialog.Builder(this);
41 builder.setMessage("Möchtest du App Wunsch Beenden?").setTitle(
42 "Beenden").setCancelable(false).setPositiveButton("Ja",
43 new DialogInterface.OnClickListener() {
44 public void onClick(DialogInterface dialog, int id) {
45 finish();
46 }
47 }).setNegativeButton("Nein",
48 new DialogInterface.OnClickListener() {
49 public void onClick(DialogInterface dialog, int id) {
50 dialog.dismiss();
51 }
52 });
53 AlertDialog alert = builder.create();
54 alert.show();
55 break;
56 case MENU_PREF:
57 Intent settingsActivity = new Intent(getBaseContext(),
58 Einstellungen.class);
59 startActivity(settingsActivity);
60 break;
61 }
62 return false;
63 }
64}
2
3 // Menu Optionen
4 private static final int MENU_AKTUALISIEREN = 0;
5 private static final int MENU_EINTRAG = 1;
6 private static final int MENU_BEENDEN = 2;
7 private static final int MENU_PREF = 3;
8
9 // Beschriftungen der Menuoptionen
10 private static final String MENU_OPTION_AKTUALISIEREN = "Aktualisieren";
11 private static final String MENU_OPTION_EINTRAG = "Neuer Eintrag";
12 private static final String MENU_OPTION_BEENDEN = "Beenden";
13 private static final String MENU_OPTION_PREF = "Einstellungen";
14
15 /* Menu Items werden erstellt */
16 public boolean onCreateOptionsMenu(Menu menu) {
17 menu.add(0, MENU_AKTUALISIEREN, 0, MENU_OPTION_AKTUALISIEREN).setIcon(
18 R.drawable.ic_menu_refresh);
19 menu.add(0, MENU_EINTRAG, 0, MENU_OPTION_EINTRAG).setIcon(
20 android.R.drawable.ic_menu_add);
21 menu.add(0, MENU_PREF, 0, MENU_OPTION_PREF).setIcon(
22 android.R.drawable.ic_menu_preferences);
23 menu.add(0, MENU_BEENDEN, 0, MENU_OPTION_BEENDEN).setIcon(
24 android.R.drawable.ic_menu_close_clear_cancel);
25 return true;
26 }
27
28 public boolean onOptionsItemSelected(MenuItem item) {
29 switch (item.getItemId()) {
30 case MENU_AKTUALISIEREN:
31 Toast.makeText(getApplicationContext(),
32 "Feature noch nicht implementiert", Toast.LENGTH_LONG)
33 .show();
34 HIER SOLL DIE LISTVIEW AKTUALISIERT WERDEN
35 break;
36 case MENU_EINTRAG:
37 startActivity(new Intent(this, Eintrag.class));
38 break;
39 case MENU_BEENDEN:
40 AlertDialog.Builder builder = new AlertDialog.Builder(this);
41 builder.setMessage("Möchtest du App Wunsch Beenden?").setTitle(
42 "Beenden").setCancelable(false).setPositiveButton("Ja",
43 new DialogInterface.OnClickListener() {
44 public void onClick(DialogInterface dialog, int id) {
45 finish();
46 }
47 }).setNegativeButton("Nein",
48 new DialogInterface.OnClickListener() {
49 public void onClick(DialogInterface dialog, int id) {
50 dialog.dismiss();
51 }
52 });
53 AlertDialog alert = builder.create();
54 alert.show();
55 break;
56 case MENU_PREF:
57 Intent settingsActivity = new Intent(getBaseContext(),
58 Einstellungen.class);
59 startActivity(settingsActivity);
60 break;
61 }
62 return false;
63 }
64}
Wie könnte man das am besten umsetzen??
Hoffe ihr wisst was ich meine sonst fragt nach
— geändert am 05.12.2010, 18:32:55
Gruß Alexander
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.