How to display selected Item in spinner with custom adapter? - java

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

Related

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.

Android GridView Place Half items on Left and Half on Right

Suppose we have 6 items, is there any possibility or way to place 3 items on left side of the row and 3 items on right side of the row in grid view and eventually we have a space in between them.
Actually I tried to give dynamic horizontal spacing after from the adapter but it didn't help me out. The picture is attached
package com.nxd.cap.Adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.nxd.cap.R;
import java.util.ArrayList;
public class GalleryAdapter extends BaseAdapter {
Context context;
int[] images = new int[]{R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4,
R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8,
R.drawable.image1, R.drawable.image2};
public GalleryAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return images.length;
}
#Override
public Object getItem(int i) {
return images[i];
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null){
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.gallery_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView)view.findViewById(R.id.imageView);
view.setTag(viewHolder);
}
ViewHolder viewHolder = (ViewHolder) view.getTag();
// Picasso.with(context).load(images[i]).into(viewHolder.image);
GridView gridView = (GridView)viewGroup;
if (i == images.length/2) {
gridView.setHorizontalSpacing(100);
}
viewHolder.imageView.setImageResource(images[i]);
return view;
}
static class ViewHolder {
ImageView imageView;
}
}
when you used gridView you can set number of column.if need only two column left and right set below code.
gridView.setNumColumns(2);

How to implement custom adapter to a nested fragment

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

Android: dont change when I click in dropdownlist in spinner

When I am trying to use the drop down list of a spinner, the option doesn't select, I was investigating in Google and I think I am configuring the spinner correctly for cities but maybe I have forgotten something in the adapter. I am using a CustomAdapter.
My fragment is:
In the Fragment call I have this code:
ArrayList<ItemPair> data1 = IAndroid.getAllCities();
Spinner spCity = (Spinner) rootView.findViewById(R.id.spPoblacion);
spCity.setAdapter(new ItemPairListAdapter(inflater.getContext(), data1));
At the moment I don't have the select click item associated, I only set all items in dropdownlist.
My fragment.xml is:
<Spinner
android:id="#+id/spProvincia"
android:layout_width="wrap_content"
android:clickable="true"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1" />
My Custom Adapter is:
package info.android.adapter;
import info.android.MainActivity;
import info.android.R;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
public class ItemPairListAdapter extends BaseAdapter implements SpinnerAdapter {
private Context context;
private ArrayList<info.android.model.ItemPair> navProOffers;
public ItemPairListAdapter(Context context, ArrayList<info.android.model.ItemPair> navProOffers) {
this.context = context;
this.navProOffers = navProOffers;
}
#Override
public int getCount() {
return navProOffers.size();
}
#Override
public info.android.model.ItemPair getItem(int position) {
return navProOffers.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.fragment_row_itempair, null);
}
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
TextView txtKey = (TextView) convertView.findViewById(R.id.txtKey);
TextView txtValue = (TextView) convertView.findViewById(R.id.txtValue);
info.android.model.ItemPair ip = getItem(position);
if (ip != null)
{
if (ip.Key != null) txtKey.setText(ip.Key);
if (ip.Value != null) txtValue.setText(ip.Value);
}
convertView.setTag(ip.Key);
return convertView;
}
}
My ItemPair class is:
package info.android.model;
public class ItemPair {
public String Key;
public String Value;
}
What is wrong? What do I need to simulate the click behaviour and afterwards receive my item?
I removed click listener in the getView, answered it for G.T.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.fragment_row_itempair, null);
}
TextView txtKey = (TextView) convertView.findViewById(R.id.txtKey);
TextView txtValue = (TextView) convertView.findViewById(R.id.txtValue);
info.android.model.ItemPair ip = getItem(position);
if (ip != null)
{
if (ip.Key != null) txtKey.setText(ip.Key);
if (ip.Value != null) txtValue.setText(ip.Value);
}
convertView.setTag(ip.Key);
return convertView;
}

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