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

4 Answers 4

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.

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

Comments

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.

Comments

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

Comments

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

1 Comment

I edited my answer, but this link comes from Vogella WebSite, links won't be invalid ;)

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.