开发者

Set and retrieve ID from database in ListView in Android

开发者 https://www.devze.com 2023-01-27 03:51 出处:网络
I have an Android app which uses the ListView to populate courses from a database. What I want is to put the CourseID from the database in the ListView. I also want to use the OnItemClickListener to p

I have an Android app which uses the ListView to populate courses from a database. What I want is to put the CourseID from the database in the ListView. I also want to use the OnItemClickListener to parse this ID to a new intent which shows the course details.

My code looks like this:

import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
impor开发者_运维知识库t android.os.Bundle;
import android.text.InputFilter.LengthFilter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class Menu extends Activity {
/** Called when the activity is first created. */

private ListView lv1;
private String lv_arr[]={"BSc Business Information Technology","BSc Computer Forensics","BSc Computer Science","BSc Computing","BSc Internet Computing","BSc IT Security","BSc Mobile Computing","BSc Software Engineering"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lv1 = (ListView)findViewById(R.id.ListView01);

    DataBaseHelper myDbHelper;
    myDbHelper = new DataBaseHelper(this);

    try 
    {
        myDbHelper.createDataBase();
    } 
    catch (IOException ioe) 
    {

        throw new Error("Unable to create database");
    }

    try 
    {
        myDbHelper.openDataBase();

        SQLiteDatabase db = myDbHelper.getReadableDatabase();

        Cursor cursor = db.query("Courses", new String[]{"coursename"}, null, null, null, null, "coursename");
        startManagingCursor(cursor);

        if (cursor.getCount() > 0)
        {
            if (cursor.moveToFirst())
            {
                ArrayList strings = new ArrayList();
                do 
                {                    
                    String mC = cursor.getString(0);
                    strings.add(mC);  
                }
                while (cursor.moveToNext());

                lv_arr = (String[]) strings.toArray(new String[strings.size()]);
            }
        }  
        else 
        {
            Toast.makeText(this, "No courses in database", Toast.LENGTH_SHORT).show();
        }

        cursor.close();

        // By using setAdpater method in listview we an add string array in list.
        lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv_arr));      

    }
    catch(SQLException sqle)
    {
        throw sqle;
    }   

    lv1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View view, int position, long id) 
        {
            Intent detailsIntent = new Intent(view.getContext(), CourseDetails.class);
            detailsIntent.putExtra("CourseID", position);
            //Cursor c = (Cursor) cursorAdapter.getItem(position);
            startActivity(detailsIntent);
         }

    });
}
}

Instead of the "position" parameter in the putExtra() method I want to put the Course ID from the database. How do I do that?


If you are populating data from a database then i suggest you to use CursorAdapter. An instance of Cursor is passed to the bind view as well as new view.

bindView(View view, Context context, Cursor cursor)

newView(Context context, Cursor cursor, ViewGroup parent)

For the current case try extending the ArrayAdapter to get a Custom Adapter. Set the tag as the Course ID value and then by view.getTag() the CourseId pertaining to the list item click can be retrived.

0

精彩评论

暂无评论...
验证码 换一张
取 消