getItem不会在这里调用是我的代码它不工作如果我使用活动然后它的工作正常但在片段getItem方法不打电话请帮助
这是我的片段:
public class HomeFragment extends BaseFragment{
private static final String TAG = "HomeFragment";
private GridView gvMainItems;
private HomeFragmentGridAdapter homeFragmentGridAdapter;
private ArrayList<HashMap<String, String>> arrayList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_home,container,false);
gvMainItems = (GridView) view.findViewById(R.id.gvMainItems);
arrayList = new ArrayList<>();
for(int i = 1; i < 100; i++){
HashMap<String, String> map = new HashMap<>();
map.put("item", String.valueOf(i));
arrayList.add(map);
}
homeFragmentGridAdapter = new HomeFragmentGridAdapter(getActivity(), arrayList);
gvMainItems.setAdapter(homeFragmentGridAdapter);
return view;
}
@Override
public void onClick(View view) {
}
}
这是我的适配器:
public class HomeFragmentGridAdapter extends BaseAdapter {
private final Context context;
private final ArrayList<HashMap<String, String>> arrayList;
private ViewHolder holder;
public HomeFragmentGridAdapter(Context context, ArrayList<HashMap<String,String>> arrayList){
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View cinvertview, ViewGroup viewGroup) {
if(cinvertview == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cinvertview = inflater.inflate(R.layout.fragment_home_grid_raw, null, true);
holder.textView = (TextView) cinvertview.findViewById(R.id.textView);
cinvertview.setTag(holder);
}else{
holder = (ViewHolder) cinvertview.getTag();
}
Log.d(TAG, "getItem: "+arrayList.get(position).get("item"));
holder.textView.setText(arrayList.get(position).get("item"));
return cinvertview;
}
public class ViewHolder{
public ImageView ivGridItem;
public TextView textView;
}
}
这是xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/content_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.neodocto.activity.MainActivity"
tools:showIn="@layout/app_bar_main">
<GridView android:id="@+id/gvMainItems"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
/>
</LinearLayout>
答案 0 :(得分:1)
在HomeFragment
:
homeFragmentGridAdapter = new HomeFragmentGridAdapter(getActivity().getApplicationContext(), arrayList);
因为您的构造使用Context而不是FragmentActivity(由getActivity()
返回。
答案 1 :(得分:1)
请尝试这种方式我工作正常 更改适配器,如
private final Context context;
private final ArrayList<HashMap<String, String>> arrayList;
private ViewHolder holder;
LayoutInflater inflater;
public HomeFragmentGridAdapter(Context context, ArrayList<HashMap<String, String>> arrayList) {
this.context = context;
this.arrayList = arrayList;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View cinvertview, ViewGroup viewGroup) {
if (cinvertview == null) {
holder = new ViewHolder();
cinvertview = inflater.inflate(R.layout.list_purchase_items, null);
holder.textView = (TextView) cinvertview.findViewById(R.id.textView);
cinvertview.setTag(holder);
} else {
holder = (ViewHolder) cinvertview.getTag();
}
Log.d(TAG, "getItem: " + arrayList.get(position).get("item"));
holder.textView.setText(arrayList.get(position).get("item"));
return cinvertview;
}
public class ViewHolder {
public ImageView ivGridItem;
public TextView textView;
}
答案 2 :(得分:0)
我将gridview更改为此工作
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/gvMainItems"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:numColumns="4" />