I have doing small application in android.During this application, i have suffer this problem:
I search the keyword using "oh" then i listing in to listview using this code.
nameList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
try {
Intent intent = new Intent(Search.this,
SearchDetails.class);
/*
* Cursor cursor = (Cursor) adapter.getItem(position);
* intent.putExtra("JOK_ID",
* cursor.getInt(cursor.getColumnIndex("_id")));
*/
intent.putExtra("id", id);
intent.putExtra("position", position);
Log.d(TAG, " I.." + id);
Log.d(TAG, " P.." + position);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
for example like this
Now, if I click oham then display just like this:
Finally, I want to , If i click previous button then display john and Next to display krackohm. And count total number of searched names.
I have try this code for next button:
Cursor cursor = db.rawQuery("SELECT _id, name FROM std WHERE _id = ?",
new String[]{""+Id});
if (cursor.getCount() == 1)
{
cursor.moveToFirst();
...body
}
Edit:
This is first time when any list item click, on create
private void dislpayFirstName() { // this is display first time click list item
writeDatabase();
cursor = db.rawQuery("SELECT _id, name FROM std WHERE._id = ?",
new String[]{""+Id});
if (cursor.getCount()> 0)
{
cursor.moveToFirst();
nameDetails();
}
}
This is the code for Next Button click:
position++;
writeDatabase();
cursor = db.rawQuery("SELECT _id,name FROM std", null);
if (cursor.getCount()> 0)
{
cursor.moveToPosition(position);
//cursor.moveToNext();
nameDetails();
}
In next button: If cursor.moveToPosition(position); then next name display matching position and table id. Means postion 2 then display 2 number name from table.
In next button: If cursor.moveToNext(); then next name display beginning of table.Means 0 id name from table.
You have to do it like this:
Cursor cursor = db.rawQuery("SELECT _id, name FROM std") // get all rows
Than, to display you first entry:
cursor.moveToFirst();
displayData(); // Method to display the data from the cursor on screen
Clicking you "next" Button:
if (cursor.moveToNext()){ // moveToNext returns true if there is a next row
displayData();}
And equally: with your "previous"Button use cursor.moveToPrevious()
Put cursor.moveToFirst(); at first. I mean put it like this..
cursor.moveToFirst();
if(your condition)
{
}
Instead of using id as extra with intent,you should use the position of the item in list.
The position will be similar to the position of the same record in your cursor so you just need to move your cursor to that position and fetch the detail you need from the same.Then you can display it on activity,as you have shown on your second activity view.
so in your SearchDetails.class,you need to do:
...
int position=getIntent.getIntExtra("position",999); // 999 is default value
Cursor cursor = db.rawQuery(<query which you fired for populating search results in previous activity>);
if (cursor.getCount()> 0 && position!=999){
cursor.moveToPosition(position);
...body // fetch details and display it.
}
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
position--;
//display details code
Cursor cursor = db.rawQuery(<query which you fired for populating search results in previous activity>);
if (cursor.getCount()> 0 && position!=-1){
cursor.moveToPosition(position);
...body // fetch details and display it.
}
}
}
nextButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
position++;
//display details code
Cursor cursor = db.rawQuery(<query which you fired for populating search results in previous activity>);
if (cursor.getCount()> 0 && position!=cursor.getCount()){
cursor.moveToPosition(position)
...body // fetch details and display it.
}
}
}
...
EDIT :
Revised code: ( repeated code removed)
...
int position=getIntent.getIntExtra("position",999); // 999 is default value
Cursor cursor = db.rawQuery(<query which you fired for populating search results in previous activity>);
if (cursor.getCount()> 0 && position!=999){
cursor.moveToPosition(position);
...body // fetch details and display it.
}
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
position--;
if (position!=-1){
cursor.moveToPosition(position);
...body // fetch details and display it.
}
}
nextButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
position++;
if (position!=cursor.getCount()){
cursor.moveToPosition(position)
...body // fetch details and display it.
}
}
}
...
Related
I always cant delete first two sometimes three records. They are on Listview, when you press element you will see delete button on another layout. On Log im getting correct index for every element.
So here is my code:
Main Activity:
viewOfT.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(MainActivity.this, popupWindow.class);
i.putExtra("index",id);
startActivity(i);
}
});
}
public void populateListView() {
Cursor data = db.getData();
ArrayList<String> listData = new ArrayList<>();
while(data.moveToNext()){
k.setId(data.getInt(0));
k.setTask(data.getString(1));
listData.add("- " + k.getTask());
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
listData);
viewOfT.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
viewOfT.invalidateViews();
Delete button in other activity:
del.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Bundle bundle=getIntent().getExtras();
long value=bundle.getLong("index");
db.deleteRecord(value);
finish(); }
});
And SQLHelper:
public void deleteRecord(long id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_ID + "=" + id, null);
close();
}
1) Delete populateListView method
2) Add the following as class variables:-
SimpleCursorAdpater sca;
Cursor data;
3) in the onCreate method add :-
data = getData();
sca = new SimpleCursorAdapter(
this, // Context same as for array adapter
android.R.layout.simple_list_item_1, // layout to be used
data, // <<<<<<<< the cursor to be used for the list
new String[]{"columnname_to_be_displayed"}, // <<<<<<<< cursor column to display name
new int[android.R.id.text1], // the view into which the cursor data will be placed
0 // a flag 0 is fine
);
viewOfT.setAdapter(arrayAdapter);
4) Add a new method to override the 'onResume' method (or alter it if already overridden) :-
#Override
protected void onResume() {
super.onResume();
data = getData();
sca.swapCursor(); // can you sca.notifyDatasetChanged()
}
As you are calling another activity to delete the task, onResume will be called when returning from the other activity so the data is again retrieved from the database (deleted row will not exist) and the adpater is told to refresh the data.
You should ideally also override the onDestroy() method to close the cursor (data.close();)
Important Consideration
A cursor column named _id must exist for CursorAdapters (that's how the SimpleCursorAdapter knows what pass to the onItemClickListener).
if KEY_ID does not equate to _id; you either need to change KEY_ID to _id or amend the getData() method to include the _id column (which should be the value of the row's identifier) e.g. assuming a very basic query:
public Cursor getData() {
return db.query(TABLE_NAME,"rowid AS _id, *",null,null,null,null,null);
} // Note! will add an extra column so beware if using column offsets
or perhaps :-
public Cursor getData() {
return db.query(TABLE_NAME,KEY_ID + " AS _id, *",null,null,null,null,null);
} // Note! will add an extra column so beware if using column offsets
A Note on Column offsets
In your code you have :-
k.setId(data.getInt(0));
k.setTask(data.getString(1));
0 and 1 are column offsets (and could change e.g. the two alternative getData() methods). As such it's generally better to take advantage of the Cursor method getColumnIndex(columnname) e.g. the above could be :-
k.setId(data.getInt(data.getColumnIndex(KEY_ID));
k.setTask(data.getString(data.getColumnIndex("TASK_COLUMN")));
Note! not that you will need to create an Array as the SimpleCursorAdpater takes the cursor as the source. (KEY_ID would likely have to be prefixed with the DatabaseHelper Class).
Currently, in anotherclass, I need to delete 1 item when I upload success a file call deleteCallWhenUploadSuccess.
In this class, I using fileName to determined item need to delete.
But it doesn't delete item in ListView from Layout activity_call_history.xml
In DAO class I delete with code:
public void deleteCallWhenUploadSuccess(String fileNameWhis)
{
db = callDatabaseHelper.getWritableDatabase();
String where = CallDatabaseHelper.FILE_NAME + "='" + fileNameWhis + "'";
db.delete(CallDatabaseHelper.TABLE_NAME, where, null);
}
And in other class I using it:
DAO.deleteCallWhenUploadSuccess(filename);
I write code to remove an item on ListView in event onActionItemClicked.
On the DAO class I start to delete an item with rowId:
public void deleteCallV2(int rowId) {
db = callDatabaseHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from "+CallDatabaseHelper.TABLE_NAME+" where rowId = "+String.valueOf(rowId),null);
while(cursor.moveToNext()){
this.rowId = CallDatabaseHelper.ROW_ID +"="+cursor.getString(cursor.getColumnIndex(CallDatabaseHelper.ROW_ID));
}
db.delete(CallDatabaseHelper.TABLE_NAME, this.rowId, null);
}
Code I used to delete item in ListView of Layout historyAdapter write code in HistoryFragment.java, I was comment in code to easy read:
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.deleteAction:
// Calls getSelectedIds method from ListViewAdapter Class
selected = historyAdapter
.getSelectedIds();
// Captures all selected ids with a loop
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
Call selecteditem = historyAdapter
.getItem(selected.keyAt(i));
// Remove selected items following the ids
historyAdapter.remove(selecteditem);
}
}
getFragmentManager().beginTransaction().replace(R.id.container,new HistoryFragment()).commit();
// Close CAB
mode.finish();
return true;
case R.id.allAction:
historyAdapter.toggleAll(listView);
Toast.makeText(getActivity(), "Đã chọn tất cả", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
But it only work when I select item with long press and select item or select all to delete. It using ID to delete item.
Since I didn't got much but according to the heading of the question which says to remove an item in list I can suggest you to use notifyDataSetChanged() whenever you are removing a view so that your listview could update itself whenever its changed
A little code which demonstrate this task is
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
I need to delete an item permanently from ListView and then from database. I have a DatabaseHandler.java class, which has the delete function as:
// Deleting single contact, in DatabaseHandler.java class
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
Then I have a FriendList.java class, when the user's friends are displayed as an item in ListView. When I long press on an item, then I get the option of "Delete" and "Cancel" in Dialog Box. Now, when I click on delete, the item is deleted from the ListView, but, not from the database. How can I delete it from database as well?
The code for getting the option of "Delete" and "Cancel"
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
Intent i = new Intent(FriendList.this, Delete_Confirm.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
//I am sending position of listitem in putExtra below//
i.putExtra("position", position);
startActivityForResult(i,CONFIRM);
item2 = (String) arg0.getItemAtPosition(position);
//Toast.makeText(FriendList.this, "Clicked"+item2, Toast.LENGTH_SHORT).show();
int l = item2.length();
c=0;
for(int j=0; j<=l; j++){
if(item2.charAt(j) != '9' || item2.charAt(j+1) != '1'){
c++;
}
else {
//Do nothing
break;
}
num = item2.substring(c, l);
}
Toast.makeText(FriendList.this, "Clicked: "+num, Toast.LENGTH_SHORT).show();
return true;
}
});
The corresponding code for onActivityResult is as follows:
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (CONFIRM) :
if(resultCode==RESULT_OK){
int posi = data.getIntExtra("position",0);
Log.d("msg","position is " + posi);
Log.d("msg","Do we reach here?");
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
//db.deleteContact(posi);
list.remove(posi);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
break;
}}
Please suggest how can I delete it from database as well. Any help would be highly appreciated.
EDIT:
On uncommenting db.deleteContact(posi), I get the following error:
The method deleteContact(Contact) in the type DatabaseHandler is not applicable for the arguments (int)
Note that the function deleteContact has contact variable of the type Contact.
Its a compilation error.
You need to pass a Contact object to the method, not an integer.
When you delete.... Try Deleting first from database then from ListView..
example:
db.deleteContact(list.get(posi)); // this will get string
list.remove(posi);
DatabaseHandler class.......
public void deleteContact(String name){
Log.d("Name:",""+ name);
db.delete(TABLE_CONTACTS, KEY_NAME + " = ?", new String[] { name });
}
You can delete from database first then from ListView. And suggest Iterator to remove list element.
I think the problem is that position is not the corresponding id in the database. A possible solution would be to add a tag with the database id of the contact you have in this listitem. And when you remove it you get the tag with the id and delete the item from the database.
Edit (added clarification):
I would add in your listviewadapter something like:
yourcontactview.setTag(contact.getId());
in which you add to your view a tag with the database id of the corresponding contact.
Then where you delete the contact I would get the contact you want to delete with something like this:
Contact deletecontact = db.getContact(view.getTag());
db.deleteContact(deletecontact);
Of course you could change your
deleteContact(Contact contact) to a method in which you give the id instead of the contactobject.
This should hopefully work.
I am creating an application, which creates checkboxes dynamically from the number of entries in the database.
The code runs fine. and creates number of checkboxes. But I want to add onClick Action Listener to the CheckBoxes, dynamically. How to do this.
I am posting the code here:
SQLiteDatabase db = this.getReadableDatabase();
String countQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int maxCount = cursor.getCount();
CheckBox[] check = new CheckBox[maxCount];
cursor.moveToFirst();
int checkboxid = 100;
int alarm_id;
for(int i=0;i<maxCount;i++)
{
check[i]= new CheckBox(this);
setCheckBoxId(i+maxCount);
}
Now how to add actionlistener to these dynamically created CheckBoxes.
check[i]= new CheckBox(this);
check[i].setOnClickListener(this);
You will want to check against the checkbox id or set a tag for the text boz when creating it.
check[i].setTag(someIdentifier);
then use the method
#Override
public void onClick(View v) {
if (v.getTag.equals(someIdentifier)){
//do stuff here
}
}
below check[i]= new CheckBox(this); add this, and your CheckBoxes will have a all clicklistener
check[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your code to be executed on click :)
}
});
I am trying to add image button to my custom SimpleCursorAdapter for my ListView in my project but I have got one problem with repeating and totally random value of one field.
This is a code of it:
public class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
final Context t = context;
final Cursor c = cursor;
ImageButton delimageButton = (ImageButton)view.findViewById(R.id.deletebutton);
delimageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(t,
"Delete ID: " + c.getInt(c.getColumnIndex(MyDBAdapter.KEY_ID)), Toast.LENGTH_SHORT).show();
}
});
if(cursor.getLong(cursor.getColumnIndex(MyDBAdapter.KEY_OWNID))>0)
{
TextView own = (TextView)view.findViewById(R.id.ownInfo);
own.setText("OWN");
}
else
{
TextView own = (TextView)view.findViewById(R.id.ownInfo);
own.setText("");
}
}
}
Now, when I press delimageButton what I get is some random ID of one record (row) of ListView that is in current view (I can see it, but it is not the correct id) e.g. if you can see like 5 rows on screen and you press on one of buttons you will get id of other row (one of those 5) but not this one which you pressed (in most of the cases). I remember that there was some trick with this own TextView, but I don't see how it can be putted in here.
So, can you please advice me how can I make it to show correct ID?
I'll be glad for help.
EDIT
There is a whole code responsible for setting a ListView together with calling MyCursorAdapter:
private void refreshList() {
mySQLiteAdapter = new MyDBAdapter(this);
mySQLiteAdapter.open();
String[] columns = { MyDBAdapter.KEY_TITLE, MyDBAdapter.KEY_GENRE,
MyDBAdapter.KEY_OWNID, MyDBAdapter.KEY_ID };
Cursor contentRead = mySQLiteAdapter.getAllEntries(false, columns,
null, null, null, null, MyDBAdapter.KEY_TITLE, null);
startManagingCursor(contentRead);
Log.d(TAG, Integer.toString(contentRead.getCount()));
MyCursorAdapter adapterCursor = new MyCursorAdapter(this,
R.layout.my_row, contentRead, columns, new int[] {
R.id.rowTitle, R.id.detail });
this.setListAdapter(adapterCursor);
mySQLiteAdapter.close();
}
To clarify, the activity is ListActivity.
Your OnClickListener is getting the id from the cursor when it is clicked, not when it is constructed. Meanwhile, the listview is changing the cursor position as you scroll around.
I think if you look carefully, you'll find the Toast is displaying the id of the last item that was loaded into view rather than the item that contains the button you clicked.
You can solve this by getting the id when you construct the click listener, like this:
delimageButton.setOnClickListener(new OnClickListener() {
private int id = c.getInt(c.getColumnIndex(MyDBAdapter.KEY_ID));
#Override
public void onClick(View arg0) {
Toast.makeText(t,
"Delete ID: " + id, Toast.LENGTH_SHORT).show();
}
});