L
likedue
Ambitioniertes Mitglied
- 1
Hey Leute,
ich habe soweit folgendes System:
Ich lege über meine App einen User über eine php-Krücke in meiner MySQL-Datenbank an und vergebe eine ID.
jetzt rufe ich den User in meiner App auf und möchte alle für ihn angelegten Informationen auslesen.
Also habe ich im Vorfeld natürlich mit Hilfe meiner App bereits beliebige Informationen angelegt und den einzelnen Usern dazu auch eine PID verliehen.
Meine SQL-Tabelle sieht also in etwa so aus:
User:
id | name
0 xy
1 yz
2 za
und die Informationstabelle dazu:
Informationen:
pid | id | name | info
0 0 xy i1
1 0 xy i2
2 1 yz i3
3 1 yz i4
4 0 xy i5
...
jetzt bin ich in der Lage alle Informationen über folgende php auszulesen:
hier die entsprechende Klasse mit der ich alles aufrufe:
Das funktioniert einwandfrei.
Jetzt möchte ich aber nur für bestimmte User alle abgelegten Informationen haben.
also wenn ich etwa eine Klasse mit einem Listview gestalte wie diese:
Möchte ich, wenn ich einen User auswähle auch nur dessen Informationen abfangen.
Also wenn ich hier...
pid | id | name | info
0 0 xy i1
1 0 xy i2
2 1 yz i3
3 1 yz i4
4 0 xy i5
... Alle Informationen für yz in dem Listview haben möchte, versuche ich das über erst über diese Klasse:
und versuche die passenden Informationen über diese PHP zu holen:
Wenn ich jetzt jedoch aus dem Listview einen User wähle bekomme ich folgende Log:
Beim Aufrufen des Listview:
Bei der Auswahl von TEST2:
Vielleicht könnt Ihr mir weiter helfen, warum ich mich hier aufhänge.
Vielen Dank für Eure Hilfe!
Chris
ich habe soweit folgendes System:
Ich lege über meine App einen User über eine php-Krücke in meiner MySQL-Datenbank an und vergebe eine ID.
jetzt rufe ich den User in meiner App auf und möchte alle für ihn angelegten Informationen auslesen.
Also habe ich im Vorfeld natürlich mit Hilfe meiner App bereits beliebige Informationen angelegt und den einzelnen Usern dazu auch eine PID verliehen.
Meine SQL-Tabelle sieht also in etwa so aus:
User:
id | name
0 xy
1 yz
2 za
und die Informationstabelle dazu:
Informationen:
pid | id | name | info
0 0 xy i1
1 0 xy i2
2 1 yz i3
3 1 yz i4
4 0 xy i5
...
jetzt bin ich in der Lage alle Informationen über folgende php auszulesen:
PHP:
...
$result = mysql_query("SELECT *FROM informationen") or die(mysql_error());
if (mysql_num_rows($result) > 0)
{
$response["products"] = array();
while ($row = mysql_fetch_array($result))
{
$product = array();
$product["pid"] = $row["pid"];
$product["id"] = $row["id"];
$product["user"] = $row["user"];
$product["info"] = $row["info"];
array_push($response["products"], $product);
}
$response["success"] = 1;
...
hier die entsprechende Klasse mit der ich alles aufrufe:
Code:
public class TweenerAll extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
private static String url_all_products = "http://10.10.0.0/get_all.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_ID = "id";
private static final String TAG_USER = "user";
private static final String TAG_INFO = "info";
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tweener);
TextView txtNamear = (TextView) findViewById(R.id.Verlauftxt);
txtNamear.setText("Gesamtverlauf");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String pid = ((TextView) view.findViewById(R.id.pidEdit)).getText().toString();
Intent in = new Intent(getApplicationContext(),
EditInfoActivity.class);
in.putExtra(TAG_PID, pid);
startActivityForResult(in, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(TweenerAll.this);
pDialog.setMessage("Lade Informationen. Bitte warten...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all, "GET", params);
Log.d("All Products: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
products = json.getJSONArray(TAG_PRODUCTS);
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String pid = c.getString(TAG_PID);
String id = c.getString(TAG_ID);
String user = c.getString(TAG_USER);
String info = c.getString(TAG_INFO);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PID, pid);
map.put(TAG_ID, id);
map.put(TAG_USER, user);
map.put(TAG_INFO, info);
productsList.add(map);
}
} else {
Context context = getApplicationContext();
CharSequence text = "Es konnten keine aktuellen Informationen gefunden werden!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent i = new Intent(getApplicationContext(),
AllProductsActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
TweenerAll.this, productsList,
R.layout.list_item_tweener, new String[] { TAG_PID, TAG_ID,
TAG_USER, TAG_INFO},//
new int[] {R.id.pidEdit, R.id.idEdit, R.id.userEdit, R.id.infoEdit, });
setListAdapter(adapter);
}
});
}}}
Das funktioniert einwandfrei.
Jetzt möchte ich aber nur für bestimmte User alle abgelegten Informationen haben.
also wenn ich etwa eine Klasse mit einem Listview gestalte wie diese:
Code:
public class AllProductsActivity extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
private static String url_all_products = "http://10.10.0.1/get_all_products.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_ID = "id";
private static final String TAG_USER = "user";
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_products);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String pid = ((TextView) view.findViewById(R.id.idView)).getText().toString();
Intent in = new Intent(getApplicationContext(),
TweenerActivity.class);
in.putExtra(TAG_ID, pid);
startActivityForResult(in, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100)
{
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
class LoadAllProducts extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(AllProductsActivity.this);
pDialog.setMessage("Lade User. Bitte warten...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args)
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json1 = jParser.makeHttpRequest(url_all_products, "GET", params);
Log.d("All Products: ", json1.toString());
try
{
int success = json1.getInt(TAG_SUCCESS);
if (success == 1)
{
products = json1.getJSONArray(TAG_PRODUCTS);
for (int i = 0; i < products.length(); i++)
{
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_ID);
String user = c.getString(TAG_USER);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_USER, user);
productsList.add(map);
}
} else {
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url)
{
pDialog.dismiss();
runOnUiThread(new Runnable()
{
public void run()
{
ListAdapter adapter = new SimpleAdapter( AllProductsActivity.this, productsList, R.layout.list_item, new String[]
{
TAG_ID, TAG_USER
},
new int[]
{
R.id.idView, R.id.userView
});
setListAdapter(adapter);
}
});
}
}
Möchte ich, wenn ich einen User auswähle auch nur dessen Informationen abfangen.
Also wenn ich hier...
pid | id | name | info
0 0 xy i1
1 0 xy i2
2 1 yz i3
3 1 yz i4
4 0 xy i5
... Alle Informationen für yz in dem Listview haben möchte, versuche ich das über erst über diese Klasse:
Code:
public class TweenerActivity extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
private static String url_all_details = "http://10.10.0.0/get_all_measures_details.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_ID = "id";
private static final String TAG_USER = "user";
private static final String TAG_METALL = "info";
String id;
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tweener);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String pid = ((TextView) view.findViewById(R.id.pidEdit)).getText().toString();
Intent in = new Intent(getApplicationContext(),
EditInfoActivity.class);
in.putExtra(TAG_PID, pid);
startActivityForResult(in, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(TweenerActivity.this);
pDialog.setMessage("Lade Informationen. Bitte warten...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id));
JSONObject json = jParser.makeHttpRequest(url_all_details, "GET", params);
Log.d("All details: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
products = json.getJSONArray(TAG_PRODUCTS);
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String pid = c.getString(TAG_PID);
String id = c.getString(TAG_ID);
String user = c.getString(TAG_USER);
String info = c.getString(TAG_INFO);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PID, pid);
map.put(TAG_ID, id);
map.put(TAG_USER, user);
map.put(TAG_INFO, info);
productsList.add(map);
}
} else {
Context context = getApplicationContext();
CharSequence text = "Es konnten keine aktuellen Informationen gefunden werden!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent i = new Intent(getApplicationContext(),
AllUsersActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
TweenerActivity.this, productsList,
R.layout.list_item_tweener, new String[] { TAG_PID, TAG_ID,
TAG_USER, TAG_INFO},
new int[] {R.id.pidEdit, R.id.idEdit, R.id.userEdit, R.id.infoEdit});
setListAdapter(adapter);
}
});
}}}
und versuche die passenden Informationen über diese PHP zu holen:
PHP:
...
if (isset($_GET["id"])) {
$id = $_GET['id'];
$result = mysql_query("SELECT *FROM daten WHERE id = $id");
if (mysql_num_rows($result) > 0) {
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
$product = array();
$product["pid"] = $result["pid"];
$product["user"] = $result["user"];
$product["info"] = $result["info"];
array_push($response["products"], $product);
}
$response["success"] = 1;
...
Wenn ich jetzt jedoch aus dem Listview einen User wähle bekomme ich folgende Log:
Beim Aufrufen des Listview:
Code:
02-17 22:11:49.772: D/All Products:(4462): {"success":1,"products":[{"id":"10","user":"TEST"},{"id":"11","user":"TEST2"},{"id":"12","user":"TEST3"}]}
02-17 22:11:55.267: D/dalvikvm(4462): GC_CONCURRENT freed 297K, 5% free 9238K/9671K, paused 0ms+10ms
Bei der Auswahl von TEST2:
Code:
02-17 22:11:55.310: E/JSON Parser(4462): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
02-17 22:11:55.311: D/All details:(4462): {"success":1,"products":[{"id":"10","user":"TEST"},{"id":"11","user":"TEST2"},{"id":"12","user":"TEST3"}]}
02-17 22:11:55.312: W/System.err(4462): org.json.JSONException: No value for pid
02-17 22:11:55.312: W/System.err(4462): at org.json.JSONObject.get(JSONObject.java:354)
02-17 22:11:55.313: W/System.err(4462): at org.json.JSONObject.getString(JSONObject.java:510)
02-17 22:11:55.313: W/System.err(4462): at pivate.appcenter.testapp.TweenerActivity$LoadAllProducts.doInBackground(TweenerActivity.java:176)
02-17 22:11:55.314: W/System.err(4462): at pivate.appcenter.testapp.TweenerActivity$LoadAllProducts.doInBackground(TweenerActivity.java:1)
02-17 22:11:55.314: W/System.err(4462): at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-17 22:11:55.315: W/System.err(4462): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-17 22:11:55.315: W/System.err(4462): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-17 22:11:55.316: W/System.err(4462): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-17 22:11:55.316: W/System.err(4462): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-17 22:11:55.318: W/System.err(4462): at java.lang.Thread.run(Thread.java:861)
Vielleicht könnt Ihr mir weiter helfen, warum ich mich hier aufhänge.
Vielen Dank für Eure Hilfe!
Chris