Unable to compile this code and run. getting issue in lv.setAdapter(new VcAdapter (this));
kindly help. If I try to not pass (this)
, then code compile fine, but run time getting error stating content need to have listview
.
import java.util.ArrayList;
import com.vaishnavismeclass.tiruppavai.tab.R;
import com.vaishnavismeclass.tiruppavai.tab.SingleRow;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class EnglishFragment extends Fragment {
Context context = null;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_english, container, false);
ListView lv = (ListView) rootView.findViewById(R.id.list);
lv.setAdapter(new VcAdapter (this));
return rootView;
}
}
class SingleRow
{
String pasuram;
int img;
SingleRow(String pasuram, int img)
{
this.pasuram=pasuram;
this.img=img;
}
}
class VcAdapter extends BaseAdapter
{
ArrayList<SingleRow> list;
Context context;
VcAdapter(Context c)
{
context = c;
list = new ArrayList<SingleRow>();
//get resources using context
Resources res=c.getResources();
String[] pasuram_en = res.getStringArray(R.array.pasuram_en);
//String[] pasuram_ta = res.getStringArray(R.array.pasurams_ta);
// String[] pasuram_te = res.getStringArray(R.array.pasurams_te);
int[] imgs = {R.drawable.p1,R.drawable.p1,R.drawable.p1,R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9,R.drawable.p10,R.drawable.p11,R.drawable.p12,R.drawable.p13,R.drawable.p14,R.drawable.p15,R.drawable.p16,R.drawable.p17,R.drawable.p18,R.drawable.p19,R.drawable.p20,R.drawable.p21,R.drawable.p22,R.drawable.p23,R.drawable.p24,R.drawable.p25,R.drawable.p26,R.drawable.p27,R.drawable.p28,R.drawable.p29,R.drawable.p30,R.drawable.p1,R.drawable.p1};
for (int i=0;i<pasuram_en.length;i++)
{
//list.add(new SingleRow(pasuram_en[i], imgs[i]));
list.add(new SingleRow(pasuram_en[i], imgs[i]));
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int i) {
// TODO Auto-generated method stub
return list.get(i);
}
@Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, viewGroup, false);
TextView pasuram = (TextView) row.findViewById(R.id.textView1);
ImageView img = (ImageView) row.findViewById(R.id.imageView1);
SingleRow temp=list.get(i);
pasuram.setText(temp.pasuram);
img.setImageResource(temp.img);
return row;
}
}
Right-click on that folder and click on the new package and name it fragments. Now inside this newly created folder create two blank fragments and name them Fragment1 & Fragment2. Navigate to the app > res > layout > fragment1.xml and add the below code to that file. Below is the code for the fragment1.xml file.
To use the BaseAdapter with a ListView, a concrete implementation the BaseAdapter class that implements the following methods must be created: Before we create our custom BaseAdapter implementation, we need to create the layout for the ListView row and also a model for the items in the ListView.
Now lets discuss BaseAdapter class. BaseAdapter is a common base class of a general implementation of an Adapter that can be used in ListView, GridView, Spinner etc. Whenever you need a customized list in a ListView or customized grids in a GridView you create your own adapter and extend base adapter in that.
BaseAdapter, as it's name implies, is the base class for so many concrete adapter implementations on Android. It is abstract and therefore, cannot be directly instantiated. If your data source is an ArrayList or array, we can also use the ArrayAdapter construct as an alternative. Note that ArrayAdapter itself extends from BaseAdapter.
Change
lv.setAdapter(new VcAdapter (this));
to
lv.setAdapter(new VcAdapter(getActivity()));
You need to pass activity context.
getActivity()
Return the Activity this fragment is currently associated with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With