0

我有一个令人作呕的问题。

我在 MySQL-DB 上有两个数据表。第一个表(用户)是给我想用 id 删除的用户。第二个表(数据)应该为我设置的每一个信息的用户设置多个信息。

创建用户作品、为用户作品创建信息、获取所有用户作品的所有信息以及编辑单个信息。

现在我正在尝试创建一个 Activity,我可以在其中显示单个用户的信息。在我选择我的用户后,Activty 启动并通过 php 获取该用户的信息(在这种情况下只有 id、用户)。

在我得到我的用户详细信息后,android 通过 php 检查是否有单个用户的任何信息。它似乎有效,但创建的 ListView 仅显示空值。

这是java:

private ProgressDialog pDialog;
private ProgressDialog pDialog1;

JSONParser jParser = new JSONParser();
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;


private static String url_all_user_details = "http://10.10.0.2/get_all_user_details.php";
private static final String url_single_details = "http://192.168.222.10:81/get_single_details.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_DETAILS = "products";
private static final String TAG_PRODUCT = "product";
private static final String TAG_WID = "wid";    
private static final String TAG_ID = "id";
private static final String TAG_USER = "user";
private static final String TAG_INFO = "info";
private static final String TAG_COMMENT = "COMMENT";

String id;
String user;
String wid;                             
String info; 
String comment;                 
String stat;
TextView txtNamear;
JSONArray products;

@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); 

    TextView txtNamear = (TextView) findViewById(R.id.GesVerlauftxt);
    txtNamear.setText("Verlauf für: "); 
    TextView txtNamear2 = (TextView) findViewById(R.id.useruni);    
    txtNamear2.setText(user);       

    Intent i = getIntent(); 
    user = i.getStringExtra(TAG_USER);  

    Intent i3 = getIntent();    
    id = i3.getStringExtra(TAG_ID);

    Intent i1 = getIntent();    
    id = i1.getStringExtra(TAG_ID); 

    productsList = new ArrayList<HashMap<String, String>>();

    new LoadAllDetails().execute();
    new GetUserDetails().execute();

    ListView lv = getListView();            
}
@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 GetUserDetails 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(true);
        pDialog.show();
    }
    protected String doInBackground(String... params) 
    {
        runOnUiThread(new Runnable() 
        {
            public void run() 
            { 
                int success;
                try 
                {
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("id", id));

                    JSONObject json = jsonParser.makeHttpRequest(
                            url_product_details, "GET", params);
                    Log.d("Single Product Details", json.toString());                       
                    success = json.getInt(TAG_SUCCESS);

                    if (success == 1) 
                    {

                        JSONArray productObj = json.getJSONArray(TAG_PRODUCT);                      
                        JSONObject product = productObj.getJSONObject(0);                           

                        txtNamear = (TextView) findViewById(R.id.useruni);
                        txtNamear.setText(product.getString(TAG_USER));                         
                    }
                    else
                    {   
                    }
                } 
                catch (JSONException e) 
                {
                    e.printStackTrace();
                }
            }
        });
        return null;
    }
    protected void onPostExecute(String file_url) 
    {
        pDialog.dismiss();
    }
}
class LoadAllDetails extends AsyncTask<String, String, String> 
{   
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog1 = new ProgressDialog(TweenerActivity.this);
        pDialog1.setMessage("Lade Informationen. Bitte warten...");
        pDialog1.setIndeterminate(false);
        pDialog1.setCancelable(false);
        pDialog1.show();
    }
    protected String doInBackground(String... args) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("id", id));
        JSONObject json = jParser.makeHttpRequest(url_all_user_details, "GET", params);     

        Log.d("All User details ", json.toString());
        try 
        {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) 
            {
                products = json.getJSONArray(TAG_DETAILS);

                for (int i = 0; i < products.length(); i++)
                {
                    JSONObject c = products.getJSONObject(i);

                    wid    = c.getString(TAG_WID);                      
                    user  = c.getString(TAG_USER);      
                    info = c.getString(TAG_INFO);
                    comment  = c.getString(TAG_COMMENT);                    
                    stat   = c.getString(TAG_STAT);

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

                    map.put(TAG_WID, wid);
                    map.put(TAG_USER, user);
                    map.put(TAG_INFO, info);
                    map.put(TAG_COMMENT, comment);
                    map.put(TAG_STAT, stat);

                    productsList.add(map);
                }
            } 
            else 
            {       
                Intent i4 = new Intent(getApplicationContext(),NewInfoActivity.class);
                i4.putExtra(TAG_ID, id);        

                startActivityForResult(in, 100);    
            }
        } 
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url)
    {
        pDialog1.dismiss();

        runOnUiThread(new Runnable() 
        {
            public void run() 
            {           
                ListAdapter adapter = new SimpleAdapter(
                        TweenerActivity.this, productsList,
                        R.layout.list_item_tweener, new String[] { TAG_WID,
                                 TAG_USER, TAG_INFO, TAG_COMMENT, TAG_STAT },
                        new int[] {R.id.widTween, R.id.userTween, R.id.infoTween, R.id.commentTween, R.id.statTween});

                setListAdapter(adapter);
            }
        });
    }
}       

这是我的php:

//if (isset($_GET["id"])){ $id = $_GET['id'];

$result = mysql_query('set names utf8'); 
$result = mysql_query("SELECT * FROM daten WHERE id = $id")or die(mysql_error()); 


if (mysql_num_rows($result) > 0)  
{ 

    $response["products"] = array(); 
    while ($row = mysql_fetch_array($result))  
    { 

    $product = array(); 
            $product["wid"]          = $result["wid"]; 
            $product["user"]       = $result["user"]; 
        $product["info"]        = $result["info"]; 
    $product["comment"] = $result["comment"]; 
    $product["stat"]       = $result["stat"]; 

    array_push($response["products"], $product); 
    } 

    $response["success"] = 1; 

    echo json_encode($response); 
}  
else  
{ 

$response["success"] = 0; 
$response["message"] = "No products found"; 

echo json_encode($response); 
} 

Logcat 显示:

D/Single User Details(7218): {"product":[{"id":"13","user":"Test"}],"success":1} D/dalvikvm(7218): GC_CONCURRENT freed 141K , 3% 免费 8834K/9095K, 暂停 1ms+3ms D/所有用户详细信息(7218): {"success":1,"products":[{"info":null,"comment":null,"wid": null,"stat":null,"user":null},{"info":null,"comment":null,"wid":null,"stat":null,"user":null},{"info ":null,"comment":null,"wid":null,"stat":null,"user":null}]}

我真的希望你能帮助我。从这里开始无法自拔。

谢谢你们!

4

1 回答 1

0

在您的 php 文件中,您需要更改$result$rowinwhile循环

  while ($row = mysql_fetch_array($result))  
  { 
    $product = array(); 
    $product["wid"] = $row["wid"]; 
    $product["user"] = $row["user"]; 
    $product["info"] = $row["info"]; 
    $product["comment"] = $row["comment"]; 
    $product["stat"] = $row["stat"]; 

    array_push($response["products"], $product); 
  } 
于 2013-02-19T20:08:01.597 回答