load spinner dynamically into getview - java

i have a class fragmentProducts.java , which i load my products in a listview.
into my xml layout , i have texts and two spinners , one for size and one for colour. everything is ok for the texts , they are shown. I have a problem with the spinners because i think that i should add an arrayAdapter into the GetView.
i get fatal exception: E/AndroidRuntime(29244): android.content.res.Resources$NotFoundException: Resource ID #0x7f0a005b type #0x12 is not valid at android.content.res.loadXMLResourceParser(Resources.java:2407)...
my GetView code with the ViewHelper class ,is this:
`private class ViewHolder {
TextView prName;
TextView prDescription;
TextView prfprice;
ImageView prImage;
//spinners
Spinner PrVariants;
Spinner PrOptions;
int ProdPosition;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
View view = convertView;
final ProductsData info = getItem(position);
if (view == null) {
// Product row
view = mInflater.inflate(R.layout.productslistitem_layout, null);
viewHolder = new ViewHolder();
assert view != null;
viewHolder.prName = (TextView) view.findViewById(R.id.product_name);
viewHolder.prDescription = (TextView) view.findViewById(R.id.product_description);
viewHolder.prfprice = (TextView) view.findViewById(R.id.product_fprice);
viewHolder.prImage = (ImageView) view.findViewById(R.id.product_image);
**//here i try to put test values on the spinner//**
viewHolder.PrVariants = (Spinner) view.findViewById(R.id.spOptions1);
String[] names = {"test1","test2","test3"};
var1Adapter = new ArrayAdapter<String>(getActivity(),R.id.spOptions1,names);
var1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewHolder.PrVariants.setAdapter(var1Adapter);
viewHolder.prfprice.setText("120.00" + "€");
viewHolder.prName.setText(info.getPrName());
viewHolder.prDescription.setText(info.getPrComments());
view.setTag(viewHolder);
} else
viewHolder = (ViewHolder) view.getTag();
return view;
}`
Does anyone understand the error message? should i not use arrayadapter into the GetView function?

Resources$NotFoundException: Resource ID #0x7f0a005b type #0x12
Because ArrayAdapter take Layout id instead of View id as second parameter.
var1Adapter = new ArrayAdapter<String>(getActivity(),
<Layout_Id_In_Which_Spinner_Added>,names);

thank you,unfortunatelly i could not make it work with your answer:
var1Adapter = new ArrayAdapter<String>(getActivity(),R.id.layoutspOptions1,names).
//layoutspOptions1 is the linearLayout id that i have the spinner in.
Although it worked by changing the arrayAdapter to:
`var1Adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,names);
var1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);`
So, we dont use the R.id.spinnerId as a second parameter of the ArrayAdapter but a default layout : android.R.layout.simple_spinner_item

Related

How to literate each list item via ListView/GridView and checked if the checkbox is checked or not - Android Java

Hello guys I want to check through my GridView and see if any of the checkBox is checked or not. Eventually, I want to delete the rows in my database and update my custom adapter but I'm lost on what function to use or how to approach it. I know to use some kind of while/for loop but which data type can surf and iterate each list items? I was looking around and .getChildAt(index) seems like something that would be useful but it doesn't work/appear for my custom adapter variable in my MainActivity.
Here is my Custom Adapter class for reference
//variable responsible for making checkbox visible or not
private boolean displayCheckBox;
//constructor - it takes the context and the list of words
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
}
//sets the visibility of the checkBox
public void setCheckBoxVisibility(boolean visible){
this.displayCheckBox = visible;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
}
//getting the checkBox view id
CheckBox checkBox = (CheckBox) listItemView.findViewById(R.id.check_box);
checkBox.setVisibility(displayCheckBox ? View.VISIBLE : View.GONE);
//Getting the current word
WordFolder currentWord = getItem(position);
//making the 3 text view to match our word_folder.xml
TextView date_created = (TextView) listItemView.findViewById(R.id.date_created);
TextView title = (TextView) listItemView.findViewById(R.id.title);
TextView desc = (TextView) listItemView.findViewById(R.id.desc);
//using the setText to get the text and set it in the textView
date_created.setText(currentWord.getDateCreated());
title.setText(currentWord.getTitle());
desc.setText(currentWord.getTitleDesc());
return listItemView;
}
}

Android; Get text out of dynamic textfield adapter

I have a dynamic adapter that fills up a listview with textviews with values read from a JSON file, here is an example ( https://imgur.com/a/w9CHzxX ). Now I need to gather the user input from those fields and use it in a later part of my application, but my question is. when i generate textviews like that they all have the same ID right? so how can i specificly gather the user input of lets say the 2nd textview?
Adapter
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_question_textfield, null, true);
---> holder.editText = (AutoCompleteTextView) convertView.findViewById(R.id.edt);
---> holder.editTitel = (Button) convertView.findViewById(R.id.btn);
convertView.setTag(holder);
}
else {...}
holder.editText.setHint(HintArrayList.get(position).getEditTextValue());
ID is an attribute of a view that scope of this attribute is limited by the inflated view group that includes this view.
for example if we have this xml file (item_view.xml):
<LinearLayout
...>
<TextView
...
android:id="#+id/textView_itemVew"/>
</LinearLayout>
and then we inflate this file several times (maybe in an adapter class) as below:
View item1 = inflater.inflate(R.layout.item_view, null,true);
View item2 = inflater.inflate(R.layout.item_view, null,true);
View item3 = inflater.inflate(R.layout.item_view, null,true);
now we can access the TextView view by it's ID but we should use the view group of this TextView to find it.
//Id's are the same but view groups no!
TextView editText_of_item1 = item1.findViewById(R.id.editText_itemVew);.
TextView editText_of_item2 = item2.findViewById(R.id.editText_itemVew);
TextView editText_of_item3 = item3.findViewById(R.id.editText_itemVew);
.
.
.
in your case you can add each EditText to a list to access it after whole list inflated ... like this:
List<EditText> editTextList = new ArrayList<>();
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View itemView = inflater.inflate(R.layout.item_view, null,true);
EditText itemEditText = (EditText) itemView.findViewById(R.id.editText);
editTextList.add(itemEditText);
return itemView;
};
there is useful example in javaTpoint website for you.

The get view method crashes before generating the objects

The get view method from the script below crashes everytime I ran the app (the error points to that method ) this is the error :
07-11 17:25:01.147 8512-8512/com.example.android.quakereport E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.android.quakereport, PID: 8512
android.content.res.Resources$NotFoundException: Resource ID #0x0
and this is the ArrayAdapter from the getview method code :
public class EarthQuakeAdapter extends ArrayAdapter<Earthquake> {
public EarthQuakeAdapter(Activity context, ArrayList<Earthquake> Earthquakes) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, Earthquakes);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
return super.getView(position, convertView, parent);
}
Earthquake currentEarthquake = getItem(position);
TextView magnitude = (TextView) listItemView.findViewById(R.id.magnitude);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
magnitude.setText(Earthquake.getmMagnitude());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView place = (TextView) listItemView.findViewById(R.id.Place); // Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
place.setText(Earthquake.getMplace());
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
TextView date = (TextView) listItemView.findViewById(R.id.Date);
date.setText(Earthquake.getmDate());
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
I can add any code or ressource if needed just tell me on the comments.
You're never really returning the ViewHolder you inflated.
You shouldn't call super when the view is null. You should inflate it, get the child views, populate them and finally return the inflated root view.
Take a look at the docs to learn more about this process.
Try to write the adapter like this:
public class EarthQuakeAdapter extends ArrayAdapter<Earthquake> {
// ...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
Earthquake currentEarthquake = getItem(position);
TextView magnitude = (TextView) listItemView.findViewById(R.id.magnitude);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
magnitude.setText(Earthquake.getmMagnitude());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView place = (TextView) listItemView.findViewById(R.id.Place); // Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
place.setText(Earthquake.getMplace());
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
TextView date = (TextView) listItemView.findViewById(R.id.Date);
date.setText(Earthquake.getmDate());
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
You can read more about smooth scrolling here on android's training page.
And as an option, you could switch to RecyclerView, you can read more about it here.
You shouldn't need to call super.getView(...) in the overridden getView function, and you're passing in an invalid resource ID. Simply remove that line, since you're not using its return value anyway:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
...
}

How can I individually validate the selected option in multiple RadioGroups within a ListView?

I'm very new to Android dev and programming in general. My goal is to create a quiz in this format.
Each row is a custom view that includes a RadioGroup with 3 RadioButtons. And that is working fine, as you can see in the picture.
This is how the List is being populated:
private void populateListView() {
ArrayAdapter<QuizQuestion> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.questionsListView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<QuizQuestion> {
public MyListAdapter() {
super(ShortQuiz.this, R.layout.item_view, myQuestions);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
QuizQuestion currentQuestion = myQuestions.get(position);
ImageView imageView = (ImageView) itemView.findViewById(R.id.item_icon);
imageView.setImageResource(currentQuestion.getImageId());
//Question
TextView question = (TextView) itemView.findViewById(R.id.item_question);
question.setText(currentQuestion.getQuestion());
//Game
TextView game = (TextView) itemView.findViewById(R.id.item_game);
game.setText(currentQuestion.getGameName());
//Option A
TextView optionA = (TextView) itemView.findViewById(R.id.item_radioA);
optionA.setText(currentQuestion.getOptionA());
//Option B
TextView optionB = (TextView) itemView.findViewById(R.id.item_radioB);
optionB.setText(currentQuestion.getOptionB());
//Option C
TextView optionC = (TextView) itemView.findViewById(R.id.item_radioC);
optionC.setText(currentQuestion.getOptionC());
return itemView;
}
}
The problem is that I do not know how to 'access' (set/get ids) each RadioGroup after creation in order to validate for right or wrong answers, which I want to do in one go pressing a button, as in: If the selected option is correct then +1 to Correct Answers.
Thanks.
Use a ViewHolder for each row. https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html
You can reference the items in each individual row. You can search for examples for how the ViewHolder is used in practice, but in general each row in the holder will be stored with a setTag() and retreived with a getTag(). So you can obtain the items in a view like:
view.getTag();

Update Custom ListView Item

I have a listview that uses a customadapter based on the baseadapter. The listview populates ok using external data and I can pick up the click events and know which item has been selected.
I'm having a problem updating the clicked item's views, like TextView and ViewFlipper.
Do I have to update something via the listview or is it via the adapter. I've tried things like the following;
View test = (View)adapter.getView(pos, null, myListView);
ViewFlipper temp = (ViewFlipper)test.findViewById(R.id.flipholder);
temp.showNext();
TextView temp2 = (TextView)test.findViewById(R.id.filmtitle);
temp2.setText("Hello World");
Which results in the viewfliper flipping the first and third item or second and fourth and the text not updating at all.
Any ideas?
Cheers
Try this pattern in getView:
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
}

Categories