Populating listview with FirebaseListAdapter in Android - 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

Related

how to limit number of checked checkBoxes android

how can I limit the number of checked checkboxes in android? I have multiple checkboxes being added programatically and it's difficult to keep track of them.
here's the code used to add them:
final CheckBox currentVariantCheckbox = new CheckBox(getApplicationContext());
checkBoxGroupList.add(currentVariantCheckbox);
Log.d(TAG, "onDataChange: added " + currentVariantCheckbox + " to the checkboxgrouplist; size = " + checkBoxGroupList.size());
currentVariantCheckbox.setChecked((Boolean) currentVariant.child("checked").getValue());
LinearLayout checkboxGroupLayout = new LinearLayout(getApplicationContext());
checkboxGroupLayout.setOrientation(LinearLayout.HORIZONTAL);
currentVariantCheckbox.setText(currentVariant.child("name").getValue(String.class));
TextView currentVariantPriceTag = new TextView(getApplicationContext());
checkboxGroupLayout.addView(currentVariantCheckbox);
if (currentVariant.child("price").exists()) {
currentVariantPriceTag.setText("+" + currentVariant.child("price").getValue(float.class).toString() + " €");
checkboxGroupLayout.addView(currentVariantPriceTag);
ok so instead of using a onCheckedStateChangedLister I used OnClickListener. And I created an ArrayList to keep track of all the checked checkboxes:
final ArrayList<CheckBox> checkedList = new ArrayList<>();//this is the list to keep track of checked checkboxes
int maxOptions = 3
int minOptions = 1
currentVariantCheckbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Boolean currentCheckState = currentVariantCheckbox.isChecked();
if (currentCheckState) {//if the clicked checkboxes was unchecked and is now checked
checkedList.add(currentVariantCheckbox);
Log.d(TAG, "onClick: added " + currentVariantCheckbox + " ;checkedList.size is now: " + checkedList.size());
if (checkedList.size() >= maxOptions) {
checkedList.get(0).setChecked(false);
checkedList.remove(0);// if limit exceeded remove the first element from the checked list
Log.d(TAG, "onCheckedChanged: checkedList's size is now: " + checkedList.size());
}
} else if (checkedList.size() <= minOptions) {
currentVariantCheckbox.setChecked(true);
// if the list is empty just add the clicked checkbox to the list for example here 0
// and it will be checked automatically
} else {
checkedList.remove(currentVariantCheckbox);
// if the checkbox was already checked and no limit is exceeded
// then it will be unchecked therfore it should be removed from checkedList
}
}
});

Empty Row in Firebase UI with Condition?

I have a problem with Firebase when using FirebaseUI. It adds an unwanted empty row when the condition is not true.
I need to delete this Empty Row.
this is the image Show the problem :
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("*****");
FirebaseListAdapter<Opinions> firebaseListAdapter = new FirebaseListAdapter<Opinions>(this,Opinions.class,android.R.layout.two_line_list_item,databaseReference) {
#Override
protected void populateView(View v, Opinions model, int position) {
TextView textView1 = (TextView) v.findViewById(android.R.id.text1);
TextView textView2 = (TextView) v.findViewById(android.R.id.text2);
Typeface Myfont = Typeface.createFromAsset(getAssets(), "font/gernralfont.ttf");
textView1.setTypeface(Myfont);
textView2.setTypeface(Myfont);
textView2.setTextSize(17);
textView2.setTextColor(Color.parseColor("#6eaafc"));
textView1.setTextColor(Color.parseColor("#4d70a0"));
if (model.isApproval() == true)
{
textView1.setText( "\n" + model.getStu_name() + " / " + model.getStu_feild() + "\n" );
textView2.setText(model.getStu_comment() + "\n");
}
}
};
hilist.setAdapter(firebaseListAdapter);
How I can remove this empty line?
Your app is current reading all data at the location. But since you're only interested in showing data that is approved, you should only read those child nodes.
You can do this with a Firebase Database query, which you can then pass into the adapter instead of the current reference:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("*****");
Query query = databaseReference.orderByChild("approval").equalTo(true);
FirebaseListAdapter<Opinions> firebaseListAdapter =
new FirebaseListAdapter<Opinions>(this, Opinions.class,
android.R.layout.two_line_list_item, query) {
...

I have 4 textviews which take their values from spinner. If TextView 3 and 4 are blank then I need to copy values from position 1 & 2

I have 4 textviews which take their values from dropdown list (spinner) selected at previous screen.
There can be either 2 or 4 numbers/letters as result of this selection.
The first position will always be a number and the second position will always be a letter. The third position can be a number or blank and the fourth position can be a letter or blank.
If position 3 and position 4 are blank then I need to make them equal to positions 1 & 2 respectively.
String myGrade = intent.getStringExtra("parameter_name_grade");
// above takes value of 'myGrade' from spinner selection at previous screen
String mDisplayGradeNumberEff = (" " + myGrade.charAt(0));
TextView displayGradeNumberEff = (TextView) findViewById(R.id.gradeNumberEffTV);
displayGradeNumberEff.setText(mDisplayGradeNumberEff);
String mDisplayGradeLetterEff = (" " + myGrade.charAt(1));
TextView displayGradeLetterEff = (TextView) findViewById(R.id.gradeLetterEffTV);
displayGradeLetterEff.setText(mDisplayGradeLetterEff);
// above works correctly
// from here down only works when a character is present in both positions
// if positions 3(2) and 4(3) are empty app stops running.
String mDisplayGradeNumberDia = (" " + myGrade.charAt(2));
if (mDisplayGradeNumberDia.isEmpty()) {
mDisplayGradeNumberDia = mDisplayGradeNumberEff;
}
TextView displayGradeNumberDia = (TextView) findViewById(R.id.gradeNumberDiaTV);
displayGradeNumberDia.setText(mDisplayGradeNumberDia);
String mDisplayGradeLetterDia = (" " + myGrade.charAt(3));
if (mDisplayGradeLetterDia.isEmpty()) {
mDisplayGradeLetterDia = mDisplayGradeLetterEff;
}
TextView displayGradeLetterDia = (TextView) findViewById(R.id.gradeLetterDiaTV);
displayGradeLetterDia.setText(mDisplayGradeLetterDia);
}
I Guess you have a array out of bounds exception, please provide Logcat....
Check if "myGrade" has 3/4 Characters, if it does not you can't read them with charAt(3)...
You can check the length of the String with "myGrade.length()"
When I asked this question I was fairly new to the site and didn't understand that I should post back the solution for future reference. Solution below worked so thanks to rocket for your help and sorry for the delay!
int myGradeLength = mGrade.length();
if (myGradeLength != 4) {
mDisplayGradeNumberEff = ("" + mGrade.charAt(0));
mDisplayGradeLetterEff = ("" + mGrade.charAt(1));
mDisplayGradeNumberDia = ("" + mGrade.charAt(0));
mDisplayGradeLetterDia = ("" + mGrade.charAt(1));
} else {
mDisplayGradeNumberEff = ("" + mGrade.charAt(0));
mDisplayGradeLetterEff = ("" + mGrade.charAt(1));
mDisplayGradeNumberDia = ("" + mGrade.charAt(2));
mDisplayGradeLetterDia = ("" + mGrade.charAt(3));
}

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.

Set text color of a textview in a listview item? (Android)

Been scouring this site for any answers, no real easy solution I've found for this. I am creating an Android application that uses an sqlite database to look up a hex value by the color name typed in. I am dynamically creating a TextView, setting its text and text color, then adding it to the ArrayList, then the ArrayList is being added to the ListView. The text shows up in the ListView, but its color property is not being set. I'd really like to find a way to get the text color set for each listview item. Here is my code thus far:
Class Variables:
private ListView lsvHexList;
private ArrayList<String> hexList;
private ArrayAdapter adp;
In onCreate():
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color2hex);
lsvHexList = (ListView) findViewById(R.id.lsvHexList);
hexList = new ArrayList<String>();
In my Button Handler:
public void btnGetHexValueHandler(View view) {
// Open a connection to the database
db.openDatabase();
// Setup a string for the color name
String colorNameText = editTextColorName.getText().toString();
// Get all records
Cursor c = db.getAllColors();
c.moveToFirst(); // move to the first position of the results
// Cursor 'c' now contains all the hex values
while(c.isAfterLast() == false) {
// Check database if color name matches any records
if(c.getString(1).contains(colorNameText)) {
// Convert hex value to string
String hexValue = c.getString(0);
String colorName = c.getString(1);
// Create a new textview for the hex value
TextView tv = new TextView(this);
tv.setId((int) System.currentTimeMillis());
tv.setText(hexValue + " - " + colorName);
tv.setTextColor(Color.parseColor(hexValue));
hexList.add((String) tv.getText());
} // end if
// Move to the next result
c.moveToNext();
} // End while
adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hexList);
lsvHexList.setAdapter(adp);
db.close(); // close the connection
}
You are not adding the created TextView to the list at all, you just add the String to the list, thus it doesn't matter what method you called on the TextView:
if(c.getString(1).contains(colorNameText)) {
// ...
TextView tv = new TextView(this);
tv.setId((int) System.currentTimeMillis());
tv.setText(hexValue + " - " + colorName);
tv.setTextColor(Color.parseColor(hexValue));
hexList.add((String) tv.getText()); // apend only the text to the list
// !!!!!!!!!!!!!! lost the TextView !!!!!!!!!!!!!!!!
}
What you need to do is to store the colors in another array, and when creating the actual list view, set the color of each TextView according to the appropriate value in the list.
To do that, you will need to extend ArrayAdapter and add the logic of the TextView color inside.

Categories

Resources