I want check cursor's row value. If cursor's row value is null I set "" my array's value, if cursor's row value not null i set "1" my array's value. How can i check my cursor's value ?
Cursor okunanlar = v3.GetAllRows();
okunanlar.moveToLast();
for(int i=0;i<10;i++)
{
if("How can i check my cursor's row value in here")
{
kayitlar[i]="";
}
else
{
kayitlar[i]="1";
}
okunanlar.moveToPrevious();
}
Solve problem!
Cursor okunanlar = v3.GetAllRows();
if (okunanlar.getCount() > 0) {
okunanlar.moveToLast();
for (int i = 0; i < (okunanlar.getCount()%10); i++) {
if (okunanlar.isNull(okunanlar.getColumnIndexOrThrow("name"))) {
kayitlar[i] = "";
okunanlar.moveToPrevious();
} else {
kayitlar[i] = okunanlar.getString(okunanlar
.getColumnIndex("name"));
okunanlar.moveToPrevious();
}
}
veriAdaptoru = new ArrayAdapter<String>(TabBar.this,
android.R.layout.simple_expandable_list_item_1,
android.R.id.text1, kayitlar);
Tab3.liste.setAdapter(veriAdaptoru);
}
}
Assuming that you want to check ONE field in your row you can use the following:
if(cursor.isNull(cursor.getColumnIndexOrThrow("column_index") || cursor.getString(cursor.getColumnIndexOrThrow("column_index")).equals("")) to check if it is null or empty.
Where "column_index" is the name of the column in your database
Edit: If what you want is the ROW position just use cursor.getPosition() to get the row position in your cursor.
Related
I have position that is equal to which question is shown. I want to add an int equal to position to an arraylist. Then I want to check and see if that arraylist has that int within it to prevent that int being added again. With the following code it adds the position int multiple times.
if(correctQuestions.size() == 0){
correctQuestions.add(position);
}else if(correctQuestions.size() > 0){
if(!Arrays.asList(correctQuestions).contains(position)){
correctQuestions.add(position);
}
}
If position = 0; then each run of this code will continually add position to my arraylist regardless if 0 is in it or not. For instance running this code 3 times will result in my arraylist outputting [0,0,0] when it should only allow it to add 0 once.
you must write like this:
if(correctQuestions.size() == 0){
correctQuestions.add(position);
}else if(correctQuestions.size() > 0){
if(!correctQuestions.contains(position)){
correctQuestions.add(position);
}
}
try this :
if(correctQuestions.indexOf(position) < 0) {//this will return -1 if object not found in the arraylist
correctQuestions.add(position);
}
You will have maximum index of your Arraylist assume it as variable size.
So you can write conditional logic :
if(size!=0 && position < size ){
correctQuestions.add(position);
}
The method arrayList.size() returns the number of items in the list - so if the index is greater than or equal to the size(), it doesn't exist.
if(correctQuestions.size() > position){
correctQuestions.add(position);
}
if you want to check if position already present in arraylist if `correctQuestions.get( index );` in try block shows that if no key present it will throw in catch
try {
correctQuestions.get( index );
} catch ( IndexOutOfBoundsException e ) {
correctQuestions.add( index, new Object() );
}
I have 5 empty TextViews where I add the names. After adding a name, it is stored in a database. The database consist on 2 columns, the item ID and the item NAME. This is an example of what I'm doing:
- Mark1 //ID=1, NAME= Mark1
- Mark2 //ID=2, NAME= Mark2
- Mark3 //ID=3, NAME= Mark3
- Empty
- Empty
I add and edit perfectly the textViews, but I'm facing a problem when deleting. This has something to do with the way I'm getting the values from the database, I'll explain:
Every time the app starts, or I edit, add or delete one element, what I do is get the items from the database, get them into a Map, and copy them into the textviews (whose at a first time are invisible) making visible just the ones that have a name setted.
This is the code I use to do that:
public void getTravelers() {
/*Create map where store items*/
Map<Integer, String> nameList = new HashMap<Integer, String>();
/*Lon in providers query() method to get database's items and save them into the map*/
Cursor c = getContentResolver().query(TravelersProvider.CONTENT_URI, PROJECTION, null, null, null);
if (c.moveToFirst()) {
do {
nameList.put(Integer.parseInt(c.getString(c.getColumnIndex(Travelers._ID))), c.getString(c.getColumnIndex(Travelers.NAME)));
}while(c.moveToNext());
}
if (c != null && !c.isClosed()) {
c.close();
}
/*Check size*/
int size = nameList.size();
if (size >= 1) {
/*Save items in TextViews*/
//TODO: This is the code I should fix
for (int i = 0; i <= size; i++) {
if (i==1) {
traveler1.setText(nameList.get(i).toString());
traveler1.setVisibility(View.VISIBLE);
}
if (i==2) {
traveler2.setText(nameList.get(i).toString());
traveler2.setVisibility(View.VISIBLE);
}
if (i==3) {
traveler3.setText(nameList.get(i).toString());
traveler3.setVisibility(View.VISIBLE);
}
if (i==4) {
traveler4.setText(nameList.get(i).toString());
traveler4.setVisibility(View.VISIBLE);
}
if (i==5) {
traveler5.setText(nameList.get(i).toString());
traveler5.setVisibility(View.VISIBLE);
}
}
}
}
The problem comes in the for loop. Let's supposse that from the items named above, I want to delete Mark2 with ID=2, so then the size of the new Map would be 2, and it would enter to (i == 1) and (i == 2). But when entering to this last one, it would do traveler2.setText(nameList.get(2).toString()); and as seen, there is no element existing with the ID=2 because that is the one that I've deleted and it throws a NPE.
So my question is, what would be the right way to do this without facing this problem?
You should go for switch case other than for loop. Than code will not be in loop.
Finally I get what I need just changing the Key value of the Map that was the same as the ID of the database:
if (c.moveToFirst()) {
int key = 0;
do {
key++;
nameList.put(key, c.getString(c.getColumnIndex(Travelers.NAME)));
}while(c.moveToNext());
}
if (c != null && !c.isClosed()) {
c.close();
}
Basically this way I don't need to change nothing more as now the key value of the Map will match with the Textview position
SparseBooleanArray selectedItem=catogoryList.getCheckedItemPositions();
for(int i=0;i<selectedItem.size();i++)
{
System.out.println("Array val:"+selectedItem.valueAt(i));
if(selectedItem.valueAt(i))
{
if(temp.contains(list.get(position)))
{}
else
temp.add(list.get(position));
}
else
{
temp.remove(list.get(position));
}
}
I use listview with multiple_choice (listview with checkbox) when i get items in listview that is checked with out scrolling i received correct values if i scroll the listview i don't get correct values. i found that selectedItem.valueAt(i) i get false when i check the items while scrolling. i don't know why false value is returned instead of true value.
How do you obtain your values? This process should be like this:
SparseBooleanArray checked = getCheckedItemPositions();
List<String> checkedResult = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
if (checked.valueAt(i)) {
checkedResult.add(getAdapter().getItem(checked.keyAt(i)).toString());
}
}
return checkedResult;
When i open Activity for EditRecord i want to select spinner row for adequate value in edited record
I find code like below, but its ok for few records in spinner, but when spinner.cursor contains many records i think its not right idea.
Is any other method to select spinner row for known rowid ?
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new SimpleCursorAdapter(...));
for (int i = 0; i < spinner.getCount(); i++) {
Cursor value = (Cursor) spinner.getItemAtPosition(i);
long id = value.getLong(value.getColumnIndex("_id");
if (id == rowid) {
spinner.setSelection(i);
}
}
Why are you getting a cursor and looking p the column index every time? Simply do:
for (int i = 0; i < spinner.getCount(); i++) {
long id = spinner.getItemIdAtPosition(i);
if (id == rowid) {
spinner.setSelection(i);
}
}
I have created a simple Spinner binding it to a SimpleCursorAdapter. I'm populating the SimpleCursorAdapter with a list of towns from a content provider.
When I go to save the users selection I'm planning on saving the row id that is being populated into my SimpleCursorAdapter.
I'm using the following code to get the ID.
townSpinner.getSelectedItemId();
What I can not figure out is how best to set the selection when I pull back up the saved item.
The following code works but it only sets selection by position number.
townSpinner.setSelection(2);
Should I just create a loop to determine the correct position value based on Id?
long cityId = Long.parseLong(cursor.getString(CityQuery.CITY_ID));
for (int i = 0; i < citySpinner.getCount(); i++) {
long itemIdAtPosition2 = citySpinner.getItemIdAtPosition(i);
if (itemIdAtPosition2 == cityId) {
citySpinner.setSelection(i);
break;
}
}
I think you've answered your own question there!
Just write your own setSelectionByItemId method using the code you posted
Example:
DB:
public Cursor getData() {
SQLiteDatabase db = getReadableDatabase();
String sql = "select ID _id, Name from MyTable order by Name ";
Cursor c = db.rawQuery(sql, null);
c.moveToFirst();
return c;
}
Activity:
Cursor myCursor = db.getData();
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
myCursor,
new String[] { "Name" }, new int[] { android.R.id.text1 }, 0);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter1);
for(int i = 0; i < adapter1.getCount(); i++)
{
if (adapter1.getItemId(i) == myID )
{
mySpinner.setSelection(i, false); //(false is optional)
break;
}
}
Android Spinner set Selected Item by Value
The spinner provides a way to set the selected valued based on the position using the setSelection(int position) method. Now to get the position based on a value you have to loop thru the spinner and get the position. Here is an example
mySpinner.setSelection(getIndex(mySpinner, myValue));
private int getIndex(Spinner spinner, String myString){
int index = 0;
for (int i=0;i<spinner.getCount();i++){
if (spinner.getItemAtPosition(i).equals(myString)){
index = i;
}
}
return index;
}
If you are using an ArrayList for your Spinner Adapter then you can use that to loop thru and get the index. Another way is is to loop thru the adapter entries
Spinner s = (Spinner) findViewById(R.id.spinner_id);
for(i=0; i < adapter.getCount(); i++) {
if(myString.trim().equals(adapter.getItem(i).toString())){
s.setSelection(i);
break;
}
}