0

I'm really new to android programming, in fact to programming itself. I'm creating an app that has tabs, and within one of the tab, i would like to add listview within it. I'm lost and stuck, pls advise

public class TabActivityQueue extends Fragment {

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View V = inflater.inflate(R.layout.activity_tab_activity_queue, container, false);
        populateListView();
        return V;
    }
 private void populateListView() {
    // Create list of items
    String[] myItems = {"Blue", "Green", "Purple", "Red"}; // Build Adapter
    // TODO: CHANGE THE [[ to a less than, ]] to greater than.
    ArrayAdapter<String> adapter = new ArrayAdapter<String> (TabActivityQueue,R.layout.da_items, myItems);
    ListView list = (ListView) findViewById(R.id.listview1);
    list.setAdapter(adapter);
 }}
Anqi Zhang
  • 27
  • 1
  • 7
  • @Aniqi can u provide where u stuck? – Spry Techies Sep 02 '14 at 07:45
  • @Aniqi go to this link it may solve http://stackoverflow.com/questions/22512833/create-listview-in-fragment-android. and also look at this http://www.mysamplecode.com/2012/08/android-fragment-example.html – Spry Techies Sep 02 '14 at 07:47

4 Answers4

1

Change your populateListView() adapter to this:

ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(),R.layout.da_items, myItems);

The problem is probably the wrong context.

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
0
ArrayAdapter<String> adapter = new ArrayAdapter<String> (TabActivityQueue,R.layout.da_items, myItems);

to

ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(),R.layout.da_items, myItems);
Piyush
  • 18,895
  • 5
  • 32
  • 63
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

Need to change something:

1) ListView list = (ListView) getView().findViewById(R.id.listview1);

2) ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActivity(),R.layout.da_items, myItems);

Note: When you'r using Fragment then you need to use getActivity() as a Context.

Piyush
  • 18,895
  • 5
  • 32
  • 63
-1

I think you should create an Adapter class. That's did the job, & you can reuse it.

Here a good tuto : http://www.vogella.com/tutorials/AndroidListView/article.html

EDIT

Well, that's the adapter example :

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 MySimpleArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.rowlayout, 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.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
    // change the icon for Windows and iPhone
    String s = values[position];
    if (s.startsWith("iPhone")) {
      imageView.setImageResource(R.drawable.no);
    } else {
      imageView.setImageResource(R.drawable.ok);
    }

    return rowView;
  }
} 
Sidd
  • 172
  • 9
  • I edited my answer, but this link comes from Vogella WebSite, links won't be invalid ;) – Sidd Sep 02 '14 at 09:25