I have successfully add List View in fragment but when I add custom List View it show error.
That's my Fragment Code
public class CustomList extends Fragment {
ListView custom_list;
ListAdapter adapter;
@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_home, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
custom_list= (ListView) getActivity().findViewById(R.id.listView1);
custom_list.setAdapter(new StringArrayAdapter(getActivity(), "http://sweepstakes.vectis360.com/api/images/sweeps//280_4f6a306c94af679657ced7273b5ad4ea41.jpg","Get 40% Off on Trip to Turkey","2013-12-23 20:42:05","$23"));
}
}
That's my StringArrayAdapter Code
public class StringArrayAdapter extends BaseAdapter {
Bitmap bmp=null;
String[] image,name,create_date,retail_value;
Context ctx;
LayoutInflater myInflater;
ImageView iv;
Drawable drw;
String i,n,d,v;
public StringArrayAdapter(FragmentActivity activity, String img, String name, String c_date,String value) {
// TODO Auto-generated constructor stub
i = img;
n = name;
d = c_date;
v = value;
myInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return image.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return name[arg0];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null)
convertView = myInflater.inflate(R.layout.row_view, parent,false);
try {
drw = DownloadDrawable(image[position], "src");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iv = (ImageView)convertView.findViewById(R.id.entries_image);
iv.setImageDrawable(drw);
TextView tv1 = (TextView)convertView.findViewById(R.id.entries_name);
tv1.setText(name[position]);
TextView tv2 = (TextView)convertView.findViewById(R.id.entries_date);
tv2.setText(create_date[position]);
return convertView;
}
Drawable DownloadDrawable(String url, String src_name) throws java.io.IOException
{
return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()), src_name);
}
}