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;
}
}
Related
I am absolute beginner for this, I'm sorry if this such a dumb mistakes.
so, I created fragment in which i have got recyclerView, but my recycler view is empty, i saw that my context is empty, and i think, that it can be the reason of this, i try to use getActivity
here is code of my fragment
package com.example.pocketcinema;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
public class SearchFragment extends Fragment implements RecyclerViewInterface {
private RecyclerView recyclerView;
RecyclerViewInterface rci;
MyAdapter myAdapter;
public List<String> nameOfFilm, youTubeUrl, photoUrl, descriptionToFilm, category;
FirebaseFirestore db = FirebaseFirestore.getInstance();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
recyclerView = inflater.inflate(R.layout.fragment_search, container,
false).findViewById(R.id.filmsList);
rci = this;
nameOfFilm = new ArrayList<>();
youTubeUrl = new ArrayList<>();
photoUrl = new ArrayList<>();
descriptionToFilm = new ArrayList<>();
category = new ArrayList<>();
loadFilmsFromDB();
return inflater.inflate(R.layout.fragment_search, container, false);
}
private void loadFilmsFromDB() {
Context context = requireContext();
db.collection("films").get().addOnCompleteListener(new
OnCompleteListener<QuerySnapshot>()
{
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
for (QueryDocumentSnapshot document : task.getResult()) {
nameOfFilm.add(document.get("nameOfFilm").toString());
descriptionToFilm.add(document.get("descriptionToFilm").toString());
photoUrl.add(document.get("imageUrl").toString());
youTubeUrl.add(document.get("videoURL").toString());
category.add(document.get("category").toString());
myAdapter = new MyAdapter(context, nameOfFilm, descriptionToFilm,
photoUrl, youTubeUrl,
rci, category);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
}
}
});
}
here is screenshot with debuger, you can see that context = null
context = null
here you can see that i set layout for recyclerView
with logcat
here is Adapter
package com.example.pocketcinema;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class MyAdapter extends
RecyclerView.Adapter<MyAdapter.MyViewHolder> {
Context ct;
List<String> name, description, image, video, category;
private final RecyclerViewInterface recyclerViewInterface;
public MyAdapter(Context ct, List<String> nameOfFilm, List<String>
descriptionOfFilm, List<String> imageUrl, List<String> videoUrl,
RecyclerViewInterface rci,List<String> category) {
name = nameOfFilm;
description = descriptionOfFilm;
image = imageUrl;
video = videoUrl;
this.ct = ct;
recyclerViewInterface = rci;
this.category = category;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int
viewType) {
LayoutInflater inflater = LayoutInflater.from(ct);
View view = inflater.inflate(R.layout.my_row, parent, false);
return new MyViewHolder(view, recyclerViewInterface);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int
position) {
holder.nameFilm.setText(name.get(position));
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ct, PlayerActivity.class);
}
});
Picasso.get().load(image.get(position)).into(holder.image2);
}
#Override
public int getItemCount() {
return image.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView nameFilm;
ImageView image2;
CardView cardView;
public MyViewHolder(#NonNull View itemView, RecyclerViewInterface
recyclerViewInterface) {
super(itemView);
nameFilm = itemView.findViewById(R.id.nameTV);
image2 = itemView.findViewById(R.id.imageView2);
cardView = itemView.findViewById(R.id.cardView);
itemView.findViewById(R.id.sectionOFRecyclerView).setOnClickListener
(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (recyclerViewInterface != null) {
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION) {
recyclerViewInterface.onItemClick(pos);
}
}
}
});
}
}
}
myAdapter isn't null
You are trying to create an instance of activity as global variable. That means the context has not been generated yet.
Basically you can just use requireContext() method of Fragment class. This will provide you the context.
As I have mentioned in comment, you need to update your onCreateView method as below:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_search, container, false);
}
Then, you need to initialize your view and other stuff on onViewCreated method. You can update that method like this:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.filmsList);
textView = view.findViewById(R.id.test);
rci = this;
nameOfFilm = new ArrayList<>();
youTubeUrl = new ArrayList<>();
photoUrl = new ArrayList<>();
descriptionToFilm = new ArrayList<>();
category = new ArrayList<>();
loadFilmsFromDB();
}
Notes
You don't have to send many string parameters to your adapter. It is better to create a Model class for that item, and send the list of that item. So, model classes will have all the data you need, and you can use them from that model. This will give you better coding style and will let you manage your data easier.
To communicate with your XML layout, you can use ViewBinding or DataBinding for that. This will also help you to use your view elements without creating variables for each of them.
I have a recyclerView that must show a list of cards, but it doesn't!
There is an arrayList of recipes get passed to the adapter to display them as cardviews, everything in the debugging seems to be alright, but it doesnt display anything on the screen.
ViewHolder:
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.nd.ameer.bake.R;
public class RecipeViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public ImageView coverImageView;
public RecipeViewHolder(View v) {
super(v);
titleTextView = (TextView) v.findViewById(R.id.titleTextView);
coverImageView = (ImageView) v.findViewById(R.id.recipeImageView);
}
}
Adapter:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import com.example.nd.ameer.bake.R;
import com.example.nd.ameer.bake.models.Recipe;
import com.example.nd.ameer.bake.views.holders.RecipeViewHolder;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class RecipeAdapter extends
RecyclerView.Adapter<RecipeViewHolder> {
ArrayList<Recipe> recipes = new ArrayList<>();
Context context;
public RecipeAdapter(ArrayList<Recipe> recipes, Context context) {
this.recipes = recipes;
this.context = context;
}
#Override
public RecipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recipe_item, parent, false);
return new RecipeViewHolder(view);
}
#Override
public void onBindViewHolder(RecipeViewHolder holder, int position) {
holder.coverImageView.setImageResource(R.color.colorPrimaryLight);
holder.titleTextView.setText(recipes.get(position).getName());
}
#Override
public int getItemCount() {
return recipes.size();
}
}
Fragment onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_recipe, container, false);
createRecipes();
recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_recipe);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
if (recipes.size() > 0 & recyclerView != null) {
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new RecipeAdapter(recipes, getContext()));
}
return rootView;
}
The method createRecipes(); creates a list of recipes and then it get passed to the adapter.
as you can see, the recipes size is 4, so it's not the problem
I didn't know the cause of the problem till now, but I moved both the adapter and the view holder to the Recipes Fragment as inner classes, and this has fixed the problem.
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;
}
Please help me resolve this issue. I'm making a drawer for my app and I'm receiving these errors from ADT:
The constructor Object(Context, int, List) is undefined
The method getView(int, View, ViewGroup) of type CustomDrawerAdapter
must override or implement a supertype method
This is my code for CustomDrawerAdapter class:
package com.example.ico;
import java.util.List;
import android.app.Activity;
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 CustomDrawerAdapter {
Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
public CustomDrawerAdapter(Context context, int layoutResourceID, List<DrawerItem> listItems) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
DrawerItemHolder drawerHolder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view
.findViewById(R.id.drawer_itemName);
drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
dItem.getImgResID()));
drawerHolder.ItemName.setText(dItem.getItemName());
return view;
}
private static class DrawerItemHolder {
TextView ItemName;
ImageView icon;
}
}
To fix all your problems, you need to make your class extend ArrayAdapter.
public class CustomDrawerAdapter extends ArrayAdapter {
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...