ListView
does not work on fragments, I tried some codes on this, but again it continues to say - "the app is stopped"
My Main Activity
public class OrtaMain extends Fragment {
private ListView listView;
private int[] user_poster_resources={
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home
};
private int[] alt_resource={
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home,
R.drawable.home
};
private String[] konu_basliks;
private String[] konu_tarihs;
private String[] konu_iceriks;
OrtaMainAdapter adapter;
public OrtaMain() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_orta_main, container, false);
listVie = (ListView) view.findViewById(R.id.orta_main_liste);
konu_basliks = getResources().getStringArray(R.array.strin_baslik);
konu_tarihs = getResources().getStringArray(R.array.konu_tarih);
konu_iceriks = getResources().getStringArray(R.array.konu_icerik);
int i = 0;
adapter = new OrtaMainAdapter(getActivity(), R.layout.ortamain_item);
listView.setAdapter(adapter);
for (String basliklar:konu_basliks) {
OrtaMainDataProvider dataProvider = new OrtaMainDataProvider(basliklar);
adapter.add(dataProvider);
i++;
}
return view;
}
}
Data Provider
public class OrtaMainDataProvider {
private String baslik;
public OrtaMainDataProvider(String baslik) {
this.setBaslik(baslik);
}
public String getBaslik() {
return baslik;
}
public void setBaslik(String baslik) {
this.baslik = baslik;
}
}
Adapter Class
public class OrtaMainAdapter extends ArrayAdapter {
List list = new ArrayList();
public OrtaMainAdapter(@NonNull Context context, @LayoutRes int resource) {
super(context, resource);
}
@Override
public void add(@Nullable Object object) {
super.add(object);
list.add(object);
}
static class DataHandler{
TextView konu_baslik;
}
public int getCount(){
return this.list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return this.list.get(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View row;
row = convertView;
DataHandler handler;
if (convertView == null){
LayoutInflater inflater =(LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.ortamain_item, parent, false);
handler = new DataHandler();
handler.konu_baslik = (TextView) row.findViewById(R.id.orta_main_baslik);
row.setTag(handler);
}
else{
handler = (DataHandler) row.getTag();
}
OrtaMainDataProvider dataProvider;
dataProvider= (OrtaMainDataProvider) this.getItem(position);
handler.konu_baslik.setText(dataProvider.getBaslik());
return row;
}
}
and logcat information
06-27 14:31:37.836 11336-11336/com.example.cc.trafikprojesi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cc.trafikprojesi, PID: 11336
java.lang.OutOfMemoryError: Failed to allocate a 80281612 byte allocation with 16777216 free bytes and 42MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:973)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2423)
at android.content.res.Resources.loadDrawable(Resources.java:2330)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.widget.ImageView.<init>(ImageView.java:146)
at android.widget.ImageView.<init>(ImageView.java:135)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
Those codes are working well on any simple activity. When I test anothers. but when i put those codes, the listview does not work and emulator is stopping..
Also, fragment works well when I don't put the ListView
code, and fragment give error and does not work on emulator.
Do ListView
not work on fragments? Must I put only on nonfragments?