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
Related
How can i set my own option on view?
I need something like this:
TableRow tblr_data = new TableRow(this);
tblr_data.setOption("my_option", "my_option_value"); //there is no such method
Another words i need to add custom option to table row (custom id for example) and then use it in onclick handler.
If I understand your question correctly, you could use the setTag(int key, Object tag) method.
//member variable
private int MY_OPTION = 1;
//when creating your tablerow
tblr_data.setTag(MY_OPTION, "my_option_value");
then in your onClickListener, you can just fetch that value again by calling
String value = (String)tblr_data.getTag(MY_OPTION);
See this accepted answer for more on the get/setTag() method.
What is the main purpose of setTag() getTag() methods of View?
I would like to improve the way i created the following UI. Currently i am creating each tablerow programmatically according to each object's type attribute.
class objectDTO {
private type; //enum
public boolean isMultiple(){...}
public boolean isSingle(){...}
}
I am trying to find a more dynamic solution like having a class for each type that might not requires programmatically adding layouts and components as i do in the fragment class,
if(objectDTO.isMultiple()) {
//Create TableRow + Multiple radiobuttons
}
else if(objectDTO.isSingle() {
//Create TableRow + Add One Checkbox
{
else {
//Create default invalid object Interface or skip
}
Havind a listadapter and applying the different ui there will just move the design problem to other class.
Early thanks for your contribution
Well, the simple solution for you would be to have a class hierarchy- a base objectDTO class and a child for each type. When you load the object list, have a factory method create the proper type of object. Each type would override a createView method which would create the view for that type. Then your table creation function becomes:
for(objectDTO object : allObjects){
View view = object.createView();
tableView.addView(view, lp);
}
But if you're creating a view for an object type, there's always going to need to be someone that dynamically creates view objects (createView in this case), and there's always going to need to be some function that knows what class to make an object (the factory in this case). Its just a matter of where you want that complexity to be.
I've got some very tricky problem. I already tried to search the web and even looked into the MvvmCross sources, but I don't seem to be able to figure it out.
I have an MvxListView with a custom Adapter. The reason is, that depending on the "DataContext" of the current ListItem, I want to display some different view.
The list itself represents some sort of questionnaire. So the items in the list are in the form of
new Question("do you need help?"){
new Answer("yes"),
new Answer("no"),
new Answer("maybe")
}
Now the answers shall be shown as a radio button list.
So in my custom adapter on "GetChildView", I retrieve the view with the radiogroup and then I
"just want to bind that group to my answers" --> so for each answer, there has to be a corresponding radiobutton.
I would love to have the "Answer" object as datacontext for each radiobutton.
radioButton.Bind("Checked", "Chosen"); // where "Chosen" is the boolean property on "Answer"
But it would already be fine if the "Question" object could be the datacontext that I bind to
radioGroup.Bind("CheckedRadioButtonId", "ChosenAnswer"); // where "ChosenAnswer" is an int property
on "Question"
So basically I want to bind my radiobutton to the MvxListItem.DataContext in code inside my customadapter.
But I just cannot figure out how to do that. :/
Can you please give me a hint?
Of course I would love to do the same with a list of checkboxes as soon as multiple answers would be allowed.
Setting the datacontext is easy: just set it :)
What you do is you create a ViewModel called something like QuestionViewModel, which has what you need as a separet ViewModel.
Then create some component to use in your View for the complete questionnaire. Below is some example code for a bindable component.
public class BindableLinearLayout : ClickableLinearLayout, IMvxDataConsumer, IMvxBindingContextOwner
{
public BindableLinearLayout(Orientation orientation, object dataContext)
: base(orientation)
{
BindingContext = new MvxBindingContext();
DataContext = dataContext;
}
public object DataContext { get { return BindingContext.DataContext; }
set { BindingContext.DataContext = value; }
}
public IMvxBindingContext BindingContext { get; set; }
}
In the Questionnaire View, create this component and assign the datacontext (in the above example as a parameter). Then you can create the binding in the normal way:
var bindings2 = layout.CreateBindingSet<BindableLinearLayout, ParagraphViewModel>();
bindings2.Bind(numberText.View).For(t => t.Text).To(vm => vm.Paragraph.Number);
bindings2.Apply();
This code is called for each element you add to the collection, eauch with its own Datacontext.
I known this code is not for a list adapter, but I hope this will give you enough hints how to do this yourself.
i am new to Android development but am trying to get one element of a string variable but can't seem to figure it out.
#Override
protected void onListItemClick(ListView walksList, View v, int position, long id)
{
String selected = walksList.getItemAtPosition(position).toString();
}
selected is returning me :
{root=WalkOneRoot, caption=WalkOneCaption, title=WalkOneTitle}
What I am trying to do is isolate 'WalkOneRoot' for each list item into a variable.
Any help would be much appreciated.
if you have the listview set up with textviews you can simple use the textview to get what you want in your onListItemClick.
TextView tv = (TextView)v.findViewById(R.id.root);
String myString = tv.getText().toString();
ListView.getItemAtPosition(int position) is returning an generic object of type Object. Instead of calling toString() on selected you should cast it as the type of object it really is and then extract the WalkOneRoot value from it that way.
What type of object is contained in your list view?
The item that you're retrieving is the View which is why toString() is simply returning whatever overridden toString method you have in your View class. You could, presumably, override toString to only return the variable you're interested in.
I have a list of players whos name are displayed listview. and each row of listview contains button, textview and imageview. How can I get the value of textview?
If you are using Custom Adapter class to populate the listview,then in your adapter,you can use HashMap for saving key-value pair,saving position of listitem with the data into that textview.and then you can easily retrieve it on OnItemClickListener of listview.
Depending on the specifics of your implementation, I would go with one of the following approaches.
Option A.
Use setOnItemClickListener to register a click listener with the list (or if you're using a ListActivity or ListFragment simply use [onListItemClick](http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick%28android.widget.ListView, android.view.View, int, long%29)). onItemClick gets passed in the View that was clicked and can be used to retrieve nested views, e.g. the TextView you're looking for.
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.textview);
String tvText = tv.getText();
}
}
Option B
Assuming you fill your list from some sort of data collection, you may be able to do something similar to above, but use the passed position parameter as an index to directly get the text from the objects in your collection; i.e.:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SomeObject so = myCollection.get(position);
String tvText = so.getTextViewText();
}
}
There are lots of more options though. I kind of like creating my own extension of ArrayAdapter to hold the models for the views of the items in the list. That way you could also call getItemAtPosition(int position) or getItem(int position) and cast the returned object to your data type.
Is it a static list or a dynamically generated one? If its a static one you can assign a different id to each textview in the xml itself, and then use FindViewById to access it. If it's not this is what you've got to do: you will obviously have one row and display it many times. So multiple textviews will have same ID. Use a for loop, inside which use FindViewById(Remember FindViewByID will only access the first element with mentioned Id, set its Id to something else, in the next iteration the next textview is selected, set its Id to something) then use these new ids to access them, thus you can access each textview