i have a list view filled with data coming from database, i want when i click on an item to
disappear, i used --list.remove(position)-- but i have an error add cast to list.
HERE the list view :
private void populateListViewFromDB() {
Cursor cursor = db.getAllRecords();
startManagingCursor(cursor);
String[] databaseColumnNames = new String[] { DBAdapter.col_Region };
int[] toViewIDs = new int[] { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter myCursordapter = new SimpleCursorAdapter(this,
android.R.layout.simple_expandable_list_item_1, cursor,
databaseColumnNames, toViewIDs, FLAG_REGISTER_CONTENT_OBSERVER);
final ListView list = (ListView) findViewById(android.R.id.list);
list.setAdapter(myCursordapter);
HERE my code when i want to delete an item :
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> arg0, View arg1,
final int position, long arg3) {
list.remove(position);
adapter.notifyDataSetChanged();}
There is no ListView.remove() method. You should remove correspondent item from the data model and then refresh your adapter.
In your OnItemClickListener listener get the bound data item:
Object itemObj = adapter.getItem(position);
Then, delete this item from your DB.
Then get new instance of your cursor and update your cursor adapter:
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD){
adapter.swapCursor(newCurosr);
} else {
adapter.changeCursor(newCursor);
}
Then, notify your list view about data changes:
adapter.notifyDataSetChanged();
Related
I use SimpleCursorAdapter build a list. And I want to start another activity when I click one of the rows. code as following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_list);
listView = (ListView) findViewById(R.id.user_list);
db.open();
fillData();
.......
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor selectedItem = (Cursor) listView.getAdapter().getItem(position);
Log.d("#####", selectedItem.getCount() +"");
// return total number of the cursor using to build the list.
selectedItem.moveToFirst();
.......
}
});
}
private void fillData() {
Cursor c = db.fetchAllSenders();
startManagingCursor(c);
String[] from = new String[] {PeerContract.KEY_NAME};
int[] to = new int[] {R.id.user_list_row_name};
adapter = new SimpleCursorAdapter(this, R.layout.user_list_row, c, from, to, 0);
listView.setAdapter(adapter);
}
But int the LogCat Log.d("#####", selectedItem.getCount() +""); always return the total number of rows in cursor used to build the list. For example, there are 4 rows in the list, after I click one row in the list, the LogCat will display #####:4. But it supposes to be 1 when I click just one item.
Thanks in advance.
It is coming 4 because there is only one cursor reference with the adapter. the method getAdapter().getItem(position) will return you the same cursor object but it will move the cursor to the position for which you requested getItem().
So whenever you call getItem(position), it will not give you a new cursor object, it will give you the same cursor object, moving it to the current position
Hello everyone and thanks in advance!
I have a listview that displays a list of name from the sqlite database. What I want to know is how can I make it so that when I click on an Item in the list, it takes me to a new activity which displays the rest of the columns in the Sqlite Table?
This is the method I used to bind the listview with the cursor:
public void displayListView() {
Cursor cursor = dataSource.findAll();
String[] columns = new String[]{
DBHelper.CHARACTERATTRIBUTES_NAME,
DBHelper.CHARACTERATTRIBUTES_NICKNAME,
DBHelper.CHARACTERATTRIBUTES_NOTES};
int[] to = new int[]{
R.id.name,
R.id.nickname,
R.id.notes};
SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this, R.layout.character_info, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
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
...
}
});
I am using database in my application,in that i have fetched data from database,but i dont know how to display it in the textview.
Here is my code:
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
db.open();
Cursor c = db.getAllTitles();
startManagingCursor(c);
String[] from = new String[] { db.KEY_INCOME };
int[] to = new int[] { R.id.textView1 };
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.report, c, from, to);
Here the notes have the database value.But don't know how to proceed how to display it in listview.
do like this
String[] titles= new String[] {c.TITLE };
int[] view = new int[] { R.id.textview };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, c, titles, view );
listview.setAdapter(notes);
You need to find your listview and set your adapter. (Assuming you are not using a ListActivity)
ListView list = (ListView) this.findViewById(R.id.yourlistview);
list.setAdapter(notes);
If you are using a ListActivity (or ListFragment), it's even simpler as the framework assumes you are using a certain id and you can just set the adapter like so:
setListAdapter(notes);
if you want to set set Tesxt on list item click you can do iy like
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = (Cursor) arg0.getAdapter().getItem(arg2);
tv.setTextt(c.getString(1));
}
This is my ListView structure and element :
// Get a cursor with all phones
Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
startManagingCursor(c);
/** ----Display the Contacts on the device----- */
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c,
new String[] { Phones.NAME, Phones.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });
setListAdapter(adapter);
and my implementation of the setOnItemClickListener is as follows:
/** ----Defining the ItemOnClickListener for the displayed List---- */
final ListView contactlistview = getListView();
contactlistview.setTextFilterEnabled(true);
contactlistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(TAG, String.valueOf(position));
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
contactlistview.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
Log.d(TAG, contactlistview.getItemAtPosition(position).toString());
}
});
and finally got the ListView as :
and when i clicked on any of the item i get the Toast Displaying the Following:
but i want my toast to display the contact name of the selected item
can u pls help me with the code :)
sorry for the inconvenience :)
THANKS :) :)
You are printing the value of the object here.
Now try like this:
Cursor c = ((Cursor) parent.getAdapter().getItem(position));
Toast.makeText(getApplicationContext(),
c.getString(Phone.NAME),Toast.LENGTH_SHORT).show();