Dynamic views are not properly loaded - android

Im retrieving values from sqlite db and trying to display the values dynamically.
This is how I retrieve the values,
List<PendingOrdersDao> contacts = db.getAllPendingOrders();
for (PendingOrdersDao cn : contacts) {
initView(cn.getProformoInvoiceNumber(), cn.getProformoInvoiceDate());
}
When I invoke initView method, it should add views to the layout dynamically depending on the number of entries present in db. Following is my initView method.
private void initView(String proformaInvoice, String invoiceDate){
LinearLayout mLinearLayoutContainer = (LinearLayout) mRootView.findViewById(R.id.dashboard_container);
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dashboard_fragment, mLinearLayoutContainer, false);
mProformaInvoice = (TextView) v.findViewById(R.id.dashProformaInvoiceNo);
mInvoiceNumber = (TextView) v.findViewById(R.id.dashInvoiceNo);
mProformaInvoice.setText(proformaInvoice);
mInvoiceNumber.setText(invoiceNumber);
mLinearLayoutContainer.addView(v);
}
The problem is that, there are three entries in the db hence 3views should be displayed but it displays only one view

This is my working code :-
for (int i = 0; i < 3; i++) {
LinearLayout mLinearLayoutContainer = (LinearLayout) findViewById(R.id.test_container);
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(android.R.layout.simple_list_item_1, mLinearLayoutContainer, false);
mTextView = (TextView) v.findViewById(android.R.id.text1);
mTextView.setText("This is i : " + i);
mLinearLayoutContainer.addView(v);
}
See if helps you. :)

First of all, check the contacts list size.
Do you setup correct orientation for your linear layout? Vertical or horizontal?
advice
Why not to use RecyclerView or ListView?

Related

how to use custom layout in existing layout using dynamic linearlayout

Hello i am developing one app where i want to inflate layout in my Card view fro existing layout.And i want to show list of text view dynamically according to my string array.
Here is my existing layout
and i want to inflate following layout in last cardview of above layout
so tried with coding to do it but it display only one textview
my coding is as follows
But it is not working it display only one textview
i want to add textview dynamically according to my string size
I want to inflaate layout just like listview but i dont want to use listview.i want to implement this without listview dynamically
it display only one textview i want to add textview dynamically
according to my string size
Probably due to inflating inflate_specification every time for each TextView and initializing container inside for-loop.
Do it as :
LinearLayout container = (LinearLayout)findViewById(R.id.temp);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i=0;i<5;i++)
{
View view = inflater.inflate(R.layout.inflate_specification, container,false);
// Create TextView
TextView product = (TextView)view.findViewById(R.id.speci);
product.setText("i= " + i);
container.addView(view);
}
Try this code:
LinearLayout container = (LinearLayout)findViewById(R.id.temp);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
container.removeAllViews();
for(int i=0;i<5;i++) {
View view = inflater.inflate(R.layout.inflate_specification, container,false);
// Create TextView
TextView product = (TextView)findViewById(R.id.speci);
product.setText("i= " + i);
container.addView(view);
}

Unable to add dynamic textView in android

I want to add textview dynamically for which I have added the below code.But I am do not see any textview on my activity. I cant add a listview as I am already using recycleview . Any one any error ?
{
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout review_layout = (LinearLayout) rootMovieView.findViewById(R.id.review_comment);
for (ReviewsResult comment : review.getReviewsResults()) {
View view = layoutInflater.inflate(R.layout.layout_movie_comments, review_layout, false);
TextView tvCommentBy = (TextView) view.findViewById(R.id.tv_comment_by);
TextView tvCommentContent = (TextView) view.findViewById(R.id.tv_comment_content);
tvCommentContent.setText(comment.getContent());
tvCommentBy.setText(comment.getAuthor());
review_layout.addView(view);
}
}
You can also try something like this
View myLay; // view that contains you textView
LinearLayout rootlay; //root view in which you want to add
myLay = LayoutInflater.from(this).inflate(R.layout.layout_movie_comments,
null);
TextView tvCommentBy = (TextView) view.findViewById(R.id.tv_comment_by);
TextView tvCommentContent = (TextView) myLay.findViewById(R.id.tv_comment_content);
tvCommentContent.setText(comment.getContent());
tvCommentBy.setText(comment.getAuthor());
rootlay.addView(myLay);
Hope this helps :)
Do below two changes and check weather it is working or not, as nothing else seems to wrong in code.
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.layout_movie_comments, null);
rootMoviewView is not the rootView it was a recylceView in a rootView .This way the textview is generated in rootView not in recylceView.

Android - programatically changing the row layout of a custom listView row fails when focus is requested on its edittext child

Sorry for the long title.
I have an android application which shows a custom listView with 5 TextView columns. When the user clicks a row, I change the layout to have 3 TextViews and 2 EditTexts. I have different layout files for both of them. Everything worked fine initially, the row layout changes properly and I am able to click on the EditText and input values. However, I want either of the 2 EditText to automatically gain focus based on what is clicked. I already have a working code for this. My problem is that programatically requesting requestFocus() seems to block the part where I change the row layout with the new view.
Here is the code that changes my row layout, it works fine without the requestFocus() line:
private void changeLayout(final View view){
//get views from old row layout
TextView textViewQuantity = (TextView)view.findViewById(R.id.qtyInput);
TextView textViewDiscountReq = (TextView)view.findViewById(R.id.discInput);
TextView textViewName = (TextView)view.findViewById(R.id.dialogItemName);
TextView textViewPrice = (TextView)view.findViewById(R.id.price);
TextView textViewDiscount = (TextView)view.findViewById(R.id.discount);
//store values in strings
String itemName = textViewName.getText().toString();
String itemPrice = textViewPrice.getText().toString();
String itemDiscount = textViewDiscount.getText().toString();
String itemQty = textViewQuantity.getText().toString();
String itemDisc = textViewDiscountReq.getText().toString();
//set the view to gone
textViewQuantity.setVisibility(View.GONE);
textViewDiscountReq.setVisibility(View.GONE);
textViewName.setVisibility(View.GONE);
textViewPrice.setVisibility(View.GONE);
textViewDiscount.setVisibility(View.GONE);
//get the old layout
LinearLayout ll_inflate = (LinearLayout)view.findViewById(R.id.search_result_layout);
//get the inflate/new view
View child = getLayoutInflater().inflate(R.layout.search_result_inflate, null);
//get the views in the new view, populate them
TextView newName = (TextView)child.findViewById(R.id.dialogItemName);
newName.setText(itemName);
TextView newDiscount = (TextView)child.findViewById(R.id.discount);
newDiscount.setText(itemDiscount);
TextView newPrice = (TextView)child.findViewById(R.id.price);
newPrice.setText(itemPrice);
qtyIn = (EditText)child.findViewById(R.id.qtyInputSearchResult);
qtyIn.setText(itemQty);
qtyIn.setFilters(new InputFilter[] {filter});
discIn = (EditText)child.findViewById(R.id.discInputSearchResult);
discIn.setText(itemDisc);
//show new layout
ll_inflate.removeAllViews();
ll_inflate.removeAllViewsInLayout();
ll_inflate.addView(child);
//request focus here
if(focusTarget == 1){
Log.d("hello", "focus target is 1 " );
qtyIn.setFocusable(true);
qtyIn.setFocusableInTouchMode(true);
qtyIn.requestFocus();
}
else if(focusTarget == 2){
Log.d("hello", "focus target is 2 " );
discIn.requestFocus();
}
Log.d("hello", "focus state qtyIn = " + qtyIn.isFocused());
Log.d("hello", "focus state discIn = " + discIn.isFocused());
}
The interesting part is that the Log shows the proper values, it says the proper focus status according to what I want. However, the ll_inflate.addView(child); line does not work at all!
Does anyone know what happened here? I'm really confused as to why the layout didn't change but the lines after the .addView() line executed. Another weird thing is how requestFocus(); prevents the view from changing.
Any help is very much appreciated. Thanks.
You are doing this in wrong way. You should have two type of view in list, one for show data and one for edit it.
First set view type count
final int TYPE_EDIT =0,TYPE_VIEW =1;//EDIT 7/9/016 type need to be start from 0
public int getViewTypeCount (){
return 2;
}
public int getItemViewType (int position){
//return type as needed. simple if you add int for this in data model
}
And inside getView
public View getView (int position, View convertView, ViewGroup parent){
int type = getItemViewType ();
if(null==convertView){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(type ==TYPE_EDIT){
convertView = inflater.inflate(R.layout.rowEditlayout, parent, false);
}else{
convertView = inflater.inflate(R.layout.rowlayout, parent, false);
}
}
}
Do the changes in list data on edit button click and call notifydatasetchanged on adapter

Adding View - Android

I have a custom view. I am trying to add multiple arrray values. But its just printing the last one. For instance check the below code. I am trying to dispay schedulePeriod.scheduleMatchups.get(i). It has two values. (get (0) and get (1). Its always getting the value of get(1) not get(0). scheduleView.add ( is returning one value not both) can anyone help me?
Thank You
private ArrayList<View> getScheduleView(final SchedulePeriod schedulePeriod) {
String teamid = FantasyFootballApplication.getFantasyTeam().teamId;
View scheduleView = playerRankingsInflate(R.layout.schedule_view);
ArrayList<View> scheduleViews = new ArrayList<View>();
for(int i=0; i<schedulePeriod.scheduleMatchups.size(); i++){
if(!(schedulePeriod.scheduleMatchups.isEmpty())) {
TextView weekNumber = (TextView) scheduleView.findViewById(R.id.week_number);
weekNumber.setText(schedulePeriod.scheduleId);
if(!(teamid.equals(schedulePeriod.scheduleMatchups.get((i)).scheduleAwayTeam.awayTeamId))) {
TextView scheduleOpp = (TextView) scheduleView.findViewById(R.id.schedule_opp);
scheduleOpp.setText(schedulePeriod.scheduleMatchups.get(i).scheduleAwayTeam.awayTeamShortName);
TextView scheduleName = (TextView) scheduleView.findViewById(R.id.schedule_teamname);
scheduleName.setText(schedulePeriod.scheduleMatchups.get(i).scheduleAwayTeam.awayTeamName);
} else if(!(teamid.equals(schedulePeriod.scheduleMatchups.get((i)).scheduleHomeTeam.homeTeamId))) {
TextView scheduleOpp = (TextView) scheduleView.findViewById(R.id.schedule_opp);
scheduleOpp.setText(schedulePeriod.scheduleMatchups.get(i).scheduleHomeTeam.homeTeamShortName);
TextView scheduleName = (TextView) scheduleView.findViewById(R.id.schedule_teamname);
scheduleName.setText(schedulePeriod.scheduleMatchups.get(i).scheduleHomeTeam.homeTeamName);
}
}
scheduleViews.add(scheduleView);
}
return scheduleViews;
}
As I can see you are creating only one view and adding it to list in for loop. And therefore you are editing only this view and you see last value in your scheduleMatchups.
You want to place your
View scheduleView = playerRankingsInflate(R.layout.schedule_view);
inside the for loop and then alter every view according and add it to your list.
Hope this helps and enjoy your work.

How to get a view which is inside a complex ListView row view?

I have a ListView which has been populated by rows containing data using an adapter.
The row layout is of "RelativeLayout" type and contains various view objects, the id of one of them is "#+id/bar_fill".
I have a loop running over all the childs of the ListView, and I want to "get" the view item i mentioned above.
The code I'm using is:
ListView list = getListView();
int count = list.getCount();
for (int i = 0; i < count; i++)
{
RelativeLayout row = (RelativeLayout) list.getChildAt(i);
View bar = row.findViewById(R.id.bar_fill);
}
However when it reaches the last line it crashes. When I commented it out and did
System.out.println(findViewById(R.id.bar_fill));
It returned nulls.
What am I doing wrong?
overide this function in custom adapter.
public View getView(int position, View convertView, ViewGroup parent);
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
}
Now like if you want certain textview from the listview row item then do it like this:
TextView t1 = (TextView) v.findViewById(com.pis.prototype.R.id.TextView01);
t1 is your required view now...:P
And if you want to get the already updated/created items in the list view and want to get some view from some row. Then do it like this..:
for (int i = 0; i < yourCustomAdapter.size(); i++) {
View viewRow = listViewParts.getChildAt(i);
CheckBox chkBox = (CheckBox) viewRow
.findViewById(R.id.checkBox);
yourCustomAdapter.get(i).attachmentCheck = chkBox
.isChecked();
}
In the above case I have extracted the checkbox views from the listview.

Categories

Resources