我有一个列表视图,下面是适配器:
public class ListViewAdapter extends ArrayAdapter<Product> {
public ListViewAdapter(Context context, int resource, List<Product> objects){
super (context, resource, objects);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if(null == v) {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
Product product = getItem(position);
ImageView img = (ImageView) v.findViewById(R.id.imageView3);
TextView txtTitle = (TextView) v.findViewById(R.id.txtTitle);
TextView txtDescription = (TextView) v.findViewById(R.id.txtDescription);
TextView txtDateandTime = (TextView) v.findViewById(R.id.txtDateandTime);
TextView txtLocation = (TextView) v.findViewById(R.id.txtLocation);
img.setImageResource(product.getImageId());
txtTitle.setText(product.getTitle());
txtDescription.setText(product.getDescription());
txtDateandTime.setText(product.getDateandTime());
txtLocation.setText(product.getLocation());
return v;
}
AsyncTask.execute(new Runnable() {
@Override
public void run() {
//TODO your background code
//pseudo code to get product, replace this code to get real products from db
productList = new ArrayList<>();
//Create a JSON parser Instance ----- Used JSON parser from Android
JSONparser jParser=new JSONparser();
//Getting JSON string from URL ------ Used JSON Array from Android
JSONArray rows=jParser.getJSONFromUrl(url);
try{
for(int i=0;i < rows.length();i++){
JSONObject e = rows.getJSONObject(i);
productList.add(new Product(e.optInt("id"),
e.optString("title"),
e.optString("price"),
e.optString("decription"),
e.optString("location")));
}
}
catch (JSONException e) {
//Log.
//
//
//
//
// e("Error", "json "+e.toString());
}
return productList;
使用以下方法填充列表视图(当前正在手动填充)
public List<Product> getProductList() {
//pseudo code to get product, replace this code to get real products from db
productList = new ArrayList<>();
productList.add(new Product(R.drawable.productimage, "Title 1 just to make", "This is description 1 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 2", "This is description 2 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 3", "This is description 3 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 4", "This is description 4 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 5", "This is description 5 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 6", "This is description 6 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 7", "This is description 7 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 8", "This is description 8 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 9", "This is description 9 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 10", "This is description 10 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 11", "This is description 11 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 12", "This is description 12 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 13", "This is description 13 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 14", "This is description 14 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 15", "This is description 15 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
productList.add(new Product(R.drawable.productimage, "Title 16", "This is description 16 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
return productList;
}
我想填充数据库中的数据;我试过下面的PHP服务器和代码,但没有工作。谢谢你的帮助。 get_all_products.php
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
//connect to db
$con=mysqli_connect("localhost","root","","ujmartdb");
// get all products from products table
$result = mysqli_query($con,"SELECT *FROM products") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["id"] = $row["id"];
$product["title"] = $row["title"];
$product["price"] = $row["price"];
$product["description"] = $row["description"];
$product["location"] = $row["location"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
AsyncTask.execute(new Runnable() {
@Override
public void run() {
//TODO your background code
//pseudo code to get product, replace this code to get real products from db
productList = new ArrayList<>();
//Create a JSON parser Instance ----- Used JSON parser from Android
JSONparser jParser=new JSONparser();
//Getting JSON string from URL ------ Used JSON Array from Android
JSONArray rows=jParser.getJSONFromUrl(url);
try{
for(int i=0;i < rows.length();i++){
JSONObject e = rows.getJSONObject(i);
productList.add(new Product(e.optInt("id"),
e.optString("title"),
e.optString("price"),
e.optString("decription"),
e.optString("location")));
}
}
catch (JSONException e) {
//Log.
//
//
//
//
// e("Error", "json "+e.toString());
}
return productList;
答案 0 :(得分:0)
可能是你有JSONException,因为你留下catch
块空白而你无法捕获
try{
for(int i=0;i < rows.length();i++){
JSONObject e = rows.getJSONObject(i);
productList.add(new Product(e.optInt("id"),
e.optString("title"),
e.optString("price"),
e.optString("decription"),
e.optString("location")));
}
}
catch (JSONException e) {
e.printStackTrace();
//Log.
//
//
//
//
// e("Error", "json "+e.toString());
}
并检查堆栈跟踪是否在Catch块中。
如果是,则检查它是否正在返回JSOn,而不是正在解析的JSON格式