Its my first post here but i am reading this Q&A for years and I always find an answer, but this time i cannot find it, or i cannot combine multiple answers with my problem. I hope you can help.
So I have fragment which overrides onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
db = new WineryDatabaseAdapter(getActivity());
bacveList = db.getBacve();
v = inflater.inflate(R.layout.sve_bacve, container, false);
Log.v("onCreateView", "oncreateview");
return v;
}
Then I have method where I set my data
public void getBacveItems(){
ArrayAdapter<Bacve> ad = new BacveListAdapter(getActivity(), bacveList);
lv = (ListView) v.findViewById(R.id.listSveBacve);
lv.setAdapter(ad);
lv.setOnItemClickListener(this);
Log.v("getBacveItems", "getBacveItems");
}
In that method I am calling my Adapter so i can use my listview layout
public class BacveListAdapter extends ArrayAdapter<Bacve>{
List<Bacve> bacve;
Context c;
String[] values = new String[] { "prva","druga" };
public BacveListAdapter(Context c,List<Bacve> l){
//super(c,R.layout.sve_bacve_item,l);
super(c,R.layout.sve_bacve_item,l);
Log.v("BacveListAdapter", "BacveListAdapter");
this.bacve = l;
this.c = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View iv = convertView;
TextView tv;
if (iv == null){
iv = (View)((LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.sve_bacve,parent,false);
tv = new TextView(c);
}
else{
Bacve bacva = bacve.get(position);
tv = (TextView) iv.findViewById(R.id.textNazivBacve);
tv.setText(bacva.getIme());
}
return iv;
}
}
But for some reason i am getting errors when trying to access tv. It is always null.
I think its something with views that are created/getted, and I am creating/getting some parent view in higher hierarchy. I have tried to debug it but dont know how to use those IDs in debug mode. Well not how to use it, but how to compare it so I can see is it right view.
Please help :)
I did it like this in this edit but nothing. Is it because i am using swipe views and I am getting wrong view in the first?
This is my sve_bacve_item and it has textview. I am kind 1 step forward with @Raghunandan solution but now I have problem with convertin String to holder. I am trying to resolve that.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textNazivBacve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="@+id/bacveIcon"
android:text="@string/BacveNaziv"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/bacveIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/textNazivBacve"
android:adjustViewBounds="true"
android:contentDescription="@string/Bacve"
android:maxHeight="120dp"
android:maxWidth="120dp"
android:src="@drawable/bacve_item" />
</RelativeLayout>
BR