我将此活动与3个ListFragments一起作为标签(" MEALS"," DRINKS"," DESSERT"},这些标签都从其各个表格中检索了列表项目mysql数据库并显示在各自的tabs.ALL上。 现在我将应用程序扩展到涉及checkablelayouts,这很顺利,但整个戏剧开始时,我为每个列表片段添加了一个customAdapter。当我运行应用程序时,“饭菜”选项卡不仅会检索并显示其表格内容,还会显示内容当我滑到饮料标签时,它什么也没显示,当我滑到甜点标签时,它什么都没显示。但是当我刷回饮料时,它显示其他表格的内容而不是他的。我是什么&# 39;我说的是,一切都只是混淆了。标签中的任何一个都不再显示各自的数据。 在添加自定义适配器
之前,以前是MealsFragment Activity,它曾经运行良好public class MealsFragment extends ListFragment {
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> mealsList;
// url to get all products list
private static String url_all_meals = "http://10.180.35.102/dbase_connect/get_all_meals.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_MEALS = "meals";
private static final String TAG_MID = "mid";
private static final String TAG_NAME = "name";
private static final String TAG_PRICE = "price";
// products JSONArray
JSONArray meals = null;
private ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_meals, container, false);
return rootView;
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
// Hashmap for ListView
mealsList = new ArrayList<HashMap<String, String>>();
// Loading meals in Background Thread
new LoadAllMeals().execute();
// Get listview
lv = getListView();
lv.setItemsCanFocus(false);
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllMeals extends AsyncTask<String, String, String> {
/**
* getting All Meals from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_meals, "GET", params);
// Check your log cat for JSON response
Log.d("All Meals: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Meals found
// Getting Array of Meals
meals = json.getJSONArray(TAG_MEALS);
// looping through All meals
for (int i = 0; i < meals.length(); i++) {
JSONObject c = meals.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_MID);
String name = c.getString(TAG_NAME);
String price = c.getString(TAG_PRICE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_MID, id);
map.put(TAG_NAME, name);
map.put(TAG_PRICE, price);
// adding HashList to ArrayList
mealsList.add(map);
}
} /*else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
*/
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all meals
// pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
String[] from = new String[] {TAG_MID,TAG_NAME,TAG_PRICE};
int[] to = new int[] { R.id.mid, R.id.name, R.id.price};
ListView listView = (ListView)getActivity().findViewById(android.R.id.list);
ListAdapter adapter = new MealsCustomAdapter(
getActivity(), mealsList,
R.layout.meals_list_item, from,
to);
// updating listview
listView.setAdapter(adapter);
}
});
}
}
}
这是MealsCustomAdapter
public class MealsCustomAdapter extends SimpleAdapter {
public MealsCustomAdapter(Context context, List<? extends Map<String, String>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// The item we want to get the view for
// --
// final Item item = getItem(position);
// Re-use the view if possible
// --
View row = super.getView(position, convertView, parent);
if (row == null) {
// LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = li.inflate(R.layout.meals_list_item, parent, false);
}
return row;
}
@Override
public boolean hasStableIds() {
return true;
}
private LayoutInflater li;
private class ViewHolder {
public ViewHolder(View root) {
ids = (TextView) root.findViewById(R.id.mid);
names = (TextView) root.findViewById(R.id.name);
prices = (TextView) root.findViewById(R.id.price);
layout = (CheckableRelativeLayout) root.findViewById(R.id.layout);
}
public TextView ids;
public TextView names;
public TextView prices;
public CheckableRelativeLayout layout;
}
}
这是listfragment的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:choiceMode="multipleChoice"/>
</LinearLayout>
meal_list_items的Xml布局
<com.example.tabs.tablayout.CheckableRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/layout"
android:orientation="vertical" >
<!-- Meals id (mid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/mid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/itemCheckBox"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
<TextView
android:id="@+id/price"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_below="@id/name"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:layout_toLeftOf="@id/itemCheckBox"
android:textSize="9dip"
android:textStyle="bold"
/>
<!-- -->
</com.example.tabs.tablayout.CheckableRelativeLayout>
其他2个标签(DRINKS和DESSERT)具有相似的代码
这里的主要问题是,Meals选项卡确实显示了数据库中MEALS表的内容,就像我添加自定义适配器之前一样。 我将不胜感激任何帮助。谢谢
答案 0 :(得分:1)
我正在附加示例代码,希望它能帮助您解决问题,但此代码没有自定义适配器。让我们看看,它会以某种方式帮助你。让我知道所需的任何其他帮助.package com.desirecharge;
//Suhan Gorya
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class DTH extends ListFragment {
TextView tvItemName;
ListView lv;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "http://charge.com/Android/browse_plan.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "price";
private static final String TAG_NAME = "validity";
View rootView;
// products JSONArray
JSONArray products = null;
String msg= "No Plan Available";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.all_products, container, false);
/* // lv = (ListView) rootView.findViewById(android.R.id.list);
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading tarif plan");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
*/
// Loading products in Background Thread
// new LoadAllProducts().execute();
return rootView;
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading meals in Background Thread
new LoadAllProducts().execute();
// Get listview
lv = getListView();
lv.setItemsCanFocus(false);
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// 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);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getActivity(),
Test.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
//pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}`
all_product.xml`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>`
list_item.xml`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
</LinearLayout>`
Let me know, if any help require.