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.
Related
With following function, I am adding view dynamically. but through this, last call values to "addview" are getting set in all views. Like If i call addview function with "aaaa" in its 0 index it will add one view with aaaa name, and then again i call addview with "bbbb" in its 0 index it will add one more view but this time it set "bbbb" to previous view also. Means now both dynamically added view will have "bbbb" value in its edittext. Please help.
public void addView(String[] values) {
layoutInflater =
(LayoutInflater) getActivity( ).getBaseContext( ).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = layoutInflater.inflate(R.layout.lay_fcp_child, null);
ImageView ivRemove;
EditText etName;
RelativeLayout layDOB;
final TextView tvDate;
final Spinner spRelationship;
tvDate = (TextView) convertView.findViewById(R.id.tv_ins_dob);
layDOB = (RelativeLayout) convertView.findViewById(R.id.lay_ins_dob);
etName = (EditText) convertView.findViewById(R.id.et_ins_personname);
etName.setFilters(new InputFilter[]{filterName, new InputFilter.LengthFilter(60)});
ivRemove = (ImageView) convertView.findViewById(R.id.iv_fcp_remove);
spRelationship = (Spinner) convertView.findViewById(R.id.sp_fcp_relationship);
AdapterIDValue adapterRelations = new AdapterIDValue(getActivity(), R.layout.spinner_selected_item_relation, alSRelationship);
spRelationship.setAdapter(new NothingSelectedSpinnerAdapter(adapterRelations, R.layout.spinner_hint_relationship, getActivity()));
etName.setText(values[0]);
spRelationship.setSelection(Integer.parseInt(values[3]));
tvDate.setText(values[1]);
tvDate.setTag(values[2]);
tvDate.setTextColor(Color.BLACK);
try {
layDynamic.addView(convertView);
}catch (Exception sdf){
System.err.print(sdf);
}
svMain.fullScroll(ScrollView.FOCUS_DOWN);
}
Yes you can create with RecyclerView OR ListView, or if you want to do with scroll view the generate dynamic Ids of every view, so you can solve your problem.
Each call to addview(...) method will create a new adapter and set it to spRelationship. This new adapter replace the previous. You are not "adding views", you're "setting the views".
You have to set the adapter only once and change its data.
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?
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
I have an activity where linearlayouts are added dynamically into a base linearlayout.
You'll understand from my code, but I'm trying to resuse the same "addBlock" method to add more data in the activity. If you can help me out with this, I would appreciate it! Thanks!
My code:
private void addData01() {
wii=R.drawable.img01;
d=getString(R.string.one_d); //this is later set to a seperate textview
//START OF BLOCK 1//
b1_t = getString(R.string.Title1);
b1 = new String[]{"DATA WILL GO HERE"};
b1_i = new String[]{"DATA WILL GO HERE"};
addBlock();
//START OF BLOCK 2//
b1_t = getString(R.string.Title2);
b1 = new String[]{"MORE DATA WILL GO HERE"};
b1_i= new String[]{"MORE DATA WILL GO HERE"};
addBlock();
}
private void addBlock() {
LayoutInflater inflater = LayoutInflater.from(this);
for(int i = 0 ; i < b1.length ; i++)
{
View childView = inflater.inflate(R.layout.two_row, null,false);
TextView tv = (TextView)childView.findViewById(R.id.one);
TextView tv2 = (TextView)childView.findViewById(R.id.two);
tv.setText(Html.fromHtml(b1[i]));
tv2.setText(Html.fromHtml(b1_i[i]));
b_ll.addView(b1t);
b_ll.addView(childView);
}
}
However when I do this I get the following error:
08-05 21:18:15.462: E/AndroidRuntime(4241): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I have tried looking at this How to add same view to parent multiple times by inflating it only once but the solutions there did not seem to work
I basically have 3 different objects in my list view:
TextView1
TextView2
TextView3
I want to get the object ID from the list view that's created dynamically. Ex: How do I set TextView2 with a background image from a list view in position 1?
I've tried using
lv.getItemAtPosition(1);
This will return the whole row and I'm just looking for the object ID TextView2 inside lv.getItemAtPosition(1)? Once I get the objectID from a certain position in list view, I can than change the TextView2 background.
Sorry, if I didn't explain clear enough. Does anyone know what I'm talking about?
If you write about row, I assume something like that can work for you:
TableRow tableRow = lv.getItemAtPosition(1);
for (int i = 0; i < tableRow.getChildCount(); i++) {
View child = tableRow.getChildAt(i);
if ( child instanceof TextView ) {
TextView textView = (TextView) child;
textView.DO_SOMETHIG__WITH_TEXT_VIEV();
textView.requestLayout();
}
}
tableRow.requestLayout();
Of course if you have some other row than tableRow, you can try to change it to that type.