Error Image listview fragment - android

enter image description hereBelow is the coding i have and it seems i have problem on the
AgentAdapter adapter = new AgentAdapter(getActivity(), R.layout.list_agent, member_names, profile_pics);
How do i use image and text together for the list adapter i have in my fragment? Please help.
public class AgentFragment extends Fragment{
String[] member_names;
TypedArray profile_pics;
ListView mylistview;
String[] agentname={"Robert","Shanni","Rachel","Mady","Nikhil"};
Integer [] imgid = {R.drawable.robert,R.drawable.shanni,R.drawable.rachael,R.drawable.maddy_pic,R.drawable.nikhil_pic};
public AgentFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_layout1, null);
setupList(view);
return view;
}
private void setupList(View view){
AgentAdapter adapter = new AgentAdapter(getActivity(), R.layout.list_agent, member_names, profile_pics);
mylistview = (ListView) view.findViewById(R.id.listView);
mylistview.setAdapter(adapter);
}
}

Since its possible that the fragment has not been placed in any activity at runtime you can afford for this by changing the getActivity() to be the context from the view parameter:
private void setupList(View view){
AgentAdapter adapter = new AgentAdapter(view.getContext(), R.layout.list_agent, member_names, profile_pics);
mylistview = (ListView) view.findViewById(R.id.listView);
mylistview.setAdapter(adapter);
}

Related

IllegalArgumentException: No view found for id for fragment when trying to setView

it says IllegalArgumentException: No view found for id for fragment when I'm trying to setview.I know problem lies at 3rd and 4th line. it's News.java file news is a fragment.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.fragment_news);
listView = (ListView) getActivity().findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
Fragment.java file is look like below code :
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news, container, false);
//ButterKnife.bind(this, view);
init(view);
return view;
}
after that in init method type below code:
private void init(View view){
listView = (ListView) view.findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
}
also u don't forgot to extends Fragment

ListView not displaying data in fragment using ArrayAdapter

i have a created a fragment in which i want to display listview but my fragment is dislplayed wtihout the listview nor i am getting an error
i have read many solutions to this question but i can't find the right one.
a small help would be great.thank you!
public class ServicesFragment extends Fragment {
public TextView servicesName;
ListView listView;
String[] servicesNameArray;
int[] serviceImages = {
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera
};
public ServicesFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.services_fragment, container, false);
listView = (ListView) view.findViewById(R.id.listView_services);
ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages);
listView.setAdapter(adapter);
Resources resources = getResources();
resources.getStringArray(R.array.servicesName);
return view;
}
public class ServicesAdapter extends ArrayAdapter<String> {
Context c;
int[] servicesImageArray;
String[] servicesNameArray;
public ServicesAdapter(Context context, String[] servicesNameArray, int[] serviceImages) {
super(context, R.layout.services_layout, R.id.listView_services);
this.c = context;
this.servicesImageArray = serviceImages;
this.servicesNameArray = servicesNameArray;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.services_layout, parent, false);
ImageView serviceImages = (ImageView) view.findViewById(R.id.image_of_services);
TextView servicesName = (TextView) view.findViewById(R.id.name_of_services);
serviceImages.setImageResource(servicesImageArray[position]);
servicesName.setText(servicesNameArray[position]);
return view;}}}
You are not assigning value to the string array: use
servicesNameArray = getResources().getStringArray(R.array.servicesName);
and instead of :
ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages);
use the activity context:
ServicesAdapter adapter = new ServicesAdapter(this.getActivity(), servicesNameArray, serviceImages);
you need to notify you adapter about your data set size:
super(context, R.layout.services_layout, servicesNameArray); //change here
I think this issue cause you dont pass data in correct way
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.services_fragment, container, false);
Resources resources = getResources();
servicesNameArray = resources.getStringArray(R.array.servicesName);
listView = (ListView) view.findViewById(R.id.listView_services);
ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages);
listView.setAdapter(adapter);
return view;
}

Display Data in Fragment in Android

I'm trying to display data from SQLite and display it in the listview in fragment.This is my code and when I run it, it closes automatically. This code is inside my main activity
public static class ListDoctorFragment extends Fragment {
ListView list;
DataDB data = new DataDB();
ArrayAdapter<String> listAdapter;
public ListDoctorFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.listdoctor, container, false);
ArrayList<String> names = new ArrayList<String>();
try {
names = data.getDoctorlistDB(getActivity());
} catch (SQLException e) {
e.printStackTrace();
}
listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, names);
// set the adapter
list.setAdapter(listAdapter);
return view;
}
}
Your 'ListView list' is not initialized. It must be something like:
list = (ListView) view.findViewById(R.id.listview);

Adapter in Spinner, Android

How do i set the adapter in Spinner for the following bunch of code? I should set the adapter under
//set adapter for spinner here
public class ListViewFragment extends Fragment implements OnItemSelectedListener{
public ListViewFragment(){}
Spinner mspinner; //declare spinner globally
ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listview, container, false);
mspinner = (Spinner) view.findViewById(R.id.spinner1);
//set adapter for spinner here
return rootView;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
//do something when particular item is selected from spinner
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Try this , Hope it works
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listview, container, false);
mspinner = (Spinner) view.findViewById(R.id.spinner1);
//set adapter for spinner here
// you need to create a array which one is added to spinner
String [] name = {"A","B","C"};
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,name);
// you can use getActivity() or this
mspinner.setAdapter(adapter);
return rootView;
}
Thank you
//here are the contents its the same as you do in a listview it can be a ArrayList or a array
String[] content={"A","B","C"};
adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,content);
mspinner.setAdapter(adapter);
try this hope this will work for you!!

set list view adapter in a fragment in android

I want a custom row, so I am using a List View in an xml and inflating into a fragment. I am very confused of how to set the adapter for the list View.
I created a new adapter which extends Base Adapter. In the getView method, I really don't know what context to pass while inflating the row.xml layout.
How do I set the adapter for the list view and where?
public class ResultsFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.results_layout, container, false);
listView = (ListView)v.findViewById(R.id.results);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
loadPage(dataBean.getWhat(), dataBean.getWhere(), dataBean.getPageStart());
//resultsAdapter.setRssData(rssData);
//setListAdapter(resultsAdapter);
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Context context = getActivity().getApplicationContext();
resultsAdapter = new ResultsAdapter(context);
}
/**
* Set List Adapter
*/
private void setAdapter(){
if(listView.getAdapter() == null){
listView.setAdapter(resultsAdapter);
}
else{
resultsAdapter.notifyDataSetChanged();
}
}
}
You must extend Listfragment (instead of Fragment), and using its ListFragment.setListAdapter to set your adapter. In the adapter getView() inflate your row.. that s all
If you do not want to change your extended class, you should use listview.setAdapter(...) method. As you see in my example :
ListView productList= (ListView) getActivity().findViewById(R.id.product_list);
SampleAdapter adapter = new SampleAdapter(getActivity());
adapter.add(new SampleItem(
"Sunny LCD TV 2\" SN022L66-T1 Full HD",
R.drawable.product_sample_pic);
productList.setAdapter(adapter);

Categories

Resources