Android spinner, Why I can't select item on first position? - android

I created spinner in my app and I'm filling it with data from a database. It works ok until I want to select item in first position, when I do that spinner closes and nothing is hapennig. But if I choose item on any other positions it works just fine and i don't have any idea why ?
my code:
public String returnString(AdapterView<?> parent,int position,long ID){
return parent.getSelectedItem().toString();
}
void showDrugStores()
{
final DBAdapter db = new DBAdapter(this);
db.open();
final Cursor drugCursor;
Spinner drugStoreSpinner = (Spinner) findViewById(R.id.drugStoreSpinner);
drugCursor = db.getDrugStores();
drugCursor.moveToFirst();
startManagingCursor(drugCursor);
String[] from = new String[]{db.KEY_NAZWAAPTEKI};
int[] to = new int[]{R.id.tvDBViewRow};
SimpleCursorAdapter drugStoreAdapter = new SimpleCursorAdapter(this,
R.layout.closed_spinner, drugCursor, from, to);
drugStoreAdapter.setDropDownViewResource(R.layout.db_view_row);
drugStoreSpinner.setAdapter(drugStoreAdapter);
drugStoreSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String selection = returnString(parent, pos, id);
db.open();
db.getDruSto(selection);
TextView show_selected =(TextView) findViewById(R.id.show);
show_selected.setText(drugCursor.getString(1));
db.close();
});
}

It is probably because the first item is ALREADY selected - if you want to add an option to represent that nothing is selected, you should add that to the head of your list.

Related

How get item from a listView Android?

How can I get a item from a listView 2 items , I am using to fill the listView a database of queries but there do not know how to do that to get the name of the item ?
from = new String[]{manager.CN_NAME,manager.CN_CREDITS};
to = new int[]{android.R.id.text1,android.R.id.text2};
manager = new DataBaseManager(this);
listaMateria = (ListView) findViewById(R.id.listView);
cursor = manager.cargarCursorMateria();
adapter = new SimpleCursorAdapter(this,android.R.layout.two_line_list_item,cursor,from,to,0);
listaMateria.setAdapter(adapter);
listaMateria.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String item = ((TextView)view).getText().toString();
String a = Integer.toString(to[position]);
Toast.makeText(getBaseContext(), a, Toast.LENGTH_LONG).show();
}
});
You could also get the cursor for that row.
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Cursor cursor = (Cursor)adapter.getItem(position);
String value = cursor.getString(cursor.getColumnIndex("column_name"))
}

Get item ID from populated ListView

I need help retrieve my ListView items ID, to add delete functionality.
My ListView is populated with this code:
ListView BookList = (ListView)findViewById(R.id.listView1);
DbAdapter db = new DbAdapter(getApplicationContext());
db.open();
Cursor cursor = db.fetchAllBooks();
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.book, cursor, new String[]{DbAdapter.KEY_TITLE,DbAdapter.KEY_AUTHOR},new int[]{R.id.booktitle,R.id.bookauthor});
BookList.setAdapter(adapter);
if(cursor.moveToFirst()) {
while (cursor.moveToNext());
}
db.close();
My delete code is set as this, in the DbAdapter class:
//delete a book
public boolean deleteBook(long bookID) {
return database.delete(DATABASE_TABLE, KEY_BOOKID + "=" + bookID, null) > 0;
}
Any help?
Full sources: pastebin.com/kKgePkPM
You have to add a listener to your listView. The best way is set
bookList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// DO YOUR STUFF HERE
}
});
There you will get the view and the position clicked. With the view or the position you could obtain the value desired to your "deletebook" method.
If I understand you correctly, you can add itemclicklistener to your listview, then use the row id to remove it from your database, if the bookid is the same as the row id of course.
BookList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (db.deleteBook(position)) { // Success!
// remove from listview data source here
adapter.notifyDataSetChanged();
}
}
});
try adding something like this in getView method:
Button btnDelete = (Button) convertedRowView.findViewById(R.id.btnDeleteCustom);
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int itemPos = 4; // This is position of item in ArrayList
deleteBook(getItemId(itemPos));
}
});

insert an item and sub item from list view

i have a problem with my list view, i display an item and subitem , i want to insert the two in database but it doesn't work !! the error is that i can't cast it to a text view
please someone help me.
This is the error that i get :
java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView
Here the code of List View :
String[] databaseColumnNames = new String[] { DBAdapter.col_N_Ordre,DBAdapter.col_Nom_prénom};
int[] toViewIDs = new int[] { android.R.id.text1,android.R.id.text2 };
SimpleCursorAdapter myCursordapter = new SimpleCursorAdapter(this,android.R.layout.simple_expandable_list_item_2 , cursor, databaseColumnNames, toViewIDs);
ListView list = (ListView) findViewById(android.R.id.list);
list.setAdapter(myCursordapter);
Here the code of insertion :
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> arg0, View arg1,
final int position, long arg3) {
Toast.makeText(getApplicationContext(), " " + position, Toast.LENGTH_LONG).show();
final String s = ((TextView)arg1).getText().toString();
db.insertest(s);
The query is :
public long insertest(String region) {
ContentValues initialValues = new ContentValues();
initialValues.put(col_Region,region);
//initialValues.put(col_Provence_prefecture );
return db.insert(MENAGE,null, initialValues);
}
Put following code in your listview item click listener
TextView v1 = (TextView)arg1.findViewById(android.R.id.text1);
String first = v1.getText().toString();
TextView v2 = (TextView)arg1.findViewById(android.R.id.text2);
String second = v2.getText().toString();
ContentValues cv = new ContentValues();
cv.put(DBAdapter.col_Region, first);
cv.put(DBAdapter._id, second);
myCursordapter.notifyDataSetChanged();
list.setAdapter(adapter);
DBAdapter sql_Adapter = new DBAdapter(context);
sql_Adapter.open();
db.insertest(cv);
sql_Adapter.close();

To get the name of the item which was clicked

I have written the code to get the data from database and to bind it to listview now I want to click on perticular item from the listview and to get the name of that item which was clicked.
Cursor cursor = dbHelper.fetchAllRecords();
String[] columns = new String[] {
RecordsDbAdapter.KEY_NAME,
RecordsDbAdapter.KEY_BIRTHDAY,
};
int[] to = new int[] {
R.id.name,
R.id.birthdate,
};
dataAdapter = new SimpleCursorAdapter(
this, R.layout.row,
cursor,
columns,
to);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),"cominggggg", Toast.LENGTH_SHORT).show();
}
});
}
Toast.makeText(getApplicationContext(), "Click ListItem Text "
+ ((TextView) view.findViewById(R.id.Txt))
.getText().toString(), Toast.LENGTH_LONG).show();
You can display the toast with selected item name
edit ur code like this:--
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
TextView textView = (TextView) view;
Toast.makeText(getApplicationContext(),textView.getText().toString(), Toast.LENGTH_SHORT).show();
}
});

In Android how to pass ListAdapter values to onItemClick?

I've got a functioning search interface on my app. Now I'm trying to make each search result list item clickable and display in a new activity. I've got them clickable, but I can't figure out how to pass the ListAdapter values (records) into the onItemClick method.
Here's my code so far:
private void showResults(String query) {
Cursor cursor = DBHelper.searchDB(query);
startManagingCursor(cursor);
String[] searchFrom = new String[] { DBAdapter.KEY_MAKE,
DBAdapter.KEY_YEAR, DBAdapter.KEY_MAKE,
DBAdapter.KEY_MODEL };
int[] displayHere = new int[] { R.id.rDateTV, R.id.rYearTV,
R.id.rMakeTV, R.id.rModelTV };
final SimpleCursorAdapter records = new SimpleCursorAdapter(this,
R.layout.record_2, cursor, searchFrom, displayHere);
setListAdapter(records);
DBHelper.close();
// --- Click on list item ---
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//--- here's the problem: how do I pass the ListAdapter values into onItemClick. the below doesn't like .getText()
String sYear = (view.findViewById(R.id.rYearTV)).getText().toString();
String sMake = (view.findViewById(R.id.rMakeTV)).getText().toString();
String sModel = (view.findViewById(R.id.rModelTV)).getText().toString();
// -- then pass these strings to an intent to launch a new activity
}
});
// --- END click on list item ----
}
Any ideas? I'm a rookie here, so if you could show me the code, that would be AWSOME!
In OnItemClickListener.onItemClick call parent.getItemAtPosition(position). That method returns cursor which is set to the given position. Then get your data from that cursor. Don't close cursor inside OnItemClickListener.onItemClick.
for example:
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor)parent.getItemAtPosition(position)
String sMake = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_MAKE));//get data from cursor
...
}
});

Categories

Resources