在Android中的ListView中设置和检索数据库中的ID

时间:2010-11-25 16:42:32

标签: android listview android-intent

我有一个Android应用程序,它使用ListView来填充数据库中的课程。我想要的是将ListID从数据库中放入ListView。我还想使用OnItemClickListener将此ID解析为显示课程详细信息的新意图。

我的代码如下所示:

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;
import 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);
         }

    });
}
}

而不是putExtra()方法中的“position”参数,我想从数据库中放入课程ID。我该怎么做?

1 个答案:

答案 0 :(得分:0)

如果要从数据库填充数据,我建议您使用CursorAdapter。 将Cursor的一个实例传递给绑定视图以及新视图。

bindView(视图视图,上下文上下文,光标游标)

newView(Context context,Cursor cursor,ViewGroup parent)

对于当前案例,请尝试扩展ArrayAdapter以获取自定义适配器。 将标记设置为课程ID值,然后通过view.getTag()可以重新检索与列表项单击相关的CourseId。