logo
down
shadow

How can I get the row id of the a table in the sqlite database when an Item is clicked in the listview


How can I get the row id of the a table in the sqlite database when an Item is clicked in the listview

By : Dror Rash
Date : November 19 2020, 12:41 AM
To fix the issue you can do First of all you have to write a query and retrieve id in your database.You must return the id in a cursor.
Refer the below code snippet:
code :
public Cursor fetchAllNames() {

        Cursor mCursor = app.myDbHelper.MyDB().query("cardlist", new String[] {"cardid as _id","cardname","carddesc"},
                null, null, null, null, null);
        try{

            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }catch(Exception e)
        {
            return mCursor;
        }

    }
Cursor cursor = myDB.fetchAllNames();
 lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
        {
      if(cursor!=null){
                    System.out.println("position===>"+position);
                    if(cursor.moveToFirst()){
                        cursor.moveToPosition(position);   
                        String cardid =    cursor.getString(cursor.getColumnIndex("_id"));

                    }
                }
        }
    });


Share : facebook icon twitter icon
deleting an item from listview (using sqlite) results in only removing it from listview.Can't delete it from database

deleting an item from listview (using sqlite) results in only removing it from listview.Can't delete it from database


By : Fernanda
Date : March 29 2020, 07:55 AM
I hope this helps . the problem you have is the call of your method delete, it should be like this :
code :
public long deleteItem(myItems itemToDelete) {
    try {
             return sqlDatabase.delete(DATABASE_TABLE,COLUMN_ID + " = ?",new String[]{itemToDelete.getID()});
            }
            catch (Exception e)
            {
                Log.e("DB ERROR", e.toString());
                e.printStackTrace();
                return -1;
            }
}
myItems selectedItem = adapter.getItem(position);
long rows = sqlHandler.deleteItem(selectedItem);
if(rows>0) {
    //get new items from database 
    adapter.setItems(getItemsFromDatabase());
    adapter.notifyDataSetChanged();
}
How to Get the SQLite column _id value for the list item clicked in listview

How to Get the SQLite column _id value for the list item clicked in listview


By : Ashton Wardle
Date : March 29 2020, 07:55 AM
it should still fix some issue I have an activity which show a list. The list is populated from data taken from a sqlite database. I'm using a simpleCursorAdapter. When an item in the list is clicked I want go to another activity page to show more details about the list item clicked. For that I need the _id(primary key) of the clicked list item which is given in the database, so that I can query the database with that id and show more details in the next activity that loads when user clicks the list item.By using onItemClickListener, How to do this??? I appreciate any suggestions. in advance! , You can pass the Id with an intent to the next activity:
code :
          Intent intent = new Intent(this, your_next_activity.class);
            intent.putExtra("ID", id);
            startActivity(intent);
         Intent intent = getIntent();
         int id= intent.getIntExtra("ID", 0);
        Cursor c = yourDBHelper.getAllDetailsMethod(); //method inside Your db-helper that gives You the cursor
        int rows = c.getCount();
        c.moveToFirst();

        for(int i=0;i<rows;i++){

            int db_id = c.getInt(0) //this belongs to the place where Your id is inside the db

           if(db_id==id){
               //here You can get all details you need

             break;//stop quering
           }else{

              c.moveToNext();
            }

        }
android - Displaying the item clicked to a Listview populated by SQLite

android - Displaying the item clicked to a Listview populated by SQLite


By : Hao Dong
Date : March 29 2020, 07:55 AM
I hope this helps you . AFAIK inside RecyclerView's onBindViewHolder(ViewHolder holder, int position), you may want to attach holder.setTag(model.getId) instead of holder.setTag(position) that we do. Take an OnClickListener outside by holder.setOnClickListener(exampleListener) and get that tag inside View.OnclickListener by using int positionId = (int) holder.getTag(). This way you may utilize RecyclerView too.
How to create a dialog which contains SQLite information when clicked on the corresponding listview item?

How to create a dialog which contains SQLite information when clicked on the corresponding listview item?


By : Михаил Потапов
Date : March 29 2020, 07:55 AM
Any of those help If you follow what @ Nongthonbam Tonthoi said, you will see that it is very simple to implement but I will show you some example code below.
You can create a method for custom dialog which takes two parameter - note and date values like below
code :
public void displayNoteDate(String noteContent, String dateValue) {
    final Dialog builder = new Dialog(this);
    builder.setContentView(R.layout.custom_dialog);

    builder.setTitle("Display note date");

    TextView note = (TextView)builder.findViewById(R.id.note);
    TextView date = (TextView)builder.findViewById(R.id.date);

    //add the database note and date
    note.setText(noteContent);
    date.setText(dateValue);

    Button closeButton = (Button)builder.findViewById(R.id.close);
    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            builder.dismiss();
        }
    });
    builder.show();
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical">

<TextView
    android:id="@+id/note"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""/>

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="12dp"
    android:text=""/>

<Button
    android:id="@+id/close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:layout_gravity="center"
    android:text="close"/>

</LinearLayout>
How do I read certain data from sqlite database and show it in a dialog when a listview item is clicked?

How do I read certain data from sqlite database and show it in a dialog when a listview item is clicked?


By : Musa Kazim AY
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Here's a solution (assuming that the displayNoteDate works, this was tested with a Toast), that uses a CursorAdapter rather than a ListAdapter.
The benefit is two-fold, no need to create an intermediate array and you have ALL the data available within the onItemClickListener.
code :
public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");
        db.execSQL("create table " + TABLE_NAME2 + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");

    }
    mDatabaseHelper.insertexpenseData("100.00","21/03/1904","Wow this old?");
    mDatabaseHelper.insertexpenseData("56.78","31/12/2010","New Celebrations");
    mDatabaseHelper.insertexpenseData("250.00","23/08/2017","Birthday Present");
    .......
    expensedata = mDatabaseHelper.getexpenseData();
    SimpleCursorAdapter sca = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1,
            expensedata,
            new String[]{DatabaseHelper.EXPENSE_AMOUNT},
            new int[]{android.R.id.text1}, 0);
    listdata.setAdapter(sca);
    listdata.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            int csrpos = expensedata.getPosition();
            expensedata.moveToPosition(i);
            displayNoteDate(
                    expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_NOTES)),
                    expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_DATE)));
            expensedata.moveToPosition(csrpos);
        }
    });


}

//!!!NOTE!!! Used for testing so don't use this method.
private void displayNoteDate(String notes, String date) {
    Toast.makeText(this,"Notes=" + notes + " Incurred on " + date,Toast.LENGTH_LONG).show();
}

@Override
public void onDestroy() {
    super.onDestroy();
    expensedata.close();
}
Related Posts Related Posts :
  • Passing variables from one activity to another
  • Create Multicolored Pie Chart in Android Studio
  • how to kill progress dialog in android
  • Show Error on the tip of the Edit Text Android
  • How do I use getWindowManager() by another java file
  • fetching images from gallery on android phones with internal storage
  • How to discover devices like Google TV and Nexus-Q using MediaRoute API on Android?
  • "Unfortunately your app stopped working"- Facebook Integration for Android
  • How to animate button in android?
  • Resource Not Found Exception in class without context or activity
  • Launching Android Netflix App And Passing Video Id
  • Making a Phone Call with a Number Extension
  • NullPointerException at BoringLayout.isBoring is anything but boring
  • java.lang.RuntimeException: Unable to get provider
  • Testing custom Views with Robolectric
  • tools:listheader doesn't work although tools:listitem does
  • oppo, vivo app kill notification not coming in android fcm
  • OnClick Method could not function correctly with if else statement
  • Best method to download image from url in Android
  • How can I open Drawer Layout moving all the view and not just slide over?
  • how to show profile pictures of friends in a list view?
  • Creating TimePickerDialog with custom style in Android
  • Android 2.3.6 support library v7 option menu is missing
  • need of vblank in Display subssytem
  • Android MP4 stream - Video cannot be played
  • FastAdapter: Undo button does not show up after swipe
  • What are reasons to pass getActivity as the LifecycleOwner to the LiveData's observe method in a fragment?
  • Implement Face Recognition in Android
  • Titanium studio cannot run "titanium" command in terminal
  • How to execute command line ffmpeg commands programatically in android?
  • Make Android List Items clickable with each item pointing to a different link
  • listview runs successfully, but nothing is display in emulator
  • How to zoom in a bitmap with high resolution?
  • Passing listView data through intent
  • Google map route direction show by arrows in android v2
  • how to start "uiautomator test run" from android code?
  • Proguard in Android Library
  • Alarm Clock from GIT - Gives error - Android
  • Mock injection using Mockito - Android
  • Get Bitmap from universal image loader
  • Unable to include custom font in android
  • Basics on using logcat in Android development
  • Android: Clear cache programmatically
  • Android: pass object without serialization
  • Can not scroll and click item on gridview inside scrollview
  • Android device keeps disconnecting from adb / eclipse
  • Volley cannot create cache directory. Could not clean up file
  • ShareActionProvider icon in actionbar
  • How to open a custom attachment using an Android app?
  • Unable to add column dynamically into the sqlite table
  • android persistence activity which never stop
  • The Google Play services resources were not found. Check your project configuration to ensure that the resources are inc
  • How to select files from google drive in android?
  • Run a AsyncTask every 10 seconds until cancelled
  • Blur on touch. Android application
  • NDK Android - Can't build ARMv5 and V7 at the same time
  • Android development with Eclipse clean and build
  • How to set Different Key Background for every Key in Android Custom Keyboard
  • Face recognition using OpenCV in android?
  • Tumblr API - Jumblr for Android [OAuthConnectionException]
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org