i have already search web to find a solution.
i have two layouts in my activity and in first layout i have my recyclearview and in second i have a textview that i want to settext by clicking one of items in recyclear.
so in onBindViewHolder i added the click listener.
but i have no access to the second layout to settext.
i used layoutinflator and created view from that.
and i could get values like gettext() but settext() is not working with no error!!
how can i fix that?
UPDATE:
View v = View.inflate(context, R.layout.etelaie_activity, null);
TextView tt = (TextView) v.findViewById(R.id.etelaie_activity_title);
Toast.makeText(context, tt.getText().toString(), Toast.LENGTH_SHORT).show();
this.title = title;
this.context = context;
tt.setText("ff");
gettext() is working but settext wont apply.
You have to send callback from your Adapter to Activity. View of RecyclerView's item is different from View of Activity.
TextView tt = (TextView) v.findViewById(R.id.etelaie_activity_title);
You inflated View and accessing its default written text(might be a text which you put inside XML file) but where this View is shown on UI? You didn't set this View on UI.
Create an interface like:
public interface MyAdapterCallback {
void updateText(String text);
}
Implement this in your Activity:
public class YourActivity implements MyAdapterCallback {
. . .
#Override
void updateText(String text){
textView.setText(text);
}
. . .
}
Create adapter with:
Constructor(its parameter.. , MyAdapterCallback callback)
Inside onClick in adapter: call
callback.updateText(list.get(position))
Hope it helps!
Related
LayoutInflater factorys = LayoutInflater.from(MainActivity.this);//获取
MainActivity中LayoutInflater (上下文参数)
View view= factorys.inflate(R.layout.bluetooth_list, null);//获取View 对象
TextView test = (TextView) view.findViewById(R.id.text_test);
Toast.makeText(this, test.getText().toString(), Toast.LENGTH_SHORT).show();
test.setBackgroundColor(Color.rgb(0,0,100));
The TextView is in another XML, Toast is work, but setBackgroundColor is not
Why? This is just a test. My final target is set ListView in another XML.
you are only using the text [test.getText().toString()] from the textView not the textview . so what ever the attributes you are applying to textview will not affect your Toast.
you can use interface to get that or simpler you can create a static method for the class that inflates the text view
for example
public static void ChangeTextcolor(){
test.setBackgroundColor(Color.rgb(0,0,100));}
and then call it
thatClass.ChangeTextcolor();
I've got a problem between view.findViewById and activity.findViewById.
Consisely it's when I use view.findViewById the data will not show and have no error report at all. And when I use the activity.findViewById , everything is fine. I dont know why or if I have made any mistake. Please give me some sugguestions.
MainActivity code
public class MainActivity extends AppCompatActivity {
ViewManager viewManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewManager=new ViewManager(this);
viewManager.setText("hello");
}
}
ViewManger Code
public class ViewManager {
private AppCompatActivity activity;
ViewManager(AppCompatActivity activity){
this.activity=activity;
}
public void setText(String text){
//in this way the word"hello" cannot be shown
View view= LayoutInflater.from(activity).inflate(R.layout.activity_main,null);
TextView textView=view.findViewById(R.id.tv);
//this way it works but i dont know why i m wrong above
//TextView textView=activity.findViewById(R.id.tv);
textView.setText("hello");
}
}
You are creating a separate view hierarchy which has no link with your activity layout so nothing will happen on screen
so you can set view reference as activity's layout
public void setText(String text){
View view= LayoutInflater.from(activity).inflate(R.layout.activity_main,null);
activity.setContentView(view);
//^^^^^^^^^^^^^^^^^^^^^^
TextView textView=view.findViewById(R.id.tv);
//this way it works but i dont know why i m wrong above
//TextView textView=activity.findViewById(R.id.tv);
textView.setText("hello");
}
and setContentView(R.layout.activity_main); no longer be needed
You're setting the text on a view in a layout that you do not show. Once you inflated R.layout.activity_main in ViewManger, you never add the view to the one that got inflated by the activity itself (the layout passed as a parameter to setContentView). The reason that it works with activity is that in that case, the activity will look in the view that was passed by setContentView, the layout that is visible.
The problem is Your activity's layout is already inflated by using this line:
setContentView(R.layout.activity_main);
So if you call in activity:
TextView textView=findViewById(R.id.tv);
textView.setText("hello");
It will work cause the parent of this view if the activity's layout.
The thing you are doing in your ViewManager is inflating a new view which is not bind to any component.So where you suppose to show the text View.
This will be the solution:
public void setText(String text){
TextView textView=activity.findViewById(R.id.tv);
textView.setText("hello");
}
As a little eperiment, I'm trying to do the following.
I have an AXML describing a vertical linear layout which contains a listview (only filling 200dp of the vertical linear layout ). The AXML is inflated when the activity starts with SetContentView. Then the listview is correctly populated with values using its Adapter.
In the GetView method of the listview Adapter, I am trying to also dynamically create a button and add it to the linear layout, but for some reason the button is not added.
If I try to add the button in the constructor method of the Adapter instead, it is correctly added.
Can you tell me what could be possibly going wrong?
Let me add some code:
class TracksAdapter : BaseAdapter<string> {
Activity context;
List<Dictionary<string,string>> trackList;
// constructor
public TracksAdapter (Activity context, List<Dictionary<string,string>> trackList) {
this.context = context;
this.trackList = trackList;
// Just as a little test, if I create the button from here it will be correctly added to linear layout:
var ll = context.FindViewById<LinearLayout>(Resource.Id.linLayForResultsActivity);
Button b1 = new Button(context);
b1.Text = "Btn";
ll.AddView(b1);
}
public override View GetView(int position, View oldView, ViewGroup parent) {
// if I create the button from here it will not be added to the layout
var ll = context.FindViewById<LinearLayout>(Resource.Id.linLayForResultsActivity);
Button b1 = new Button(context);
b1.Text = "Btn";
ll.AddView(b1);
// this other code is working
View view = context.LayoutInflater.Inflate(Resource.Layout.ResultItem, null);
var artistLabel = view.FindViewById<TextView>(Resource.Id.resultArtistNameTextView);
artistLabel.Text = trackList[position]["trackArtistName"];
return view;
}
}
Update: adding some more context information because I know this can be a bit weird to understand without it:
In GetView, I don't need to return the new button I am trying to create there. GetView only need to return a listview view item, but, along its execution, GetView also has to create and add a button to the linear layout containing the listview.
The real code is much more complex than that. I have simplified it in the question. In the real code, the listview items are made of text and a button. The GetView also attaches event handlers to the buttons. Then what I need is, when a user clicks a button in any of the listview items, another button is added below the listview. So I need the code for adding another button to be in GetView, and the button needs to be added outside of the listview, ie. to the linear layout containing the listview.
Use the LayoutInflator to create a view based on your layout template, and then inject it into the view where you need it.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
I looked in you code, you are returning view, while you add the button to ll, you should return ll
what you return in getView() is what you see in the list item layout, since you're adding the button to ll and returning view, the button won't appear.
you can add the button to view as you implementation
Also check this:
Try using boolean addViewInLayout (View child, int index, ViewGroup.LayoutParams params)
http://developer.android.com/reference/android/view/ViewGroup.html#addViewInLayout(android.view.View, int, android.view.ViewGroup.LayoutParams)
It's working... Without making any changes now it's working as it should... ! Ugh!
I really don't know what I was doing wrong here... probably it was because of some sort of caching of older version of the installed APK.. ? I know this sort of stuff can happen, and that's why I've always been uninstalling the app before deplyoing the new version to the device... but still...!
I created a class that extends ActionBarActivity and displays a custom XML. That class is extended by almost all my activities.
I want to access an element of that custom XML from one of my activities. Let's say I want to change the background of item2 when I'm in Activity2.
In my activity's onCreate method, after setContentView, I tried:
View cView = getLayoutInflater().inflate(R.layout.custom_menu, null);
ImageButton rewards_link = (ImageButton) cView.findViewById(R.id.rewards_link);
rewards_link.setVisibility(View.GONE); // For test purpose
Even if the button id seems correct, the changes doesn't apply. Any ideas ?
If you are setting the custom view via getActionBar().setCustomView(R.layout.custom_menu); (or getSupportActionBar() for v21 of AppCompat), then you can access those views by using findViewById() directly as the view is part of your view hierarchy just like views added via setContentView():
ImageButton rewards_link = (ImageButton) findViewById(R.id.rewards_link);
rewards_link.setVisibility(View.GONE); // For test purpose
Use #getCustomView
View view =getSupportActionBar().getCustomView(); ImageButton imageButton =
(ImageButton)view.findViewById(R.id.action_bar_back); imageButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v){
// Your code here ...
}
});
I have a ListView whose rows are formatted by me. Each row has a mix of ImageView and TextView.
I have also implemented my own adapter and am able to draw each row through it.
Now, I would want something like this-
User clicks on an ImageView (not anywhere else on the row, but only this ImageView should respond to clicks)
I get to know the position of the row whose ImageView was clicked.
I have tried many things for this and have wanted my code to be as efficient as possible (in terms of overkill).
Currently i can capture the click event on that particular ImageView only, but I can't know which row was clicked.
I have provided an attribute in the Row XML like this-
<ImageView android:id="#+id/user_image"
android:padding="5dip"
android:layout_height="60dip"
android:layout_width="60dip"
android:clickable="true"
android:onClick="uImgClickHandler"/>
And in my code, I have a method like this:
public void uImgClickHandler(View v){
Log.d("IMG CLICKED", ""+v.getId());
LinearLayout parentRow = (LinearLayout)v.getParent();
}
I can get the parent row (perhaps) but am not sure how to go further from here.
Can someone please help?
Please refer this,
Me just writing the code to give you idea, Not in correct format
class youaddaper extends BaseAdapter{
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflate = LayoutInflater.from(context);
View v = inflate.inflate(id, parent, false);
ImageView imageview = (ImageView) v.findViewById(R.id.imageView);
imageview.setOnClickListener(new imageViewClickListener(position));
//you can pass what ever to this class you want,
//i mean, you can use array(postion) as per the logic you need to implement
}
class imageViewClickListener implements OnClickListener {
int position;
public imageViewClickListener( int pos)
{
this.position = pos;
}
public void onClick(View v) {
{// you can write the code what happens for the that click and
// you will get the selected row index in position
}
}
}
Hope it helped you
Another option is to use the methods setTag() and getTag() of the view. You set it in your getView like this:
imageView.setTag(new Integer(position));
Then in the onClick() you can find the tag by:
Integer tag = v.getTag();
This will then be used to correlate the image view to the position of the listview item.
Note that this approach will give problems if the listview can lose items from the middle, so that the item positions change during the lifetime of the listview.
you can simply do like this:
in the getview method of our adapter
Button btn1 = (Button) convertView.findViewById(R.id.btn1);
btn1.setOnClickListener(mActivity);
further you can handle the onclick event in your activity,,
for the context of the activity here mActivity just pass the this in the constructer of the adapter and cast it here into the activity like
MyActivity mActivity=(MyActivity)context;
in the adapter.
thanx
This appears to work in a ListActivity whose item layout contains an ImageView with android:onClick="editImage":
public void editImage(View v) {
int[] loc = new int[2];
v.getLocationInWindow(loc);
int pos = getListView().pointToPosition(loc[0], loc[1]);
Cursor c = (Cursor) adapter.getItem(pos);
// c now points at the data row corresponding to the clicked row
}