Listview inside tabpager fragment not showing - java

Im trying to add a listview inside a fragment witch is also inside a tabhost. the app is not crashing or anything, but the list view is not showing anything.
Here is my code:
Class for the tabPager
public class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FragmentHome();
case 1:
return new FragmentBadges();
case 2:
return new FragmentBenefits();
}
return null;
}
Following is the fragment where I want the listview:
public class FragmentBadges extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_badges_list, null);
BadgesListAdapter adapter = new BadgesListAdapter(this.getActivity(), getCurrentBadges());
ListView badgeslist = (ListView) rootView.findViewById(R.id.badges_list);
badgeslist.setAdapter(adapter);
badgeslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//TODO do some
}
});
return rootView;
}
And following is the adapter:
public class BadgesListAdapter extends ArrayAdapter<String> {
private final Activity context;
private List<Badge> badges;
public BadgesListAdapter(Activity context, List<Badge> badges) {
super(context, R.layout.fragment_badges_listrow);
this.context = context;
this.badges = badges;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.fragment_badges_listrow, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.text);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(badges.get(position).getName());
imageView.setImageResource(badges.get(position).getDrawableValue());
return rowView;
}
while debugging I found out that the getView method inside the badgesListAdapter is never called.
I have this same code for another app but without the tabpager and there it all works perfectly...

Check whether getCurrentBadges() method not returns empty list. If so there is no data to show in ListView.

I was able to make it work by inhering ListFragment in FragmentBadges, following this tutorial https://www.youtube.com/watch?v=heR4zhj_wV0

Related

listView items are repeating and becoming more each time app runs

I am implementing a sample app for android and in the list view I don't use view holder so I couldn't get my answer from other answers. my list view items are repeating and becoming more each time I run the app unless I uninstall the app from emulator.
here is my code where in the first class I declare the list view and set adapter and the next class is the customized adapter(I removed unnecessary functions):
public class CrimeListFragment extends Fragment {
private ListView crimesListView;
private CrimeAdapter adapter;
#Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View CrimesList=inflater.inflate(R.layout.fragment_crime_list,container,false);
crimesListView=(ListView) CrimesList.findViewById(R.id.crimesList);
adapter=new CrimeAdapter(inflater.getContext(),R.layout.list_item_crime,CrimeLab.get(inflater.getContext()).getCrimes());
crimesListView.setAdapter(adapter);
}
public class CrimeAdapter extends ArrayAdapter {
Context _context;
List<Crime> _crimes;
static Crime currentCrime;
public CrimeAdapter( Context context, int resource, List crimes) {
super(context, resource, crimes);
_context=context;
_crimes=crimes;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View _convertView = convertView; // re-use an existing view, if one is available
if (_convertView == null)
_convertView= ((Activity)_context).getLayoutInflater().inflate(R.layout.list_item_crime,null);
currentCrime=_crimes.get(position);
((TextView)_convertView.findViewById(R.id.crimeTitleText)).setText(currentCrime.getTitle().toString());
((TextView)_convertView.findViewById(R.id.crimeDateText)).setText(currentCrime.getCrimeDate().toString());
((ImageView)_convertView.findViewById(R.id.crimeImageSolved)).setVisibility(currentCrime.getSolved() ? View.VISIBLE : View.GONE);
return _convertView;
}
public void setCrimes(List<Crime> crimes){
_crimes=crimes;
}
}
Change
_convertView= ((Activity)_context).getLayoutInflater().inflate(R.layout.list_item_crime,null);
to
_convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_crime, parent, false);
Try replacing your CrimeListFragment code with below snippet. Hope it will solve your problem.
public class CrimeListFragment extends Fragment {
private ListView crimesListView;
private CrimeAdapter adapter;
#Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View CrimesList=inflater.inflate(R.layout.fragment_crime_list,container,false);
crimesListView=(ListView) CrimesList.findViewById(R.id.crimesList);
List<Crime> _crimes = new ArrayList();
_crimes.clear();
_crimes = CrimeLab.get(inflater.getContext()).getCrimes();
adapter=new CrimeAdapter(inflater.getContext(),R.layout.list_item_crime, _crimes);
crimesListView.setAdapter(adapter);
}
public class CrimeAdapter extends ArrayAdapter {
Context _context;
List<Crime> _crimes;
static Crime currentCrime;
public CrimeAdapter( Context context, int resource, List crimes) {
super(context, resource, crimes);
_context=context;
_crimes=crimes;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View _convertView = convertView; // re-use an existing view, if one is available
if (_convertView == null)
_convertView= ((Activity)_context).getLayoutInflater().inflate(R.layout.list_item_crime,null);
currentCrime=_crimes.get(position);
((TextView)_convertView.findViewById(R.id.crimeTitleText)).setText(currentCrime.getTitle().toString());
((TextView)_convertView.findViewById(R.id.crimeDateText)).setText(currentCrime.getCrimeDate().toString());
((ImageView)_convertView.findViewById(R.id.crimeImageSolved)).setVisibility(currentCrime.getSolved() ? View.VISIBLE : View.GONE);
return _convertView;
}
public void setCrimes(List<Crime> crimes){
_crimes=crimes;
}
}

How to implement a ListView in a Fragment in Android

I have Fragment.java and its XML. In XML I put ListView and I want to update the ListView in Fragment and add some String.
I am trying to make a custom adapter. When I run the ListView in Activity with custom adapter all works great, but when I put the same code in Fragment nothing happens. I am using EventBus3.
This is my Activity send Event
EventBus.getDefault().post(new SendDataHelper(nameOfItem[myItemInt]));
This is my Fragment:
public class MyOrderFragment extends Fragment {
TextView name;
ListView listOrder;
private Context context;
List<cources> collegeList;
public MyOrderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
#Subscribe
public void onMessageEvent(SendDataHelper event){
Toast.makeText(getActivity(), event.getText(), Toast.LENGTH_SHORT).show();
String text = event.getText();
name.setText(text);
cources college;
collegeList = new ArrayList<cources>();
college = new cources();
college.name = "sharon";
collegeList.add(college);
listOrder.setVisibility(View.VISIBLE);
ListAdapter adapter = new ListAdapter(collegeList, context);
listOrder.setAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_order_fragment, container, false);
name = (TextView) rootView.findViewById(R.id.textView);
listOrder = (ListView) rootView.findViewById(R.id.listOrder);
return rootView;
}
}
This is my adapter
public class ListAdapter extends BaseAdapter
{
Context context;
List<cources> valueList;
public ListAdapter(List<cources> listValue, Context context)
{
this.context = context;
this.valueList = listValue;
}
#Override
public int getCount()
{
return this.valueList.size();
}
#Override
public Object getItem(int position)
{
return this.valueList.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItem viewItem = null;
if(convertView == null)
{
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInfiater = LayoutInflater.from(context);
convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);
viewItem.txtNamePlanche = (TextView)convertView.findViewById(R.id.Tx_name_planche);
viewItem.txtPriceBaget = (TextView)convertView.findViewById(R.id.Tx_price_baget);
viewItem.txtPricePlate = (TextView)convertView.findViewById(R.id.Tx_price_plate);
convertView.setTag(viewItem);
}
else
{
viewItem = (ViewItem) convertView.getTag();
}
viewItem.txtNamePlanche.setText(valueList.get(position).name);
viewItem.txtPriceBaget.setText(valueList.get(position).price_baget);
viewItem.txtPricePlate.setText(valueList.get(position).price_plate);
return convertView;
}
}
This is my ItemView
import android.widget.TextView;
class ViewItem
{
TextView txtNamePlanche;
TextView txtPricePlate;
TextView txtPriceBaget;
}
This is cources:
public class cources
{
public String name;
public String price_plate;
public String price_baget;
}
Change extends Fragment to extends ListFragment

java.lang.IllegalStateException: ViewPager

My application gets all images URL from server and saves that to an ArrayList and displays these images in ViewPager. But it generates a IllegalStateException. Adapter given below:
public class FullScreenImageAdapter extends PagerAdapter {
private Context _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Context activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
#Override
public int getCount() {
return this._imagePaths.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.item, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.cardImage);
Picasso.with(_activity).load(_imagePaths.get(position)).into(imgDisplay);
((ViewPager) container).addView(viewLayout, 0);
return viewLayout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
Adapter created as
FullScreenImageAdapter adapter=new FullScreenImageAdapter(FullScreenActivity.this,all_url);
viewPager.setAdapter(adapter);
And the log looks like below:
java.lang.IllegalStateException: The application's PagerAdapter
> changed the adapter's contents without calling
> PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1,
> found: 3 Pager id: com.wat.clickzy:id/view_pager Pager class: class
> android.support.v4.view.ViewPager Problematic adapter: class
> com.wat.clickzy.FullScreenImageAdapter
Please help me
You need to call notifysetdatachanged on the adapter that you're using, every time you're adding/removing something to that adapter.
Look here for even more clarity.

viewpagerindicator: listview empty in fragment empty on second time showing

I've a NavigationDrawer to start fragments. I've a fragment with 4 tabs from TabPageIndicator with fragments which contains a listview.
If I click the entry in the menu again which launches the fragment, the elements are gone. Sometimes if I switch between them they are gone too. I also have to swipe sometimes to get them back.
ArchivFragment
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
View view = layoutInflater.inflate(R.layout.fragment_archiv, viewGroup, false);
List<String> titles=new ArrayList<String>();
titles.add(getString(R.string.category_animal));
titles.add(getString(R.string.category_earth));
titles.add(getString(R.string.category_water));
titles.add(getString(R.string.category_air));
FragmentPagerAdapter adapter = new ArchivTabAdapter(getFragmentManager(), titles);
ViewPager pager = (ViewPager)view.findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator)view.findViewById(R.id.indicator);
indicator.setViewPager(pager);
return view;
}
ArchivTabAdapter using from an example
public class ArchivTabAdapter extends FragmentPagerAdapter {
private List<String> tabs;
public ArchivTabAdapter(FragmentManager fm, List<String> tabs) {
super(fm);
this.tabs = tabs;
}
#Override
public Fragment getItem(int position) {
return ArchivListFragment.newInstance(position);
}
#Override
public CharSequence getPageTitle(int position) {
return tabs.get(position % tabs.size()).toUpperCase(Locale.getDefault());
}
#Override
public int getCount() {
return tabs.size();
}
}
ArchivListFragment
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View view = (View) inflater.inflate(R.layout.archiv_list, viewGroup, false);
ListView lv = (ListView) view.findViewById(R.id.tab);
ArrayList<Item> al = new ArrayList<Item>();
al = getArrayListToApply(filter);
lv.setAdapter(new ArchivArrayAdapter(getActivity(), al));
return view;
}
ArchivArrayAdapter
similar to ArchivTabAdapter
update 1
it seems, that I've to scroll over all items (especially the last one) to get the others back
ArchivTabAdapter
similar problem
change from
android.support.v4.app.FragmentPagerAdapter to android.support.v4.app.FragmentStatePagerAdapter

Unable to get OnItemClickListener to function for Android in Java

I am fairly new to Java and have had little experience with listviews. I have been able to successfully add items to a listview with a custom list adapter. However, I now want to perform actions when each item is touched.
I have not been able to get the OnItemClickListener event to execute when a list item is touched, i am not sure where the problem lies.
Code:
public class FragmentA extends Fragment implements OnItemClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment_a, container, false);
ListView listView = (ListView)V.findViewById(R.id.list);
getData data = getData.getMyData();
CustomList adapter = new
CustomList(getActivity(), data.Headline.toArray(new String[data.Headline.size()]), data.Description.toArray(new String[data.Description.size()]), data.imageId.toArray(new Integer[data.imageId.size()]));
listView.setAdapter(adapter);
return V;
}
public void onItemClickListener(AdapterView<?> parent, View view, int position, long id)
{
Log.e("CLICKED","CLICKED");
}
}
Also if it helps, here is the code to the custom adapter class:
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] titleId;
private final String[] descriptionId;
private final Integer[] pictureid;
public CustomList(Activity context,
String[] Headline, String[] Description, Integer[] imageId) {
super(context, R.layout.single_row, Headline);
this.context = context;
this.titleId = Headline;
this.descriptionId = Description;
this.pictureid = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.single_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.tvTitle);
TextView txtDescription = (TextView) rowView.findViewById(R.id.tvDescription);
ImageView imageView = (ImageView) rowView.findViewById(R.id.ivIcon);
txtTitle.setText(titleId[position]);
txtDescription.setText(descriptionId[position]);
imageView.setImageResource(pictureid[position]);
return rowView;
}
}
You have to use listView.setOnItemClickListener to link the callbacks to the view.
You can do as follows:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view,int pos, long id) {
//Your code here
}
});
But, in your case, as the activity already implements OnItemClickListener() you can simply:
listView.setOnItemClickListener(this);
Set the listener to your ListView:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("CLICKED","CLICKED");
}
});

Categories