0

I am new to listView and search a lot of tutorial but so confused because tutorial teach a different style and so complicated. Basically i want to create a listView with the image and text when i press Favourite tab.

The only thing i confuse is the coding for FavouriteFragment.java as i not sure whether it is correct. The listView doesn't appear. Hope anybody can help me in this.

This is my activity_favourite_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5ba4e5"
android:orientation="vertical" >

<TextView
    android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

This is my single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="90px"
    android:layout_height="60dp"
    android:layout_marginLeft="5px"
    android:layout_marginRight="30px"
    android:layout_marginTop="5px"
    android:layout_weight="0.68"
    android:src="@drawable/ic_launcher" >

</ImageView>

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:layout_gravity="center"
    android:text="@+id/label"
    android:textSize="30px" />

</LinearLayout>    

MainActivity.java

import android.app.ActionBar;
import android.content.Intent;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import com.example.sgrecipe.TabsAdapter;

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Recipes", "Favourites", "Quiz" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

// insert option menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

FavouriteFragment.java

import com.example.sgrecipe.MobileArrayAdapter;
import android.app.Fragment;
import android.app.ListActivity;
import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FavouriteFragment extends Fragment{

String[] Recipe = new String[] { "Chinese Food", "Malay Food", "Indian        Food", "Others"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle    savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.activity_favourite_fragment, null);

    MobileArrayAdapter adapter = new MobileArrayAdapter(this.getActivity(), Recipe);
    ListView listView = (ListView) getActivity().findViewById(R.id.listview);

    listView.setAdapter(adapter);

    return root;
}
}

MobileArrayAdapter.java

import com.example.sgrecipe.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.single_row, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.single_row,
            parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
    // Change icon based on name
    String s = values[position];
    System.out.println(s);
    if (s.equals("Chinese Food")) {
        imageView.setImageResource(R.drawable.chinese);
    } else if (s.equals("Malay Food")) {
        imageView.setImageResource(R.drawable.malay);
    } else if (s.equals("Indian Food")) {
        imageView.setImageResource(R.drawable.indian);
    } else {
        imageView.setImageResource(R.drawable.others);
    }
    return rowView;
}
}
2
  • 1
    Can you also paste the code of MobileArrayAdapter Commented Feb 11, 2015 at 16:07
  • just uploaded the mobileArrayAdapter.java Commented Feb 12, 2015 at 3:33

1 Answer 1

3

Your problem is how you are retrieving the ListView, just modify this line:

ListView listView = (ListView) getActivity().findViewById(R.id.listview);

to look like this:

ListView listView = (ListView) root.findViewById(R.id.listview);

The list view is part of the root view of the fragment so it needs to be found in that view, not in your MainActivity

Sign up to request clarification or add additional context in comments.

2 Comments

But there is another problem with the TabsAdapter. case 1: return new FavouriteFragment(); It says that type cannot convert from FavouriteFragment to Fragment
FavouriteFragment needs to extend android.support.v4.app.Fragment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.