SQL Datein in GridView

  • 0 Antworten
  • Letztes Antwortdatum
D

Didi95

Neues Mitglied
0
Hi,
ich habe folgendes Problem:
Meine App fragt Serverdaten aus einer SQL Datenbank ab und gibt sie mir bislang in einer ListView untereinander aus. Nun muss ich diese Daten aber nebeneinander angezeigt bekommen. Ist es da sinnvoll eine Gridview zu nehmen oder kann ich einfach auch mehrere Listviews nebeneinander setzen und sie mir dann so anzeigen lassen?

Hier der Codefür die ListView:
Code:
package com.example.vertretung;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.vertretung.R;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class AlleVertretungen extends ListActivity {
	// Progress Dialog
		private ProgressDialog pDialog;

		// Creating JSON Parser object
		JSONParser jParser = new JSONParser();

		ArrayList<HashMap<String, String>> productsList;

		// url to get all products list
		private static String url_all_products = "http://yourcloud.eichsfeld-gymnasium.de/app_neu/get_all_products.php?id=23";

		// JSON Node names
		private static final String TAG_SUCCESS = "success";
		private static final String TAG_PRODUCTS="products";
		private static final String TAG_DATUM = "Datum";
		private static final String TAG_Stunde = "Stunde";
		private static final String TAG_Klassen = "Klassen";
		private static final String TAG_Vertreter = "Vertreter";
		private static final String TAG_Fach = "Fach";
		private static final String TAG_Raum = "Raum";
		private static final String TAG_Art = "Art";
		private static final String TAG_Lehrer_alt = "Lehrer_alt";
		private static final String TAG_le_nach = "le_nach";
		private static final String TAG_Vertretungstext = "Vertretungstext";
		

		// products JSONArray
		JSONArray Vertretung = null;

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.all_products);

			// Hashmap for ListView
			productsList = new ArrayList<HashMap<String, String>>();

			// Loading products in Background Thread
			new LoadAllProducts().execute();

			// Get listview
			ListView lv = getListView();

			// on seleting single product
			// launching Edit Product Screen
			

		}

		// Response from Edit Product Activity
		@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			super.onActivityResult(requestCode, resultCode, data);
			// if result code 100
			if (resultCode == 10000) {
				// if result code 100 is received 
				// means user edited/deleted product
				// reload this screen again
				Intent intent = getIntent();
				finish();
				startActivity(intent);
			}

		}

		/**
		 * Background Async Task to Load all product by making HTTP Request
		 * */
		class LoadAllProducts extends AsyncTask<String, String, String> {

			/**
			 * Before starting background thread Show Progress Dialog
			 * */
			protected void onPreExecute() {
				super.onPreExecute();
				pDialog = new ProgressDialog(AlleVertretungen.this);
				pDialog.setMessage("Vertretungen werden geladen...");
				pDialog.setIndeterminate(false);
				pDialog.setCancelable(true);
				pDialog.show();
			}

			/**
			 * getting All products from url
			 * */
			protected String doInBackground(String... args) {
				// Building Parameters
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				// getting JSON string from URL
				JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
				
				// Check your log cat for JSON reponse
				Log.d("Alle Vertretungen: ", json.toString());

				try {
					// Checking for SUCCESS TAG
					int success = json.getInt(TAG_SUCCESS);

					if (success == 1) {
						// products found
						// Getting Array of Products
						Vertretung = json.getJSONArray(TAG_PRODUCTS);

						// looping through All Products
						for (int i = 0; i < Vertretung.length(); i++) {
							JSONObject c = Vertretung.getJSONObject(i);

							// Storing each json item in variable
							String Datum = c.getString(TAG_DATUM);
							String Klasse = c.getString(TAG_Klassen);
							String Stunde = c.getString(TAG_Stunde);
							String Vertreter = c.getString(TAG_Vertreter);
							String Fach = c.getString(TAG_Fach);
							String Raum = c.getString(TAG_Raum);
							String Art = c.getString(TAG_Art);
							String Lehrer_alt = c.getString(TAG_Lehrer_alt);
							String le_nach = c.getString(TAG_le_nach);
							String Vertretungstext = c.getString(TAG_Vertretungstext);

							// creating new HashMap
							HashMap<String, String> map = new HashMap<String, String>();

							// adding each child node to HashMap key => value
							map.put(TAG_DATUM, Datum);
							map.put(TAG_Klassen, Klasse);
							map.put(TAG_Stunde, Stunde);
							map.put(TAG_Vertreter, Vertreter);
							map.put(TAG_Fach, Fach);
							map.put(TAG_Raum, Raum);
							map.put(TAG_Art, Art);
							map.put(TAG_Lehrer_alt, Lehrer_alt);
							map.put(TAG_le_nach, le_nach);
							map.put(TAG_Vertretungstext, Vertretungstext);

							// adding HashList to ArrayList
							productsList.add(map);
						}
					
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

				return null;
			}

			/**
			 * After completing background task Dismiss the progress dialog
			 * **/
			protected void onPostExecute(String file_url) {
				// dismiss the dialog after getting all products
				pDialog.dismiss();
				// updating UI from Background Thread
				runOnUiThread(new Runnable() {
					public void run() {
						/**
						 * Updating parsed JSON data into ListView
						 * */
						ListAdapter adapter = new SimpleAdapter(
								AlleVertretungen.this, productsList,
								R.layout.list_item, new String[] { TAG_DATUM,
										TAG_Klassen, TAG_Stunde, TAG_Vertreter, TAG_Fach, TAG_Raum, TAG_Art, TAG_Lehrer_alt,TAG_le_nach, TAG_Vertretungstext},
										new int[] { R.id.Datum, R.id.Klassen, R.id.Vertreter, R.id.Fach,R.id.Raum,R.id.Art, R.id.Lehrer_alt,R.id.le_nach,R.id.Vertretungstext });
						// updating listview
						setListAdapter(adapter);
					}
				});

			}

		}
	

}

Schonmal danke an euch! :)
Dudei95
 
Zurück
Oben Unten