我在Fragment类中的Adapter类如下:
更新
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="myController">
<input type="checkbox" ng-model="isTrue" > <tt>isTrue</tt>
<p>Main solution</p>
<p ng-class="{ true: 'bg-green', false: 'bg-red' }[!!isTrue]">{ true: 'bg-green', false: 'bg-red' }[!!isTrue]</p>
<p>Alternative solutions</p>
<p ng-class="isTrue ? 'bg-green' : 'bg-red'">isTrue ? 'bg-green' : 'bg-red'</p>
<p ng-class="{ 'bg-green': isTrue, 'bg-red': !isTrue }" >{ 'bg-green': isTrue, 'bg-red': !isTrue }</p>
</div>
Item.java
public class FeedBackFragment extends Fragment {
ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
MyAdapter adapter;
ArrayList<Item> manufacturersList = new ArrayList<>();
JSONArray manufacturers = null;
// manufacturers JSON url
private static final String URL_MANUFACTURERS = "URL";
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));
adapter = new MyAdapter(getActivity(), manufacturersList);
gridView.setAdapter(adapter);
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
{
private LayoutInflater inflater;
public MyAdapter(Context context, ArrayList<Item> arrayList)
{
this.manufacturersList = arrayList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return manufacturersList.size();
}
@Override
public Object getItem(int i)
{
return manufacturersList.get(i);
}
@Override
public long getItemId(int i)
{
return manufacturersList.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
TextView name;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
}
}
}
我的gridview_item.xml:
public class Item
{
final String name;
final int drawableId;
Item(String name, int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
我无法在POJO类中填充Adapter类中的数据?我不知道如何将ArrayList manufacturerList改为POJO,我需要大家的帮助。我已经搜索并尝试了很多方法但是没有成功
已更新,导致错误行:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.totoroads.android.app.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:layout_gravity="bottom"
android:textColor="@android:color/white"
android:background="#55000000"
/>
答案 0 :(得分:0)
您应该更改Adapter的构造函数并接收Item对象的List而不是HashMap。
ArrayList manufacturersList;
private LayoutInflater inflater;
public MyAdapter(Context context, ArrayList<Item> arrayList)
{
this.manufacturersList = arrayList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return manufacturersList.size();
}
@Override
public Object getItem(int i)
{
return manufacturersList.get(i);
}
@Override
public long getItemId(int i)
{
return manufacturersList.get(i).drawableId;
}
将您的Item
类设为全局,以便Activity也可以访问它。
在AsyncTast中,而不是将数据添加到HashMap,将其添加到ArrayList
ArrayList<Item> manufacturersList = new ArrayList<>(); // declare outside your AsyncTask
//Inside AsyncTask loop
Item item = new Item(name,id); // I hope id is your drawableID
manufacturersList.add(item );