I have images in fragment that I want to display in a fragment ListView
.
This is what I have tried, can u guess what's wrong with my code?
I made an Integer ArrayList
of drawable images
ArrayList<Integer> ex1 = new ArrayList<Integer>();
ex1.add(R.drawable.a);
ex1.add(R.drawable.b);
ex1.add(R.drawable.c);
ex1.add(R.drawable.d);
and tried to pass it to fragment like we would pass any String ArrayList
I declared in in main activity like this
public static final String Data = "list";
and then
bundle = new Bundle();
bundle.putIntegerArrayList(DATA, ex1);
Fragment fragment = new ExFragment();
fragment.setArguments(arguments);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack("");
ft.commit();
and then in fragment class
private ArrayList<Integer> images = new ArrayList<>();
in oncreate of fragment
if (getArguments() != null) {
images = getArguments().getIntegerArrayList(MainMenu.DATA);
}
and in oncreateview
View rootView = inflater.inflate(R.layout.fragment_ex, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.listview);
ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, R.id.text1, images);
listView.setAdapter(adapter);
but it gave me NullPointerException
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:395)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2724)
at android.widget.ListView.makeAndAddView(ListView.java:1811)
at android.widget.ListView.fillDown(ListView.java:697)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1641)
at android.widget.AbsListView.onLayout(AbsListView.java:2549)
at android.view.View.layout(View.java:15762)
at android.view.ViewGroup.layout(ViewGroup.java:4867)
This code works perfect with Strign ArrayList
if I change from Integer
to String and put a String ArrayList
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter{
String [] Text;
int [] Image;
Context context;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] TextItem, int[] mainImages) {
Text = TextItem;
context = mainActivity;
Image = mainImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
// Get number of items in list
public int getCount() {
return Text.length;
}
@Override
// Get position of item in list
public Object getItem(int position) {
return position;
}
@Override
// ???
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.YOURCUSTOMXML, null);
/** Get relevant view
* Set image/text resource, based on list name, based on list position
*/
holder.img=(ImageView) rowView.findViewById(R.id.IMAGEIDINCUSTOMXML);
holder.img.setImageResource(Image[position]);
holder.tv=(TextView) rowView.findViewById(R.id.TEXTIDINCUSTOMXML);
holder.tv.setText(Text[position]);
return rowView;
}
}
This class lets you create your own ListAdapter with a custom XML layout.
You can call it in your fragment instead of android.R.layout.simple_list_item_1
when you load the adapter into your ListView
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.