How to implement custom adapter to a nested fragment - java

i am trying to make a listview in a nested fragment which will hold as elements images with captions beneath it, though i keep getting nullpointer errors inside my custom adapter i created to fill the listview. here is the code:
The custom adapter:
import android.app.Activity;
import android.view.View;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ImageAdapter extends ArrayAdapter<Image> {
ArrayList<Image> imageList;
private Context mContext;
int resource;
public ImageAdapter(Context context, int resource, ArrayList<Image> imageObjects) {
super(context, resource, imageObjects);
this.imageList = imageObjects;
this.mContext = context;
this.resource = resource;
}
public ArrayList<Image> getImageList() {
return imageList;
}
public void setImageList(ArrayList<Image> imageList) {
this.imageList = imageList;
}
public Context getmContext() {
return mContext;
}
public void setmContext(Context mContext) {
this.mContext = mContext;
}
public int getResource() {
return resource;
}
public void setResource(int resource) {
this.resource = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView==null){
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.fragment_membership, null, true);
}
Image image = getItem(position);
ImageView layoutImage = (ImageView)convertView.findViewById(R.id.membershipimage);
layoutImage.setImageResource(image.getImageResourceID());
TextView layoutText = (TextView)convertView.findViewById(R.id.membershipCaption);
layoutText.setText(image.getImageCaption());
return super.getView(position, convertView, parent);
}
}
The fragment i use, within another fragment:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import java.util.ArrayList;
public class MembershipFragment extends Fragment {
ArrayList<Image> imageList;
ListView membershipListView;
public static MembershipFragment newInstance() {
MembershipFragment fragment = new MembershipFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_membership, container, false);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
imageList = new ArrayList<>();
for(int i=0; i<3; i++){
Image image = new Image(R.drawable.cinnamon_apple_breakfast, "Test Caption");
imageList.add(image);
}
membershipListView = (ListView)view.findViewById(R.id.membershipListView);
try{
ImageAdapter adapter = new ImageAdapter(getContext(), R.layout.list_view_item, imageList);
membershipListView.setAdapter(adapter);
}
catch (Exception e){
e.printStackTrace();
}
}
}
The app keeps crashing at startup, throwing nullpointer exceptions in the custom adapter in the getView method, when i try to set the image resource id to the imageview. Where is the problem?

I think you're confusing your layouts.
And you should use getActivity here.
ImageAdapter adapter = new ImageAdapter(getContext(), R.layout.list_view_item
You passed that layout resource there. But inside the adapter, you're using a different layout.
#Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView==null){
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.fragment_membership, null, true);
}
You made a getResource method, for a reason, I assume? You never call it.
I don't think you want your Fragment layout to hold a list of Fragment layouts (because recursion)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_membership, container, false);
return view;
}

Related

How to display selected Item in spinner with custom adapter?

I am trying to implement a spinner in my android app with a custom adapter. I am able to populate the spinner with data, but I am not able to select the data and have it display on the spinner. I also want to display a text like 'Select' when the user hasn't clicked on it.
Here is my custom adapter-
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class TransactionModeSpinnerAdapter extends ArrayAdapter<TransactionModeDetails> {
LayoutInflater layoutInflater;
public TransactionModeSpinnerAdapter(#NonNull Context context, int resource, #NonNull List<TransactionModeDetails> transactionModeDetails) {
super(context, resource, transactionModeDetails);
layoutInflater = LayoutInflater.from(context);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View rowView = layoutInflater.inflate(R.layout.activity_spinner_transaction, null,true);
TransactionModeDetails transactionMode = getItem(position);
TextView txnMode_spn = rowView.findViewById(R.id.spntransaction_tv_txnName);
txnMode_spn.setText(transactionMode.getTransactionModeName());
return rowView;
}
#Override
public View getDropDownView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.activity_spinner_transaction, parent,false);
}
TransactionModeDetails transactionMode = getItem(position);
TextView txnMode_spn = convertView.findViewById(R.id.spntransaction_tv_txnName);
txnMode_spn.setText(transactionMode.getTransactionModeName());
return convertView;
}
}
Below is where I am using it-
public void populateActiveTransactionModes(){
List<TransactionModeDetails> getTransactionModes = dbHandler.getActiveTransactionModes();
transactionModeListAdapter = new TransactionModeSpinnerAdapter(this, R.layout.activity_spinner_transaction, getTransactionModes);
selectTransactionMode.setAdapter(transactionModeListAdapter);
}

how to fix LayoutInflater error in java android?

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import com.recipi.myapprishi.Model.RecipieModel;
import com.recipi.myapprishi.R;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class Recipe_Adapter extends RecyclerView.Adapter<Recipe_Adapter.viewHolder> {
ArrayList<RecipieModel> list;
Context context;
public Recipe_Adapter(ArrayList<RecipieModel> list, Context context) {
this.list = list;
this.context = context;
}
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//this line have error indicated by logcat
View view = LayoutInflater.from(context).inflate(R.layout.sample_recyclerview , null);
//this line have error indicated by logcat
return new viewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull viewHolder holder, int position) {
RecipieModel model = list.get(position);
holder.imageView.setImageResource(model.getPic());
holder.textView.setText(model.getText());
}
#Override
public int getItemCount() {
return list.size();
}
public class viewHolder extends RecyclerView.ViewHolder {
public ImageSwitcher imageView;
ImageView imageview;
TextView textView;
public viewHolder(#NonNull View itemView) {
super(itemView);
imageview = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
}
}
}
what's wrong in this code it giving me run time error like unfortunately app has been stopped and logcat is indicating me on bolded line
Error :
at com.recipi.myapprishi.Adapter.Recipe_Adapter.onCreateViewHolder(Recipe_Adapter.java:32)
at com.recipi.myapprishi.Adapter.Recipe_Adapter.onCreateViewHolder(Recipe_Adapter.java:16)
You can inflate your view in this way:
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContet()).inflate(R.layout.sample_recyclerview , parent, false);
return new viewHolder(view);
}
Also, verify if R.layout.sample_recyclerview is the correct parameter.

Add Function for each bottom from Bottomsheet

I have set of different buttons in my list view:
This is my app:
MyBottomSheetDialogFragment:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by sasank on 26-11-2016.
*/
public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment {
String mString;
private RecyclerView mRecyclerView;
private VerticalListAdapter mVerticalListAdapter;
static MyBottomSheetDialogFragment newInstance(String string) {
MyBottomSheetDialogFragment f = new MyBottomSheetDialogFragment();
Bundle args = new Bundle();
args.putString("string", string);
f.setArguments(args);
return f;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mString = getArguments().getString("string");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bottom_sheet_modal_list, container, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<Model> list = new ArrayList<Model>();
list.add(new Model(R.drawable.ic_call_black_24dp, "Call"));
list.add(new Model(R.drawable.ic_chat_black_24dp, "Chat"));
list.add(new Model(R.drawable.ic_android_black_24dp, "Android"));
list.add(new Model(R.drawable.ic_location_on_black_24dp, "Location"));
list.add(new Model(R.drawable.ic_call_black_24dp, "Call"));
list.add(new Model(R.drawable.ic_chat_black_24dp, "Chat"));
list.add(new Model(R.drawable.ic_android_black_24dp, "Android"));
list.add(new Model(R.drawable.ic_location_on_black_24dp, "Location"));
mVerticalListAdapter = new VerticalListAdapter(getContext(), list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mVerticalListAdapter);
}
}
My verticalListAdapter.
package com.example.user.bottomsheet_android;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class VerticalListAdapter extends RecyclerView.Adapter<VerticalListAdapter.MyViewHolder>{
private Context mContext;
private List<Model> mModelList;
public VerticalListAdapter(Context context, List<Model> list) {
this.mContext = context;
this.mModelList = list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.modal_list_item, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.txtView.setText(mModelList.get(position).getName());
holder.imgView.setImageDrawable(mContext.getResources().getDrawable(mModelList.get(position).getImgId()));
}
#Override
public int getItemCount()
{
return mModelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView txtView;
public ImageView imgView;
public MyViewHolder(View view) {
super(view);
txtView = (TextView) view.findViewById(R.id.txtView);
imgView = (ImageView) view.findViewById(R.id.imgView);
}
}
}
My goal is to add function for each button that is showed in the bottom sheet.
For any clarification please leave a comment.
In your onBindViewHolder method, I would put an onClickListener, and then use either a switch-case structure or several different if-elseif statements. For example, the switch-case would look something like:
holder.txtView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
switch (holder.getAdapterPosition()) {
case 1:
// do call related things here
.
.
.
break;
case 2:
// do chat related things here
.
.
.
break;
}
}
}
and so on. You could implement something similar with if statements

List View items text not changing colour

I'm trying to change the text colour of my list items using a custom adapter so that they each have a colour of their own but I get an error that I don't know how to fix. How can this error be resolved?
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
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;
public class FragmentLineChooserList extends android.support.v4.app.Fragment {
ListView list_linechooser;
String[] listContent = {
"Line 1",
"Line 2",
"Line 3"
};
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_line_chooser_list, container, false);
list_linechooser = (ListView)v.findViewById(R.id.list_linechooser);
MyColoringAdapter adapter = new MyColoringAdapter(this,listContent);
list_linechooser.setAdapter(adapter);
return v;
}
private class MyColoringAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MyColoringAdapter(Context context, String[] values) {
super(context, R.layout.list_item, 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.list_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.list_item);
textView.setText(values[position]);
int textColorId = R.color.white;
switch (position) {
case 0:
textColorId = R.color.green; break;
case 1:
textColorId = R.color.orange; break;
case 2:
textColorId = R.color.blue; break;
}
textView.setTextColor(getResources().getColor(textColorId));
return rowView;
}
}
}
The first argument to the constructor for MyColoringAdapter is a Context. A Fragment is not a Context, and thus you cannot pass this into the constructor as you are attempting to do in your Fragment.
You can get the Fragment's Activity (which is a Context) by calling getActivity().

How to show both spinner and textview in android

I'm creating and an android application and i want to show spinner and textview in a list view.i used separate xml classes for the values for spinner and textview.for the following code sample it show only values from textview not values from spinner..how can i show both values from spinner and textview..i;m totally new for android programming..
package com.example.dcsd;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class Result extends ListActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
setListAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.textView1, getResources().getStringArray(R.array.modules)));
setListAdapter(new MyAdapter(this, android.R.layout.simple_spinner_dropdown_item,R.id.spinner1, getResources().getStringArray(R.array.grades)));
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.item_layout, parent,false);
String[] items=getResources().getStringArray(R.array.modules);
TextView tv=(TextView) row.findViewById(R.id.textView1);
tv.setText(items[position]);
Log.d("num", "lu");
return row;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.item_layout, parent,false);
String[] items=getResources().getStringArray(R.array.grades);
Spinner sp = (Spinner) findViewById(R.id.spinner1);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(Result.this, android.R.layout.simple_spinner_item,items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
Log.d("num", "huuuu");
return row;
}
}
}

Categories

Resources