I am trying to set the TextView of the Spinner, but I cannot reach it. There are many solutions about this issue but all of them solve from Spinner Adapter. In my issue I must not change the text from Adapter. I want to change it from the spinner object itself.
So I tried several ways to solve it.
1)
LayoutInflater layoutInflater = LayoutInflater.from(spnSektorInsert.getContext());
View convertView = layoutInflater.inflate(R.layout.spinner_item, null);
TextView txtSpinner = (TextView) convertView.findViewById(R.id.txtSpinner);
txtSpinner.setText("Something");
txtSpinner.setTextColor(Color.RED);
2)
View convertView = spnSektorInsert.getAdapter().getView(0, null, null);
TextView txtSpinner = (TextView) convertView.findViewById(R.id.txtSpinner);
txtSpinner.setText("Something");
txtSpinner.setTextColor(Color.RED);
3)
TextView txtSpinner = (TextView) spnSektorInsert.getChildAt(0);
txtSpinner.setText("Something");
txtSpinner.setTextColor(Color.RED);
In the first two ways, nothing happened, in the last way, the application crashed with NullPointerException. Any ideas will be appreciated.
Related
I have inflated one layout in my Activity now in that Activity I have some common fields for multiple times, so I have created one more layout with those common fields and included in my layout.And I got inflated all those fields.But now My problem is that While I am inserting data in those fields only first included fields gets inserted with data not the second, third, and so on... So how to get data inserted in such fields with common ids..
LayoutInflater inflater = LayoutInflater.from(getContext());
View w4FragmentRowView = inflater.inflate(R.layout.weekly_report4_row_item, null);
etWhere = (EditText)w4FragmentRowView.findViewById(R.id.et_weekly_reports_where_w4_tab);
etHowSubStd = (EditText)w4FragmentRowView.findViewById(R.id.et_weekly_reports_how_sub_std_w4_tab);
Now I have to insert data multiple times in same activity with these edit Text fields.
Say you have two views and they have common ids and you inflate them
Like shown below
now for common ids when you are doing find view by id make sure
the point to right parent
//look at item and item2 as while finding view
LinearLayout item = (LinearLayout) mLayoutInflater.inflate(
R.layout.layout1, null);
TextView skunitNameTextView = (TextView) item.findViewById(R.id.textView);
LinearLayout item2 = (LinearLayout) mLayoutInflater.inflate(
R.layout.layout1, null);
TextView skunitNameTextView2 = (TextView) item2.findViewById(R.id.textView);
i think this should solve your problem
Take example you have a layout file with name "reuse.xml" where you have only one view TextView with id "txv".
Now in your Activity you are inserting this layout file multiple times like:-
TextView tv1 = (TextView) mLayoutInflater.inflate(
R.layout.reuse, null);
tv1.setId(R.id.txv_activity_1);
TextView tv2 = (TextView) mLayoutInflater.inflate(
R.layout.reuse, null);
tv2.setId(R.id.txv_activity_2);
TextView tv3 = (TextView) mLayoutInflater.inflate(
R.layout.reuse, null);
tv3.setId(R.id.txv_activity_3);
Now you are using the same layout file but they gave different id so you can easily update any of file which one you want.
But if you are using the same layout more than 3-4 times it is recommended to use it as ListView or RecyclerView item, which also provide you better performance in case of large layout and easy to maintain.
I created two activites, the first one is containing the dynamic added textview and the second one will do some operation then settext to the dynamic added textview in activity one. please help to suggest a sample code for my reference. Thank you!
To add textViews can dynamically using the following method.
private void addChild(boolean right) {
LayoutInflater inflater = LayoutInflater.from(this);
int id = R.layout.unLayout_;
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(id, null, false);
TextView textView = (TextView) relativeLayout.findViewById(R.id.textViewDate);
textView.setText(String.valueOf(System.currentTimeMillis()));
layout.addView(relativeLayout);
}
I have a ListView that I add a headerView as below. When the user clicks a button, I attempt to update a TextView that is inside the ListView's header View
if(mHeaderView == null){
mHeaderView = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.header_report, listViewReports, false);
}
TextView textViewName = (TextView) mHeaderView.findViewById(R.id.report_header_name);
textViewName.setText(student.getFullName();
The TextView only updates if the TextView changes size - for example if the student.getFullName() returns a longer name causing the textView to wrap to another line.
I have tried invalidating textViewName and mHeaderView. Also I have tried having textViewName as a class variable. Neither have worked.
What would be the proper way to achieve this?
Store reference to the header text that was originally used when creating the Header. Then just call the setText when needed.
I'm working on a ListView based app and I have a very weird problem, my ListItems are reappering and the correct item is not shown in the correct spot. For the sake of making this easy to understand I've set the text on each ListItem to be the same as it's position. I'm doing this in my adapters getView() call. If I have my Nexus 7 4 ListItems are visible. If I have a total of 10 ListItems then it will go like 0, 1, 3, 4, 0, 1, 2, 3, 4. This goes for all devices meaining that the number of items initially on screen + 1 will be correct while all other ListItems are rearrenged.
In which part of my code do you guys think my problem lies because right now I've been trying to fix this for hours and I'm clueless. All help is very much appreciated.
EDIT:
Here's my getView():
#Override
public View getView(int position, View convertView, ViewGroup parent) {
CountdownItem ci = mTitle.get(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, parent, false);
holder = new CountdownViewHolder();
holder.mTitle = (TextView) convertView.findViewById(R.id.textPrim);
holder.mSubtitle = (TextView) convertView
.findViewById(R.id.textSec);
holder.mDayProgress = (ProgressBar) convertView
.findViewById(R.id.day_progress);
holder.mMonthProgress = (ProgressBar) convertView
.findViewById(R.id.month_progress);
holder.mYearText = (TextView) convertView
.findViewById(R.id.year_text);
holder.day_help = (TextView) convertView
.findViewById(R.id.day_help);
holder.month_help = (TextView) convertView
.findViewById(R.id.month_help);
holder.setTitle(Integer.toString(position) + " Title");
holder.setSubtitle(ci.getSubtitle());
holder.fixImageAndText(position);
convertView.setTag(holder);
} else {
holder = (CountdownViewHolder) convertView.getTag();
}
return convertView;
}
You aren't using the ViewHolder pattern correctly. The following code needs to be moved outside the if/else clause and before return convertView:
holder.setTitle(Integer.toString(position) + " Title");
holder.setSubtitle(ci.getSubtitle());
holder.fixImageAndText(position);
This is the correct behaviour for the listview when it is reusing cells, the problem is that you only set the values when the cell is first created.
When convertView == null the listview has no cell to recycle. However, once it has created a few it can reuse them to display as you scroll.
What you need to do is set the title and subtitle even when convertView is not null. That way you're setting them for each new list position.
Yes, this is because android reuses views in lists, to increment performance and rendering speed.
The holder pattern is used to store views ids. After you retrieve them, you have to set the text you want to see inside.
For example, you retrieve your data (e.g. myDataArray[position]), and if it's all ok, you proceed setting title, subtitle, dayprogress, etc. with TextView's setText().
How can I inflate several same layouts with diffrent texts and images? I tried use this code
for (final Tour t : info.tours) {
final LinearLayout list = (LinearLayout) findViewById(R.id.tours_list);
Log.d(Const.LOG_TAG, "Add child view to tours_list");
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View child = inflater.inflate(R.layout.tour_item_for_guide_info,
list, true);
final ImageView tourImage = (ImageView) findViewById(R.id.tour_image);
final String tourImgUrl = Const.HOST + t.image;
imageLoader.DisplayImage(tourImgUrl, tourImage);
TextView tourText = (TextView) findViewById(R.id.text);
tourText.setText(t.name);
}
but text and image setted in first inflated layout, replacing the previous text and image. I understand what it's happens because layouts have same ids. Can I resolve it? I know about ListView and adapters, but I would like to learn if it might be done without them.
I assume that the ImageView and TextView - tourImage, tourText - are elements of R.layout.tour_item_for_guide_info.
If this is so, then when you reference it, you should use the child view to get them.
With other words, instead of:
ImageView tourImage = (ImageView) findViewById(R.id.tour_image);
TextView tourText = (TextView) findViewById(R.id.text);
You should have:
ImageView tourImage = (ImageView)child.findViewById(R.id.tour_image);
TextView tourText = (TextView)child.findViewById(R.id.text);
Not sure if this will definetly fix your problem, but it looks like a bug.