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>
Bibin Velayudhan
  • 3,043
  • 1
  • 22
  • 33

2 Answers2

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];
5hssba
  • 8,079
  • 2
  • 33
  • 35
  • How will i get integer position when i click on edit button?.. can u please tell.. – Bibin Velayudhan Apr 21 '12 at 06:44
  • is your button not a part of listview's row? – 5hssba Apr 21 '12 at 06:47
  • yes button is within the row. i have updated question.. added my layout. please check. – Bibin Velayudhan Apr 21 '12 at 06:52
  • [You should use a custom adapter.. see this link](http://www.geekmind.net/2009/11/android-custom-list-item-with-nested.html) and [this one](http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons) – 5hssba Apr 21 '12 at 07:11
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

user370305
  • 108,599
  • 23
  • 164
  • 151