使用Android从editext中检索MySQL数据

时间:2015-12-09 16:25:05

标签: android

我正在尝试从MySQL数据库中检索数据(我通过phpMyAdmin访问)并尝试在EditText中显示数据。我不知道我在哪里弄错了,但想摆脱这个问题。

log cat中的错误显示城市没有值。我知道我犯了错误,但我是Android新手。以下是我的代码:

MainActivity.java

# class Author
# assumes has_many :books
# and     has_many :articles
def writings
  if books_count.nonzero?
    books
  else
    articles
  end
end

PHP脚本

public class MainActivity extends Activity {

    private ProgressDialog pDialog;
    String id;
    Button btn;
    EditText txt1,txt2;
    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String,String>> result; 
    private static final String url = "http://10.0.2.2/letstry/value.php";
    private static final String tag_success = "success";
    private static final String tag_cities = "cities";
    private static final String tag_id = "id";
    private static final String tag_city = "city_name";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        btn=(Button)findViewById(R.id.select);

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

        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                new loadresult().execute();
            }
        });
    }

    class loadresult extends AsyncTask<String,String,String>
    {

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
             int success;
             try {

                 // Building Parameters
                 List<NameValuePair> params = new ArrayList<NameValuePair>();
                 params.add(new BasicNameValuePair("id", id));

                 // getting product details by making HTTP request
                 // Note that product details url will use GET request
                 JSONObject json = jsonParser.makeHttpRequest(
                         url, "GET", params);

                 // check your log for json response
                 Log.d("Single Product Details", json.toString());

                 // json success tag
                 success = json.getInt(tag_success);
                 if (success == 1) {
                     // successfully received product details
                     JSONArray listObj = json
                             .getJSONArray(tag_cities); // JSON Array

                     // get first product object from JSON Array
                     JSONObject list = listObj.getJSONObject(0);

                     // product with this pid found
                     // Edit Text
                     txt1 = (EditText) findViewById(R.id.one1);
                    txt2 = (EditText) findViewById(R.id.two1);

                     // display product data in EditText
                     txt1.setText(list.getString(tag_id));
                     txt2.setText(list.getString(tag_city));

                 }else{
                     // product with pid not found
                 }
             } catch (JSONException e) {
                 e.printStackTrace();
             }
            return null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要使用线程处理程序,因为在Android中,您无法从另一个线程中运行的方法更改视图元素(在主线程中运行)。

这是文档中的一个很好的axample。 另一个很好的例子已经回答here

希望它有所帮助。