modify elements of inflated view used as ListView header. - android

I have a list view with a custom adapter that I am attaching and It needs to have a header with some modifiable information in it. I have created an xml layout for the header, and when its applied everything displayes correctly. But when I try to reference the elements in the header view and set there contents, I receive a Resource not found exception. The exception is specifically thrown at "name.setText(task.getName());". I presume it would be thrown at each of the 3 elements I reference If it go that far.
What is the correct way to do this? Are the resources im referencing not in scope? How can I modify them correctly?
The onCreate of my activity containing the list looks like what is below:
super.onCreate(savedInstanceState);
setContentView(R.layout.taskview);
Object sTask = getIntent().getSerializableExtra("task");
TaskNode task = (TaskNode)sTask;
View header = getLayoutInflater().inflate(R.layout.header, null);
TextView name = (TextView) header.findViewById(R.id.hname);
TextView description = (TextView) header.findViewById(R.id.hdescription);
TextView completion = (TextView) header.findViewById(R.id.hcompletion);
name.setText(task.getName());
description.setText(task.getDescription());
completion.setText(task.completion());
taskList = (ListView) findViewById(R.id.taskList);
taskList.addHeaderView(header);
TaskViewListItem adapter = new TaskViewListItem(this, getApplicationContext(), task);
taskList.setAdapter(adapter);

From the exception you get one from getName(), getDescription() and completation() is returning a numeric value. When you call setText with an int value android will lookup inside R in order to find the String with the id you provide as parameter to setText. If a String with that id does not exist the android.content.res.Resources$NotFoundException exception will be thrown. You can get the String representation of a number with:
String value = String.valueOf(numericValue);

Related

how can i decode HTML code to Android by kotlin?

I'm trying to decode HTML entities, currently my code is:
val str = name
val textView = findViewById<View>(R.id.text) as TextView
textView.text = Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT)
and here the error message is:
kotlin.TypeCastException: null cannot be cast to non-null type android.widget.TextView
Your error has nothing to do with the HTML handling, you're getting the exception on this line, because findViewById is returning null, and then the cast to TextView is failing:
val textView = findViewById<View>(R.id.text) as TextView
Why this happens exactly is hard to tell without context, but the issue is that the View with the ID text was not found.
If you're in an Activity, make sure you're doing this after you call setContentView.
If you're in a Fragment, make sure you're doing this after the onCreateView method has run.
In either case, make sure you're actually using a layout that contains a TextView with the ID #+id/text.
Additionally, you're looking up the TextView as a View first and then casting it, you could do either of these instead:
val textView = findViewById<TextView>(R.id.text)
val textView: TextView = findViewById(R.id.text)

Resource ID is not returning correct value

I am having difficulty displaying the images in a ListView.
The output that I want should contain the different images for different list items.
However what I am getting is this:
As you can see that the icons that are being displayed are same (booking).
What I could figure out until now is that the method itemImagesViews.setImageResource(itemsImages.getResourceId(position, R.drawable.booking)) from the ItemAdapter.java file is not returning the expected value.
Here is the repo:
https://github.com/amar0891/Aroma-Holiday
I don't know the code in ItemAdapter.java. But basically all you need is do this, if you want to see booking icon:
itemImagesViews.setImageResource(R.drawable.booking);
Note: I don't know what code setup you have with getResourceId() and maybe that is the main problem.
Another good solution, recommended by Google, I think is:
Resources resources = getResources();
itemImagesViews. setImageDrawable(resources.getDrawable(R.drawable.myfirstimage));
Note: itemImagesViews is your ImageView. setImageDrawable method is supposed to be more efficent with large images.
I checked your files and the reason getResourceId is always returning the default value is because the items in your array are not resources, they're just strings.You should add the references to your resources in your array. Try this instead:
<array name="items_images">
<item>#drawable/plane</item>
....
</array>
I saw your code
if (convertView == null){
you are setting the image only when the convert view is null but not
always the convertView is null
}
So what you should do is
View view=convertView
if (view == null){
view= activity.getLayoutInflater().inflate(R.layout.item_adapter_view, null);
}
TextView itemDetailsViews = (TextView) view .findViewById(R.id.item_details);
itemDetailsViews.setText(itemDetails[position]);
TextView itemsDescriptionsViews = (TextView) view .findViewById(R.id.item_description);
itemsDescriptionsViews.setText(itemDescriptions[position]);
ImageView itemImagesViews = (ImageView) view.findViewById(R.id.item_image);
itemImagesViews.setImageResource(itemsImages.getResourceId(position, R.drawable.booking));
Log.d(this.getClass().getName().toString(), "" + itemsImages.getResourceId(position, R.drawable.booking));
Log.d(this.getClass().getName().toString(), "" + R.drawable.booking);
you have to assign your set your imageviews and textviews after the if not inside the if

What does RHS of this code statement do?

ListView listView = (ListView) findViewById(R.id.listview);
It would be understandable for me if it was something like
ListView listView = new ListView()
But I don't understand what the RHS of ListView listView = (ListView) findViewById(R.id.listview) means; I know that LHS creates a reference variable named listView which will contain the reference to an object of ListView.
To the best of my understanding, is it retrieving a view by findViewById() and parsing to a ListView object (how can an object of one type be even parsed into an object of another type) , and then assigning a reference to that ListView object in listView reference variable?
Thank you in advance.
R.id.listview
here in one of your xml layouts you name a list as "listview"
android assigns id to every name you allot. id are stored in R java file
it would be like
public static final int listview=0x7f050002;
even you could directly use this int value in place of R.id.listview
findViewById(R.id.listview);
this will tell your activity to find a view (whose id is stored as R.id.listview)
(listview)
you are casting your view as a LISTVIEW object
and assigning it to the
listView
object of class ListView

Android: give textfield 'id' in java

I was wondering how to give a TextField an 'id' in java. So I am pretty much wanting to know how to give the TextField android:id="#id/NAMEHERE" but do it in java. Here is the code so far:
TextView taskname = new TextView(this);
taskname.setText("Task " + tasknumber);
tasklayout.addView(taskname);
What do I need to add to give it an id?
If your question is to how to set id into activity, then understand purpose of id.
Id is specially required when you want to fetch view reference into activity class, where view has been declared into xml file. But in Activity, if you are creating any view, by
TextView taskname = new TextView(this);
here, you already have view reference, but if you still want to set id, then you can use method
taskname.setId(10002);
Try this
static final int TEXT_ID = 80001;
TextView taskname = new TextView(this);
taskname.setId(TEXT_ID);
and later in your code you can refer to this textview by
TextView t = (TextView)findViewById(TEXT_ID);
or
TextView t = taskname;

android: adapter for textview

I have multiple TextViews . When I click on the TextView I would like to grab the associated object. I know there are ListAdapters for list views and other collection type views. Is there a way to dynamically associate a custom object with a TextView?
Yes you can attach. Here are apis from the View since TextView is extended from View you can use them.
setTag(Object tag)
setTag(int key, Object tag)
How to use:
class YourCustomData {
public int data;
}
// in your onCreate
TextView tv = (TextView)findViewById(R.id.your_text_view);
tv.setTag(new YourCustomData());
-
// say somewhere you have a handler/listener for text view
// Note: you have to write the code to get the textview
YourCustomData ycd = (YourCustomData)tv.getTag();
Android Developers Reference:
setTag(java.lang.Object)
getTag()
You can also use a HashMap<View, Object>
http://developer.android.com/reference/java/util/HashMap.html

Categories

Resources