Question

    import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;

public class FragmentTab2 extends SherlockFragment {

    private String[]names = {"Video", "Audio"};
    private String[]urls = {"http://c172200.r0.cf3.rackcdn.com/104824.mp4", "http://c172200.r0.cf3.rackcdn.com/106973.mp4"};

    @Override
    public View onCreateView(LayoutInflater inflater1, ViewGroup container,
            Bundle savedInstanceState) {
        // Get the view from fragmenttab2.xml
        View view = inflater1.inflate(R.layout.fragmenttab2, container, false);

        return view;

        ListView lv = (ListView)findViewById(R.id.List);
        VideoAdapter adapter = new VideoAdapter(this, names, urls);
        lv.setAdapter(adapter);

    }
}

I'm to make a listview in a fragmenttab. I have the created the interface using a tutorial. http://www.youtube.com/watch?v=54wC1lgmbKQ In the video you can see application. I'm not sure where to declare and to declare the adapter. Right now i'm declaring in in the fragmenttab1 activity it self, but this causes errors to appear, saying that "The Constructor in undefined" and that "The method findViewById(int) in undefined within the type FragmentTab2". Can anyone help me? This would really boost my school project. Thanks in advance!

The code of the adapter:

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class VideoAdapter  extends ArrayAdapter<String> {
          private final Context context;
          private final String[] names;
          private final String[] urls;

          public VideoAdapter(Context context, String[] names, String[] urls) {
            super(context, R.layout.videorow, names);
            this.context = context;
            this.names = names;
            this.urls = urls;
          }

          @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.videorow, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.textView2);
            Button button = (Button) rowView.findViewById(R.id.grootheden);
            textView.setText(names[position]);
            button.setTag(urls[position]);

            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse((String) v.getTag()), "video/mp4");
                    context.startActivity(intent);

                }
            });  

            return rowView;
          }
        } 
Was it helpful?

Solution

Try this:

ListView lv = (ListView)findViewById(R.id.List);

Should be:

ListView lv = (ListView) view.findViewById(R.id.List);

EDIT:

Also, you need to return the view at the END of your onCreate();

 return view;

Your code, fixed:

public class FragmentTab2 extends SherlockFragment {

    private String[]names = {"Video", "Audio"};
    private String[]urls = {"http://c172200.r0.cf3.rackcdn.com/104824.mp4", "http://c172200.r0.cf3.rackcdn.com/106973.mp4"};

    @Override
    public View onCreateView(LayoutInflater inflater1, ViewGroup container,
            Bundle savedInstanceState) {
        // Get the view from fragmenttab2.xml
        View view = inflater1.inflate(R.layout.fragmenttab2, container, false);

        ListView lv = (ListView) view.findViewById(R.id.List);
        VideoAdapter adapter = new VideoAdapter(this, names, urls);
        lv.setAdapter(adapter);

        return view;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top