我想问一下, 我有2个班级:
1. an activity class (to get data from json)
2. a fragment class (not to do something)
我希望通过片段类从活动的json获取数据。 可以在一个类中结合Activity和Fragment吗?怎么办?
我在Fragment类中组合了活动和片段,我使用了GridView来获取数据和显示 JSON,在此片段中执行AsyncTask
这是我在25/10更新后的代码:
public class FeedBackFragment extends Fragment {
ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
MyAdapter adapter;
JSONArray manufacturers = null;
// manufacturers JSON url
private static final String URL_MANUFACTURERS ="MYURL";
public FeedBackFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.feedback_gridview_manufacturer, container, false);
GridView gridView = (GridView) view.findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(getActivity(), manufacturersList));
gridView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
// on selecting a single manufacturer
// CategoryCarActivity will be launched to show category car inside the manufacturer
Intent i = new Intent(getActivity(), CategoryCarActivity.class);
// send manufacturer id to activity to get list of cars under that manufacturer
String manufacturer_id = ((TextView) view.findViewById(R.id.manufacturer_id)).getText().toString();
i.putExtra("manufacturer_id", manufacturer_id);
startActivity(i);
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// manufacturersList = new ArrayList<>();
new LoadAllManufacturers().execute();
}
class LoadAllManufacturers extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
adapter.notifyDataSetChanged();
// dismiss the dialog after getting all manufacturers
if (pDialog.isShowing())
pDialog.dismiss();
}
}
private class MyAdapter extends BaseAdapter
{
// List<POJOManufacturer> listData = null;
LayoutInflater inflater;
Context context;
public MyAdapter(Context context, ArrayList<HashMap<String, String>> arrayList)
{
// this.context = context;
this.manufacturersList = arrayList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
if (manufacturersList != null)
return manufacturersList.size();
return 0;
}
@Override
public Object getItem(int i)
{
if (manufacturersList != null)
return manufacturersList.get(i);
return null;
}
@Override
public long getItemId(int i)
{
if (manufacturersList != null)
return manufacturersList.get(i).hashCode();
return 0;
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup)
{
ViewHolder holder;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.gridview_item, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.text);
holder.iconName = (ImageView) convertView.findViewById(R.id.picture);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(this.manufacturersList.get(i).getClass().getName());
// holder.iconName.setImageResource(this.manufacturersList.get(i).image);
return convertView;
}
public class ViewHolder
{
TextView name;
ImageView iconName;
}
}
}
我已更新并添加:manufacturerList = new ArrayList&lt;&gt ;.一切似乎都更好,并且在 getView() 方法中遇到了一些问题, 我试过,它只在gridview中显示7个空项目,而不显示内容和图像
那么如何将适配器中的数据填充到Gridview中?
答案 0 :(得分:1)
类ManufacturerFragment中的构造函数ManufacturerFragment不能应用于给定类型;
gridView.setAdapter()
使用适配器,而不是Fragment
并且new ManufacturerFragment()
不接受Context
。
我不确定你为什么认为你需要在你已经进入的Fragment类中创建new ManufacturerFragment
。你的意思是gridView.setAdapter(new MyAdapter(getActivity()))
吗?
此外,您的manufacturersList
需要加载到该适配器中,因此您需要弄明白。
您需要在大多数地方使用getActivity()
代替getActivity().getApplicationContext()
。
然后,您应该只调用new LoadAllManufacturers().execute();
或onCreateView
中的onActivityCreated
,而不是两者。否则,您正在运行两个AsyncTasks。
然后,onPostExecute
已在UI线程上运行,无需使用getActivity().runOnUiThread(new Runnable() {...
一旦弄清楚如何将ArrayList放入Adapter类,你需要在adapter.notifyDataSetChanged()
内调用onPostExecute
告诉适配器刷新数据,从而更新GridView以显示数据。