How can i call the listview method out of the FragmentTwo class? I'm using a NavDrawer which is fragment-based, so i have to initiate the ListView in the FragmentTwo class.
This is my code:
package xy;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class FragmentTwo extends Fragment {
View rootview;
private Context context;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_two, container, false);
return rootview;
list.this.listview();
}
class list extends MainActivity{
public void listview(){
String[] listcontent = {"Test1","text2","text4"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, getView().R.layout.listviewcontent, listcontent);
ListView listView = (ListView) getView().findViewById(R.id.list);
listView.setAdapter(adapter);
}
public void registerClickCallback() {
ListView listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view;
String message = "You clicked # " + position + " which is:" + textView.getText().toString();
Toast.makeText(list.this, message, Toast.LENGTH_LONG).show();
}
});
}
}
}
My main problem is the error message
.....is not an enclosing class
when calling list.this.listview(); from the onCreate method.
This isn't the right way to call methods in your Fragment's parent Activity.
You can get the parent Activity with:
MainActivity activity = (MainActivity) getActivity();
And you can then access its methods in this style:
activity.callSomeMethodInYourActivity();
Update:
Also your onCreateView() throws an "unrechable statement" error because you have code after the return statement. This code can't be executed because the method is quit when the return statement is reached.
Complete solution:
Place this in your MainActivity.java file:
public void listview(){
String[] listcontent = {"Test1","text2","text4"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, getView().R.layout.listviewcontent, listcontent);
ListView listView = (ListView) getView().findViewById(R.id.list);
listView.setAdapter(adapter);
}
public void registerClickCallback() {
ListView listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view;
String message = "You clicked # " + position + " which is:" + textview.getText().toString();
Toast.makeText(list.this, message, Toast.LENGTH_LONG).show();
}
});
}
And your fragment should look like this:
public class FragmentTwo extends Fragment {
View rootview;
private Context context;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_two, container, false);
MainActivity activity = (MainActivity) getActivity();
activity.listview()
return rootview;
}
}
After all I don't really understand why you are not placing the listview() and registerClickCallback() methods directly in your Fragment. IMHO this would make more sense...
Related
I am making a tour guide app that is made up of one main activity with and two fragments. The top fragment contains a list of cities and when you clock on a city the bottom fragment displays a description of that city. The problem is that when I change orientation I get an error saying "Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"
Here is the city list fragment:
package com.example.tourguide;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* A simple {#link Fragment} subclass.
*/
public class CityFragment extends Fragment {
View view;
String[] cities;
String[] descriptions;
ListView listView;
DescriptionFragment text;
int mPosition;
public CityFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of member from saved state
text = (DescriptionFragment) getFragmentManager().getFragment(savedInstanceState, getString(R.string.DESCRIPTION_FRAG));
mPosition = savedInstanceState.getInt(getString(R.string.POSITION));
cities = savedInstanceState.getStringArray(getString(R.string.CITY_ARRAY));
descriptions = savedInstanceState.getStringArray(getString(R.string.DESCRIPTION_ARRAY));
text.change(descriptions[mPosition], cities[mPosition]);
}
// Get views
this.view = inflater.inflate(R.layout.fragment_city, container, false);
cities = getResources().getStringArray(R.array.cities);
descriptions = getResources().getStringArray(R.array.cities_description);
listView = this.view.findViewById(R.id.city_list);
// Create an ArrayAdapter
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, cities);
// Set adapter on the listView
listView.setAdapter(listViewAdapter);
// Set an item click listener for ListView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the selected item text from ListView
text = (DescriptionFragment) getFragmentManager().findFragmentById(R.id.fragmentBottom);
text.change(descriptions[Integer.parseInt(String.valueOf(position))], cities[Integer.parseInt(String.valueOf(position))]);
mPosition = position;
}
});
// Inflate the layout for this fragment
return this.view;
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
// Save the fragments Instance
getFragmentManager().putFragment(outState, getString(R.string.DESCRIPTION_FRAG), text);
outState.putInt(getString(R.string.POSITION), mPosition);
outState.putStringArray(getString(R.string.CITY_ARRAY), cities);
outState.putStringArray(getString(R.string.DESCRIPTION_ARRAY), descriptions);
}
}
And here is the description fragment:
package com.example.tourguide;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class DescriptionFragment extends Fragment {
TextView cityName;
TextView text;
public DescriptionFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
}
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_description, container, false);
text = view.findViewById(R.id.city_description);
cityName = view.findViewById(R.id.city_name);
return view;
}
public void change(String description, String city) {
text.setText(description);
cityName.setText(city);
}
}
The text.change method seems to be where the problem lies but how else can I implement it to save the description state?
if (savedInstanceState != null) {
// Restore value of member from saved state
text = (DescriptionFragment) getFragmentManager().getFragment(savedInstanceState, getString(R.string.DESCRIPTION_FRAG));
mPosition = savedInstanceState.getInt(getString(R.string.POSITION));
cities = savedInstanceState.getStringArray(getString(R.string.CITY_ARRAY));
descriptions = savedInstanceState.getStringArray(getString(R.string.DESCRIPTION_ARRAY));
text.change(descriptions[mPosition], cities[mPosition]);
}
Why don't you just store the state within the DescriptionFragment.
/**
* A simple {#link Fragment} subclass.
*/
public class DescriptionFragment extends Fragment {
private static final String DESCRIPTION = "DESCRIPTION";
private static final String CITY = "CITY";
TextView cityName;
TextView text;
public DescriptionFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_description, container, false);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
text = view.findViewById(R.id.city_description);
cityName = view.findViewById(R.id.city_name);
if (savedInstanceState != null) {
String description = savedInstanceState.getString(DESCRIPTION);
String city = savedInstanceState.getString(CITY);
change(description, city);
}
}
#Override
public void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
// Save the fragments Instance
outState.putString(DESCRIPTION, text.getText().toString());
outState.putString(CITY, cityName.getText().toString());
}
public void change(String description, String city) {
text.setText(description);
cityName.setText(city);
}
}
Can use the property in your xml AndroidManifest.xml
android:configChanges="keyboardHidden|orientation"
Here it will indicate to the application that you will be in charge of handling a rotation and will not restart your activity so that there is no null data.
I have to do a small app for a school project on Android, and I wanted to update my listView through a Fullscreen Dialog but I can't find a way to get the values inside my EditText fields to put add them to my ArrayLists.
Here is the code of my Dialog class:
package com.example.memo;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.ArrayList;
#SuppressLint("ValidFragment")
public class FullscreenDialog extends DialogFragment implements View.OnClickListener {
private EditText titleEdit;
private EditText infoEdit;
private EditText fullEdit;
private ArrayList<String> titleArray;
private ArrayList<String> infoArray;
private ArrayList<Integer> imageArray;
private ArrayList<String> fullArray;
private Callback callback;
public FullscreenDialog(ArrayList<String> titleArray, ArrayList<String> infoArray, ArrayList<String> fullArray, ArrayList<Integer> imageArray){
this.titleArray = titleArray;
this.infoArray = infoArray;
this.fullArray = fullArray;
this.imageArray = imageArray;
}
static FullscreenDialog newInstance(ArrayList<String> titleArray, ArrayList<String> infoArray, ArrayList<String> fullArray, ArrayList<Integer> imageArray) {
return new FullscreenDialog(titleArray,infoArray,fullArray,imageArray);
}
public void setCallback(Callback callback) {
this.callback = callback;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.FullscreenDialogTheme);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fullscreen_dialog, container, false);
ImageButton close = view.findViewById(R.id.fullscreen_dialog_close);
TextView action = view.findViewById(R.id.fullscreen_dialog_action);
EditText titleEdit = view.findViewById(R.id.fullscreen_dialog_title);
EditText infoEdit = view.findViewById(R.id.fullscreen_dialog_info);
EditText fullEdit = view.findViewById(R.id.fullscreen_dialog_full);
close.setOnClickListener(this);
action.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.fullscreen_dialog_close:
dismiss();
break;
case R.id.fullscreen_dialog_action:
this.titleArray.add(titleEdit.getText().toString());
this.infoArray.add(infoEdit.getText().toString());
this.fullArray.add(fullEdit.getText().toString());
this.imageArray.add(R.drawable.ic_launcher_foreground);
callback.onActionClick("Memo saved!");
dismiss();
break;
}
}
public interface Callback {
void onActionClick(String name);
}
}
I guess I must have made some mistakes on the way I used findViewById or something, but Android is kind of new for me and I can't seem to find where it's wrong.
titleArray, infoArray and fullArray are the ArrayLists in which is stored my data.
imageArray is my ArrayList for my images IDs.
You calling or making use of an instance variable (i.e fullEdit, titleEdit etc) that is not initialised. To correct this initialised them in method onCreateView, so it should look like this
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fullscreen_dialog, container, false);
ImageButton close = view.findViewById(R.id.fullscreen_dialog_close);
TextView action = view.findViewById(R.id.fullscreen_dialog_action);
titleEdit = view.findViewById(R.id.fullscreen_dialog_title);
infoEdit = view.findViewById(R.id.fullscreen_dialog_info);
fullEdit = view.findViewById(R.id.fullscreen_dialog_full);
close.setOnClickListener(this);
action.setOnClickListener(this);
return view;
}
With the above the class instance EditText variables are instantiated and can be use to get the text onClick method
You define the variables titleEdit, infoEdit and full Edit globally in the class but the problem is that you defined them again locally in the onCreateView method and you assigned them to the view by using findViewById .
So you have 2 kind of variable now :
the global ones ( doesn't linked yet )
the local ones ( linked to the view )
When you try to get the value of the editText you accessed to the global ones that is not linked to the View .
To solve that delete the local ones. you onCreateView should look like this :
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fullscreen_dialog, container, false);
ImageButton close = view.findViewById(R.id.fullscreen_dialog_close);
TextView action = view.findViewById(R.id.fullscreen_dialog_action);
titleEdit = view.findViewById(R.id.fullscreen_dialog_title);
infoEdit = view.findViewById(R.id.fullscreen_dialog_info);
fullEdit = view.findViewById(R.id.fullscreen_dialog_full);
close.setOnClickListener(this);
action.setOnClickListener(this);
return view;
}
I am trying to load a fragment from the SideMenu. There is a listview in the side menu from where I want to load a Fragment in the application. There are 3 items in the list view. A code for clicking on 0th and 3rd position is working fine, but not working for 1st position ie i==1.
what i have to do for this !
package comm.design.amer.sidemenu_new;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;<code>
public class MainActivity extends AppCompatActivity {
private ArrayList<listView> Listview = new ArrayList<listView>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slideview);
//Create the ListView Items
Listview.add(new listView("page1", "enter", R.drawable.a));
Listview.add(new listView("page2", "enter", R.drawable.b));
Listview.add(new listView("page3", "enter", R.drawable.c));
Listview.add(new listView("page4", "enter", R.drawable.d));
//Call the Adapter
mycomstumerAdapter adapter = new mycomstumerAdapter(this, Listview);
ListView listview = (ListView) findViewById(R.id.list);
listview.setAdapter(adapter);
//make selection
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
selectItem(i);
}
});
}
public void selectItem (int i){
if (i==0){
Toast.makeText(getApplicationContext(),"bn",Toast.LENGTH_LONG).show();}
else if (i==1) {
Fragment1 fragment0 = new Fragment1();
FragmentManager fragmentm = getFragmentManager();
fragmentm.beginTransaction()
.add(R.id.linear,fragment0)
.commit();
}
else if (i==2)
finish();
}
public class mycomstumerAdapter extends BaseAdapter {
Context context;
ArrayList<listView> Listview;
public mycomstumerAdapter(Context context, ArrayList<listView> Listview) {
this.context = context;
this.Listview = Listview;
}
#Override
public int getCount() {
return Listview.size();
}
#Override
public Object getItem(int i) {
return Listview.get(i);
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1;
if (view == null) {
LayoutInflater inflater = getLayoutInflater();
view1 = inflater.inflate(R.layout.listview, null);
} else
view1 = view;
TextView title = (TextView) view1.findViewById(R.id.textView);
TextView detail = (TextView) view1.findViewById(R.id.textView2);
ImageView imageView = (ImageView) view1.findViewById(R.id.imageView);
title.setText(Listview.get(i).Title);
detail.setText(Listview.get(i).Detail);
imageView.setImageResource(Listview.get(i).imageView);
return view1;
}
}
}
In your code I have seen you are using add fragment instead of that try with replacing the fragment. See the below code:-
Fragment1 fragment0 = new Fragment1();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.linear,fragment0);
fragmentTransaction.commit();
OneFragment.java
package com.pixalstudio.musicadda;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class OneFragment extends android.support.v4.app.Fragment {
public OneFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
ArrayList<String> songs = new ArrayList<String>();
List<String> Songs = Arrays.asList(getResources().getStringArray(R.array.songs));
// list.add("item1");
// list.add("item2");
MyCustomAdapter adapter = new MyCustomAdapter(songs, getActivity());
ListView listView = (ListView) view.findViewById(R.id.listView_fragone);
listView.setAdapter(adapter);
return view;
}
/*#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
MyCustomAdapter adapter = new MyCustomAdapter(list, this);
ListView listView = (ListView) getView().findViewById(R.id.listView_fragone);
listView.setAdapter(adapter);
return view;
}*/
}
I am getting error on this file.
Error:(43, 13) error: <identifier> expected
Error:(43, 14) error: illegal start of type
Error:(44, 13) error: <identifier> expected
Error:(44, 14) error: illegal start of type
Error:(49, 24) error: <identifier> expected
Error:(49, 32) error: <identifier> expected
[This is FragmentOne.java where I am getting errors.]
MyCustomAdapter.java
package com.pixalstudio.musicadda;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by akkie on 3/7/2016.
*/
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.cust_layout, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
Button addBtn = (Button)view.findViewById(R.id.add_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
addBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
notifyDataSetChanged();
}
});
return view;
}
}
Added MyCustomAdapter.java please check it and let me know if there changes need to be made.
Try to place your code inside onCreateView method.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
MyCustomAdapter adapter = new MyCustomAdapter(list, this);
ListView listView = (ListView) getView().findViewById(R.id.listView_fragone);
listView.setAdapter(adapter);
...
}
you are passing this to MyCustomerAdapter which is current fragment not the Activity context, please change this to getActivity()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
ArrayList<String> Songs = Arrays.asList(getResources().getStringArray(R.array.songs));
// list.add("item1");
// list.add("item2");
MyCustomAdapter adapter = new MyCustomAdapter(Songs , getActivity());
ListView listView = (ListView) view.findViewById(R.id.listView_fragone);
listView.setAdapter(adapter);
return view;
}
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;
}
}
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;
}
}