Dynamic numberpicker android does not work - android

I'm looking for instantiate dynamically a numberpicker. I want to place a value (kitQty - YourQty) in numberpicker order.
The next code is now working but only for the last one Id=9.
Because this only working with the last record?
Try setTag and setId and I can not change the numberpicker that desire. Anyone know what may be wrong in my code?
Thank you very much!
public class MyKits extends Fragment {
private Button performAudit;
private Button submitOrder;
NumberPicker yourQty;
NumberPicker kitQty;
NumberPicker order;
Conexion basedatos;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mykits, container, false);
Context context=v.getContext();
basedatos=new Conexion(v.getContext());
TextView tvModel = (TextView) v.findViewById(R.id.tvModel);
TableLayout table= (TableLayout) v.findViewById(R.id.Tabla);
tvModel.setText(getArguments().getString("model"));
for(int i=0;i<10;i++)
{
TableRow tR = new TableRow(v.getContext());
LinearLayout l1= new LinearLayout(v.getContext());
LinearLayout l2= new LinearLayout(v.getContext());
LinearLayout l3= new LinearLayout(v.getContext());
LinearLayout l4= new LinearLayout(v.getContext());
Button item = new Button(v.getContext());
kitQty = new NumberPicker(getActivity().getApplicationContext());
yourQty= new NumberPicker(getActivity().getApplicationContext());
order= new NumberPicker(getActivity().getApplicationContext());
item.setText("Prueba Imparjdsbfjbdsjfgbijsdfgijnsfdignsidfgh");
l1.addView(kitQty);
l2.addView(item);
l3.addView(yourQty);
l4.addView(order);
///TamaƱo Texto
item.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
kitQty.setMaxValue(100);
kitQty.setValue(50);
kitQty.setMinValue(0);
kitQty.setEnabled(false);
order.setMinValue(0);
order.setMaxValue(kitQty.getMaxValue());
yourQty.setMaxValue(kitQty.getMaxValue());
yourQty.setMinValue(0);
yourQty.setTag(i);
yourQty.setId(i);
order.setTag(i);
order.setId(i);
kitQty.setTag(i);
kitQty.setId(i);
yourQty.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Log.e("oldValue: " + oldVal + " ID: " + picker.getId(), " newVal: " + newVal + " ID: " + picker.getTag());
kitQty.setId(picker.getId());
yourQty.setId(picker.getId());
order.setId(picker.getId());
order.setValue(kitQty.getValue() - yourQty.getValue());
}
});
order.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Log.e("oldValue: " + oldVal + " ID: " + picker.getId(), " newVal: " + newVal + " ID: " + picker.getTag());
kitQty.setTag(picker.getTag());
yourQty.setTag(picker.getTag());
order.setTag(picker.getTag());
order.setValue(kitQty.getValue() - yourQty.getValue());
// calcularQty(picker);
}
});
tR.addView(l1);
tR.addView(l2);
tR.addView(l3);
tR.addView(l4);
table.addView(tR);
}
return v;}
public static MyKits newInstance(String model) {
MyKits f = new MyKits();
Bundle b = new Bundle();
b.putString("model", model);
f.setArguments(b);
return f;} }

Well, there are a few things wrong in your code...
But let's just focus on the main question in saying first that you should not assign id by yourself. I'd rather either inflate each of them with a granular xml layout file, or if you insist on creating your widgets using "new", then leave the id alone and Android should assign them properly.
But really the main reason why your code don't work is that you assign widget ids in a "for" loop with your counter variable (i) and you are using the same value as an id for order widget, yourQty widget and kitQty widget. You need to understand that each widget id must be unique in the same view hierarchy. First start by fixing this issue. But I won't tell you how: you should be able to fix this by yourself.

Related

Populating listview with FirebaseListAdapter in Android

I am using counter value for counting my ListView items. But when I am scrolling down and back scrolling up it still count value inside of my code and changes values.
This values should 1 and 2 instead of 7 and 8...
Here is my code
DatabaseReference myQuery = databaseReference.child("Tests").child(PIN).child("Questions");
FirebaseListAdapter<QuizStatistic_list> adapter = new FirebaseListAdapter<QuizStatistic_list>(this, QuizStatistic_list.class, R.layout.list_quiz_statistic, myQuery) {
#Override
protected void populateView(View v, QuizStatistic_list model, int position) {
TextView quizQuestion, questionNumber, ansA, ansB, ansC, ansD;
quizQuestion = (TextView) v.findViewById(R.id.textView_quizQuestion);
questionNumber = (TextView) v.findViewById(R.id.textView_questionNumber);
ansA = (TextView) v.findViewById(R.id.textView_ansA);
ansB = (TextView) v.findViewById(R.id.textView_ansB);
ansC = (TextView) v.findViewById(R.id.textView_ansC);
ansD = (TextView) v.findViewById(R.id.textView_ansD);
quizQuestion.setText(model.getQuestion());
questionNumber.setText(counter.toString());
ansA.setText("A: " + model.getAnswerA());
ansB.setText("B: " + model.getAnswerB());
ansC.setText("C: " + model.getAnswerC());
ansD.setText("D: " + model.getAnswerD());
//This value counts listview items
counter++;
}
};
questionsList.setAdapter(adapter);
use position instead of counter.!
questionNumber.setText(""+position+1);
position is index number of your array list .!
so it will start from 0 by adding one it will show your desired result first number will be index 0+1=1;
maybe it will help u like

Creating new Textviews in another activity based onClick android

I would like to create a new textview to hold the information sent each time the button is clicked. I have the data I want passing to another screen in a textview but each time i try to put new data it overrides this data because it uses the same textView (textview4). I would like to know if there is a way of creating a new text view to hold my data each time the button is clicked. I hope I was clear enough, thanks.
This code is from a class called CreateWorkout.Java
public void createNewWorkout (View view){
TextView Exercise1TextView = (TextView) findViewById(R.id.Exercise1TextView);
EditText weightEntered = (EditText)findViewById(R.id.WeightLiftedEditText);
EditText reps = (EditText)findViewById(R.id.RepsEditText1);
EditText sets = (EditText)findViewById(R.id.setsEditText1);
Intent getWorkoutIntent = new Intent(this, SelectWorkout.class);
getWorkoutIntent.putExtra("Workout", Exercise1TextView.getText().toString()
+ " " + weightEntered.getText().toString() + "kg"
+ " " + reps.getText().toString() + " reps"
+ " " + sets.getText().toString() + " sets");
startActivity(getWorkoutIntent);
}
This is where the intent is called. This is from SelectWorkout.Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_workout);
TextView textView4 = (TextView) findViewById(R.id.textView4);
textView4.setText(getIntent().getExtras().getString("Workout"));
}
i want to take the data entered here to the next screen. So the exercise name(Leg Press), weight(50), set(3), reps(10)
creating a new text view to hold my data each time
Not an exact answer, but this should guide you in the right direction. You could do something like the following:
Get your bundle
Iterate bundle
Create Textviews during iteration and add them to "your_main_layout" which is the id of the main layout inside select_workout
The bundle is the extra data you're sending over from the Intent. The Iterate is going through each item you sent over to this new intent, the create part refers to creating a TextView for each item you sent over. I've supplied links to what I found below.
Listing all extras of an Intent
//Get a bundle:
for (String key : bundle.keySet()) {
// This is each value (text) you sent over from the last intent
Object value = bundle.get(key);
//output data to log, so you can see what prints out
Log.d(TAG, String.format("%s %s (%s)", key, value.toString(), value.getClass().getName()));
//adding a textview to a layout called your_main_layout (which can be a linear layout or something)
//with value.toString() as the text
your_main_layout.addView(createATextView(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_PARENT_RIGHT,
value.toString(), 20, 10, 20));
}
How can I add a TextView to a LinearLayout dynamically in Android?
//method to create view:
public TextView createATextView(int layout_widh, int layout_height, int align,
String text, int fontSize, int margin, int padding) {
TextView textView_item_name = new TextView(this);
// LayoutParams layoutParams = new LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// layoutParams.gravity = Gravity.LEFT;
RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams(
layout_widh, layout_height);
_params.setMargins(margin, margin, margin, margin);
_params.addRule(align);
textView_item_name.setLayoutParams(_params);
textView_item_name.setText(text);
textView_item_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
textView_item_name.setTextColor(Color.parseColor("#000000"));
// textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView_item_name.setPadding(padding, padding, padding, padding);
return textView_item_name;
}

Button OnClicklistener from dynamically genereated Buttons

I have a problem to build correct OnClicklistener for buttons I generate at runtime. I found some threads here on stackoverflow but after many tries I don't get it working.
I have a methode with builds a GUI with a TextView in left "column" and in right "column" x buttons. Each Button has an other link which should be open by onClick. I don't know the link before it's created at runtime.
Here my Code with my actuall try. But in this Case I get everytime only the link of the last generated button. If I click on first one, second one .... it's everytime the same link.
Hope there is a solution for it!
private void createNewView (String JsonInputBeacon, String JsonInputConfig){
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
tableLayout.setStretchAllColumns(true);
try {
final JSONArray jsonArrayBeacon = new JSONArray(JsonInputBeacon);
final JSONArray jsonArrayConfig = new JSONArray(JsonInputConfig);
int Patientencounter = 1;
for(int JsonObjectCounterBeacon = 0; JsonObjectCounterBeacon < jsonArrayBeacon.length(); JsonObjectCounterBeacon ++ ){
final JSONObject objectBeacon = jsonArrayBeacon.getJSONObject(JsonObjectCounterBeacon);
TableRow row = new TableRow(this);
TextView outputLeft = new TextView(this);
outputLeft.setText("Patient " + Patientencounter + ":\n" + "Name: " + objectBeacon.getString("surname") + ", " + objectBeacon.getString("firstName") + "\n" + "Geb-Datum: " + objectBeacon.getString("birthdate"));
row.addView(outputLeft);
for (int JsonObjectCounterConfig = 0; JsonObjectCounterConfig < jsonArrayConfig.length(); JsonObjectCounterConfig++){
final JSONObject objectConfig = jsonArrayConfig.getJSONObject(JsonObjectCounterConfig);
TableRow rowRight = new TableRow(this);
Button buttonRight = new Button(getApplicationContext());
buttonRight.setText(objectConfig.getString("name"));
final String myURL = objectConfig.getString("link");
buttonRight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(myURL));
startActivity(browserIntent);
}
});
rowRight.addView(buttonRight);
row.addView(rowRight);
}
tableLayout.addView(row);
Patientencounter +=1;
}
} catch (JSONException e) {
e.printStackTrace();
}
setContentView(tableLayout);
}
But in this Case I get everytime only the link of the last generated
button. If I click on first one, second one .... it's everytime the
same link
Because myURLcontains value which is assigned by last iteration of for-loop.
Use setTag/getTag method of buttonRight to get url according to Button click. like:
Set value using setTag :
final String myURL = objectConfig.getString("link");
buttonRight.setTag(myURL);
and get myURL value using v parameter of onClick method:
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(v.getTag().toString()));
Set the tag of your button to be the URI, then access the tag from the view passed in to your onclick method.

How to add colour (i.e. red) on the dates where there are events?

I'm using tyczj's ExtendedCalendarView. I would like to add colour such as red onto dates where there are events. Have asked the same question here too.
Here are my codes:
Calendar.xml
<com.tyczj.extendedcalendarview.ExtendedCalendarView>
android:id="#+id/calendar"
android:layout_width="match_parent"
android:layout_height="416dp"
</com.tyczj.extendedcalendarview.ExtendedCalendarView>
Calendar.java
ExtendedCalendarView calendar = (ExtendedCalendarView)rootView.findViewById(R.id.calendar);
calendar.setOnDayClickListener(new OnDayClickListener() {
#Override
public void onDayClicked(AdapterView<?> adapter, View view, int position, long id, Day day) {
day.getDay();
int perfectmonth = day.getMonth()+1;
perfectdate = day.getYear() + "-" + perfectmonth + "-" + day.getDay();
Toast.makeText(getActivity(), ""+ perfectdate , Toast.LENGTH_SHORT).show();
accessWebService();
}
});
Bundle extras = getActivity().getIntent().getExtras();
value = extras.getString("ad");
For your info, Calendar.java has imported the ExtendedCalendarView. Any help is appreciated.

Android : How to get Dynamic EditText values

In my application I'am creating 10 EditText by dynamically. Now I want to give different value in run time and I want to add it to the list. I have assigned EditText object to the String variable like object.getText.toString(). But i cant get any value.I'am a beginner in android. Can anyone help me how to achieve this? Thanks in advance.
for(int i=0;i<=10;i++)
{
requirement = require.get(i);
RelativeLayout rl1 = new RelativeLayout(getActivity());
rl1.addView(req1);
req1estimate_value = new EditText(getActivity());
String value = req1estimate_value.getText().toString();
rl2.addView(req1estimate_value);
}
Try this. You should instantiate relative layout (rl1) at out of for loop, and should add child views with in that, so that all views could belongs to a parent layout. After that for accessing the values of all EditText you can use following:
String viewValue;
ViewGroup rootView = (ViewGroup) rl1;
int count = rootView.getChildCount();
for (int i = 0; i < count; i++) {
View view = rootView.getChildAt(i);
if (view instanceof EditText) {
viewValue = ((EditText) view).getText().toString();
Log.v("Value:: ", i + " " + viewValue);
} else if (view instanceof Spinner) {
viewValue = ((Spinner) view).getSelectedItem()
.toString();
Log.v("Value:: ", i + " " + viewValue);
}
}
Now after getting values you can put on a List or anywhere you want to use.

Categories

Resources