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;
}
}
}
Related
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);
}
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;
}
I am able to change the background of a selected item in a listview. I want the first item's background highlighted when the list is created. The code below gives an error when I try to perform a "performItemClick".
I have seen suggestions to modify my getView in the array adapter. However, I get the error "no default construction in Android.widget.ArrayAdapter when I try to create the class MyCustomAdapter in my java code.
So, if I need to create an extention of my ArrayAdapter class to do this, how do I get around this error. If there is another way I am open to suggestions.
Thanks
package com.hofficer.aclsfast;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.lang.reflect.Array;
public class NarrowChild extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_narrow_child);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//fix orientation
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//MyCustomAdapter adapter = new MyCustomAdapter();
createList();
}
/*
class MyCustomAdapter extends ArrayAdapter{
}*/
void createList(){
String[] narrowChoices = {"aFib/Flutter", "Narrow Cmplx Tachycardia", "PSVT", "Junctional Tachycardia", "Multifocal Tachycardia"};
ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, narrowChoices);
final ListView listview = (ListView) findViewById(R.id.narrowListView);
listview.setAdapter(theAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//clear menu and highlight selected item
for (int i = 0; i < 5; i++) {
listview.getChildAt(i).setBackgroundColor(Color.WHITE);
}
listview.getChildAt(position).setBackgroundColor(Color.CYAN);
}
});
Boolean result = listview.performItemClick(listview,0,0); //THIS GIVES ERROR NULL POINT EXCEPTION
}
}
in your MyCustomAdapter you have to override the one or more of the constructors of ArrayAdapter or create your own. and example of custom Adapter is :
public class WeatherAdapter extends ArrayAdapter<Weather>{
Context context;
int layoutResourceId;
Weather data[] = null;
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
more info from the source
in your case you need to check the position if it is 0 and then return the desired item.
I can't test it right now but can't you just call listview.getChildAt(0).setBackgroundColor(Color.CYAN); instead of Boolean result = listview.performItemClick(listview,0,0);?
Try this:
Boolean result = listview.performItemClick(listview.getAdapter().getView(0),0,0);
or:
Boolean result = listview.performItemClick(listview.getAdapter().getView(0, null, null),0,0);
See this question and check the docs.
EDIT
I think your problem is the getChildAt() method. You should look at this question.
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().
Im using an array adapter with a list view as follows:
ArrayList<Store> allStores = dm.getAll(Store.class);
ArrayAdapter<Store> adapter = new ArrayAdapter<Store>(context,R.layout.listview_layout, R.id.textView1, allStores);
ListView mylist = (ListView) view.findViewById(R.id.listViewFromDb);
mylist.setAdapter(adapter);
The array that I pass into the listview is an array of objects. At the minute the list prints out all of the attributes of the object is it possible to just print the first attribute of the object so "name". Please help
Extend the ArrayAdapter and override the getView method, using the position argument to access the element in the array. Here is an example from Vogella:
The following example doesn't recycle views, so keep that in mind as it will affect performance (a great deal on some devices).
package de.vogella.android.listactivity;
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 MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, 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.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Change the icon for Windows and iPhone
String s = values[position];
if (s.startsWith("iPhone")) {
imageView.setImageResource(R.drawable.no);
} else {
imageView.setImageResource(R.drawable.ok);
}
return rowView;
}
}