我正在开发一个android应用程序。我想从我的数据库中获取数据并在textview中显示它。我正在使用Mysql服务器。 mylay.xml是我的xml文件的名称,其中包含三个textview。在我的代码中,我手动将一些字符串传递给数据库并获取与传递的字符串匹配的记录。
package com.example.festipedia_logo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockListFragment;
import com.example.festipedia_logo.Searchpage.LoadAllProducts;
//import com.example.connection.disp;
import android.app.Activity;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class calledclass extends SherlockFragment {
ArrayAdapter<String> adapter;
String[] city;
// Progress Dialog
private ProgressDialog pDialog;
String nameeve;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
JSONArray products = null;
EditText b;
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
// private static String url_all_products = "http://192.168.43.185:8080/festipedia/get_product_details.php";
private static String url_all_products = "http://192.168.43.185:8080/festipedia/myall.php";
Button a;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "product";
// products JSONArray
//JSONArray products = null;
TextView one;
TextView two;
TextView three;
String n,ll,m;
Spinner spinner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.second);
View rootView = inflater.inflate(R.layout.mylay, container, false);
// nameeve=getArguments().getString("message");
// setContentView(R.layout.all_products);
one = (TextView) rootView.findViewById(R.id.textView1);
two = (TextView) rootView.findViewById(R.id.textView2);
three = (TextView) rootView.findViewById(R.id.textView3);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
// Loading products in Background Thread
// new LoadAllProducts().execute();
return rootView;
// Get listview
//ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
}
// Response from Edit Product Activity
//@Override
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//List<NameValuePair> params = new ArrayList<NameValuePair>();
//Here i am sending some name gan to the database so that it returns the details where name is gan
params.add(new BasicNameValuePair("eventname", "gan"));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
// Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
//products = json.getJSONArray(TAG_PRODUCTS);
/*
JSONArray productObj = json
.getJSONArray(TAG_PRODUCTS); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0); */
JSONArray productObj = json
.getJSONArray(TAG_PRODUCTS); // JSON Array
// get first product object from JSON Array
JSONObject c = productObj.getJSONObject(0);
/*products = json.getJSONArray(TAG_PRODUCTS);
JSONObject c = products.getJSONObject(0);
*/
/* n = c.getString("eventname");
ll = c.getString("collegename");
m = c.getString("location");
*/
one.setText(c.getString("eventname"));
two.setText(c.getString("collegename"));
three.setText(c.getString("location"));
} else {
// no products found
// Launch Add New product Activity
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
}
答案 0 :(得分:0)
我认为有一个问题是,您的课程会延伸到SherlockFragment
,因此您需要使用getSherlockActivity()
代替getActivity()
从这里改变
pDialog = new ProgressDialog(getActivity());
到
pDialog = new ProgressDialog(getSherlockActivity());
以及您使用getActivity()
的其他位置。
也只是声明
的全局变量JSONParser jParser;
并且不会在onCreateView()
方法上初始化它。
jParser = new JSONParser();