2

I am new to android. I am developing an application in which there is a list view of students with edit and delete buttons. Like


 STUDENT LIST

[ABC]              [edit]              [delete]

[DEF]              [edit]              [delete]


With this code im able to list student details in a <Textview>

public class DataBaseDemoActivity extends ListActivity {
/** Called when the activity is first created. */
  SQLiteDatabase db;
  Button btnInsert;
  ArrayAdapter<String> students;

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  try{
  db=openOrCreateDatabase("StudentDB",SQLiteDatabase.CREATE_IF_NECESSARY,null);

  Cursor c=db.rawQuery("SELECT * FROM temp",null);
  String[] students = new String[c.getCount()];

  if (c.moveToFirst())
  {                       
      for (int i = 0; i < c.getCount(); i++)
      {
          students[i] = c.getString(0).toString()+" "+c.getString(1).toString();
          c.moveToNext();
      }           
  }
  c.close();

  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, students));

  ListView lv = getListView();
  lv.setTextFilterEnabled(true);

  lv.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(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
    }
  });


  }catch(SQLException e)
  {
  }
}

}

I need to store the id of the record with in the list view so that when i click on the edit or delete button, i'll have to find out the id and make changes on the DB. How can i set values to two fields say <TextView>[show details] and <EditText>[visibility: insisible - to save id of the record]. I am able to get the details to the <TextView> using the above code. Any suggestion will be appreciated.

UPDATE :

Layout i am using

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"    
    >
    <EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:inputType="text"
        android:onClick="showInfo"
        />
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="230dip"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:id="@+id/studentInfo" >
    </TextView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/action"
        android:onClick="showInfo"
         />

</LinearLayout>
2
  • "id of the record " means database record id with text? M I right? Commented Apr 21, 2012 at 5:34
  • @user370305 : i mean id of the database record. Commented Apr 21, 2012 at 5:37

2 Answers 2

1

You can get the Id within cursor data fetch loop, use any HashMap<id,String> collection for it and store id along with data (text).

As per your requirement, I suggest you to use SimpleCursorAdapter instead of ArrayAdapter with ListView.

So you can manage easily your database records with your list and also get the all information related to database for particular record when List Item clicked.

Look at SimpleCursorAdapters and ListViews

Creating a custom CursorAdapter for Android

Sign up to request clarification or add additional context in comments.

Comments

1

after this line..

   students[i] = c.getString(0).toString()+" "+c.getString(1).toString();
    student_ids[i]=Integer.parseInt(//get the  id... )
  //and you dont need to put it in invisible edittext...

in onclick method you will get integer "position"..and get tha value in students_ids[] at that position..

 int id=students_ids[position];

4 Comments

How will i get integer position when i click on edit button?.. can u please tell..
is your button not a part of listview's row?
yes button is within the row. i have updated question.. added my layout. please check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.