ListView not displaying data in fragment using ArrayAdapter - android

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;
}

Related

Access Fragment Layout Objects From Java Not Working

I'm trying to access from my code to listview and a textview, I read a lot of posts on the internet but I didn't find anything that works for me.
I have a blank fragment like this:
public class MyFrag extends Fragment {
//My Code
List<String> Names = new ArrayList<String>();
List<Double> Prices = new ArrayList<Double>();
List<String> Dates = new ArrayList<String>();
List<String> Hours = new ArrayList<String>();
List<String> iDs = new ArrayList<String>();
Double TotalMy = 0.00;
SimpleAdapter adapter;
public MyFrag() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//RelativeLayout rootView = (RelativeLayout) inflater.inflate(R.layout.fragment_my_frag, container, false);
//View view = inflater.inflate(R.layout.fragment_my_frag, container, false);
LayoutInflater lf = getActivity().getLayoutInflater();
View view = lf.inflate(R.layout.fragment_my_frag, container, false);
ListView listview = (ListView) getView().findViewById(R.id.list);
TextView textTotalMy = (TextView) getView().findViewById(R.id.textTotalMy1);
return view;
}
But when I open the fragment the application crashes.
Please help me
I solved it by creating the same Relative Layout programmatically

Error Image listview fragment

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);
}

ArrayAdapter fails in fragment

Hy, I am new to android and I am trying to build a ListView inside a fragment. I have a main class MainActivity extends FragmentActivity implements ActionBar.TabListener and a few inner classes.
I am using a public class PopulatePageAdapter extends FragmentPagerAdapter to return the fragment i need, and a class representing my fragment:
public class usersList extends Fragment {
private List<User> usersList = new ArrayList<User>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list, container, false);
populateUsernameList();
ArrayAdapter<User> adapter = new MyListAdapter();
ListView list = (ListView)rootView.findViewById(R.id.listView);
Log.d("Q","QQ");
list.setAdapter(adapter);//here my app fails
Log.d("Q","WW");
return rootView;
}
This is my ArrayAdapter
private class MyListAdapter extends ArrayAdapter<User> {
public MyListAdapter() {
super(getActivity().getApplicationContext(), R.layout.contact_layout, usersList);
}
public View getView(int position, View convertView, ViewGroup parent) {
View userView = convertView;
if (userView == null) {
userView = MainMenuActivity.this.getLayoutInflater().inflate(R.layout.contact_layout, parent, false);
}
User user = usersList.get(position);
ImageView imageView = (ImageView) userView.findViewById(R.id.icon);
imageView.setImageResource(user.getProfilePictureId());
TextView textView = (TextView) userView.findViewById(R.id.textViewCompleteName);
textView.setText(user.getFirstName() + " " + user.getLastName());
return userView;
}
}
I am thinking that I am not using right the ArrayAdapter but I dont see any other way to use it.
It looks like you don't parse your data (userList) to your adapter. Maybe take a look at this: http://www.vogella.com/tutorials/AndroidListView/article.html
You have to attach your data to you adapter and parse your context as well:
ArrayList<User> usersList;
Context mContext;
public MyListAdapter(Context context, ArrayList<User> myListViewData) {
this.mContext = context;
this.usersList = myListViewData;
}
You need to pass data to your adapter
ArrayList<Data> data=new ArrayList<Data>();
data= getArguments().getParcelableArrayList("data");
adapter = new DataAdapter(getActivity(),data);
listView.setAdapter(adapter);

Converting Intent to Fragment in Android

In general most of the time I work with Intent rather than Fragment because I found It somehow complex. I have a class with ListView to show some data from an array and I want to convert it in Fragment also want to pass the extras in a new fragmented class.
ListView Class:
public class MainActivity extends Activity {
ListView list;
String[] web = { "Google", "Twitter", "Windows", "Bing", "Itunes",
"Wordpress", "Drupal" };
Integer[] imageId = { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new CustomList(MainActivity.this, web, imageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,
"You Clicked at " + web[+position], Toast.LENGTH_SHORT)
.show();
Intent i = new Intent(MainActivity.this, intent.class);
i.putExtra("name", web[+position]);
startActivity(i);
}
});
}
}
CustomList Class:
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context, String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
I got a main class with three tabs created using fragment and in this class I would like to add the ListView!
public class LayoutOne extends Fragment {
public static Fragment newInstance(Context context) {
LayoutOne f = new LayoutOne();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
return root;
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
What I want to do is to show the listview in the fragment class and when a list item will be click I want to launch the New Intent to show which Item was clicked!
If you want to pass parameters into a Fragment, common practice is to create a static method that produces an instance of your Fragment with certain parameters. You already have the newInstance method, now you just need to add parameters to the arguments and return the Fragment. You should check for the arguments in your onCreateView The following is an example of how you would write one.
public class LayoutOne extends Fragment
{
public static Fragment newInstance(int someInt, String someString)
{
LayoutOne f = new LayoutOne();
Bundle args = new Bundle();
args.putInt("someInt", someInt);
args.putString("someString", someString);
f.setArguments(args);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
Bundle args = getArguments();
//From here you would check to see if your arguments are present
//proceed accordingly
return root;
}
}
For your specific case, you could pass along the String array and add it as an argument.
Hope this helps. Good luck!
Edit
The problem you're facing is that you're instantiating a ListView in your Activity when in reality you should be doing it in your Fragment. Intents aren't needed here, you just need to make sure that there is a ListView attribute to get from the layout you attach to your Fragment. The following is a small example of how that should work.
Note, this method goes in your LayoutOne class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
//Be sure to use the actual id of your list view
ListView lv = (ListView) root.findViewById(R.id.list_view_id);
//Make sure that you retrieve the values for 'web' and 'imageId' from
//the activity using the "newInstance" method.
CustomList adapter = new CustomList(getActivity(), web, imageId);
lv.setAdapter(adapter);
//From here, set your onClickListener and stuff as you did in your Activity
return root;
}
As a note, Fragments don't have the findViewById method. You have to call it on the View that you are inflating to be its layout, so in this case you inflated root and then you would call root.findViewById(R.id.some_id) to get a view.

Custom listview inside of a fragment

I am using a fragment to display a custom listview. The problem is that it doesnt show. But if a I use a normal listview it works. I know that I can use a listfragment. But I need some buttons that there are in fragment layout.
And Also I manage the framents with a tabHost.
class Contactos extends Fragment
private ListView lstContactos;
private AdLstContactos adListaContactos;
#Override
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
displayListView();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(container == null){
return null;
}
return inflater.inflate(R.layout.activity_contactos, container, false);
}
// Aqui se hace los findview
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// lstContactos = (ListView)view.findViewById(R.id.lstContactos);
// adLstContatos = new LstContactos(view.getContext());
// lstContactos.setAdapter(adLstContatos);
}
private void displayListView() {
// create an ArrayAdaptar from the String Array
adListaContactos = new AdLstContactos(getActivity().getApplicationContext());
ListView listView = (ListView) getActivity()
.findViewById(R.id.lstContactos);
// Assign adapter to ListView
listView.setAdapter(adListaContactos);
}
public class AdLstContactos extends ArrayAdapter<String>
private Context context;
private ArrayList<Contactos> datos;
private List<String> urlList;
private TextView txtNombre, txtNumero;
public AdLstContactos(Context contexto) {
super(contexto, R.layout.adaptador_lst_contactos);
this.context = contexto;
//this.datos = datos;
urlList = new ArrayList<String>();
urlList.add("test1");
urlList.add("test1");
urlList.add("test1");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Create a new view into the list.
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.adaptador_lst_contactos, parent, false);
txtNombre = (TextView)rowView.findViewById(R.id.txtNombreC);
txtNumero = (TextView)rowView.findViewById(R.id.txtNumeroC);
txtNombre.setText(urlList.get(position));
txtNumero.setText(urlList.get(position));
System.out.println("getView");
return rowView;
}
Main activitvy
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTabHost = new FragmentTabHost(this);
setContentView(mTabHost);
mTabHost.setup(this, getSupportFragmentManager(),
R.layout.activity_principal);
mTabHost.addTab(mTabHost.newTabSpec("DatosPersonales")
.setIndicator("Your Info"),DatosPersonales.class,null);
mTabHost.addTab(mTabHost.newTabSpec("Contactos")
.setIndicator("Contacts"),Contactos.class,null);
}
xml Contactos
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Button" />
<ListView
android:id="#+id/lstContactos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1" >
</ListView>
Probably you have an error in code.
Remember, if you are using custom list you have to do:
create custom adapter wchich extends by ArrayAdapter or BaseAdapter
override "getView()" function in adapter
add some data into adapter
add adapter to list by function "list.setAdapter()"
and it should works but if not then is an error and please add your code.
Here you have example
Contactos
private void displayListView() {
List<String> urlList;
urlList = new ArrayList<String>();
urlList.add("http://www.google.com");
urlList.add("http://mail.google.com");
urlList.add("http://maps.google.com");
// create an ArrayAdaptar from the String Array
adListaContactos = new AdLstContactos(getActivity().getApplicationContext(),urlList);
ListView listView = (ListView) getActivity()
.findViewById(R.id.lstContactos);
// Assign adapter to ListView
listView.setAdapter(adListaContactos);
}
AdLstContactos
public AdLstContactos(Context contexto, List<String>urlList) {
super(contexto, R.layout.adaptador_lst_contactos,urlList);
this.context = contexto;
this.urlList = urlList;
}

Categories

Resources