Android ListView显示来自php的空值

时间:2013-02-19 19:27:27

标签: php android mysql null

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

我在MySQL-DB上有两个数据表。 第一个表(用户)适用于我想要使用ID删除的用户。 第二个表(数据)应为具有wids的用户设置多个信息,用于我设置的每个信息。

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

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

在我获取用户详细信息之后,如果单个用户有任何信息,则通过php检查android。 它似乎工作,但创建的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 /单个用户详细信息(7218):{“product”:[{“id”:“13”,“user”:“Test”}],“success”:1}  D / dalvikvm(7218):GC_CONCURRENT释放141K,3%免费8834K / 9095K,暂停1ms + 3ms  D /所有用户详细信息(7218):{“success”:1,“products”:[{“info”:null,“comment”:null,“wid”:null,“stat”:null,“user”:空},{ “信息”:空, “评论”:空, “妇女参与发展”:空, “统计”:空, “用户”:空},{ “信息”:空, “评论”:空,“妇女参与发展“:空,” STAT “:NULL,” 用户“:空}]}

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

谢谢大家!

1 个答案:

答案 0 :(得分:0)

在您的php文件中,您需要在$result循环中将$row更改为while

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