ListView i Android
Jeg ønsker at lave en Android app med et ListView med en række kurser, som kommer fra en database. Når man klikker i ListView'et, skal man sendes videre til en ny intent, hvorfra ID'et fra databasen kan hentes, så der kan vises mere information fra kurset.Mit problem er, at jeg ikke ved, hvordan jeg lægger ID'et ind i ListView'et og ikke ved, hvordan jeg henter det ud igen på den nye intent.
Min side med ListView'et ser således ud:
---------------------------------------------------
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);
startActivity(detailsIntent);
}
});
}
}
---------------------------------------------------
Dvs i stedet for defailtIntent.putExtra("CourseID", position); vil jeg gerne sende kursusID'et videre i stedet for positionen. Dette vil jeg ligeledes gerne trække ud på den nye intent.
Nogle bud?