I have a method named getData
that populates a listView
from my database limited to specified columns from the table:
public ArrayList<String> getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_MODULE_CODE,
KEY_MODULE_NAME, KEY_LECTURE_PRACTICAL,
KEY_LECTURE_PRACTICAL_SHORT, KEY_LECTURE_DAY,
KEY_LECTURE_DAY_SHORT, KEY_START_TIME, KEY_END_TIME,
KEY_LOCATION, ADDITIONAL_INFO };
Cursor c = ourDatabase.query(DATABASE_MODULES, columns, null, null,
null, null, null);
ArrayList<String> results = new ArrayList<String>();
int indexModCode = c.getColumnIndex(KEY_MODULE_CODE);
int indexLectPracShort = c.getColumnIndex(KEY_LECTURE_PRACTICAL_SHORT);
int indexLectDayShort = c.getColumnIndex(KEY_LECTURE_DAY_SHORT);
int indexLectStart = c.getColumnIndex(KEY_START_TIME);
int indexLectLoc = c.getColumnIndex(KEY_LOCATION);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
results.add(c.getString(indexModCode) + " "
+ c.getString(indexLectPracShort) + " "
+ c.getString(indexLectDayShort) + " "
+ c.getString(indexLectStart) + " "
+ c.getString(indexLectLoc));
}
return results;
}
This works fine and here is how I populate my listview (ModuleDatabaseHandler
is the class where the database is managed):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_modules_test);
ModuleDatabaseHandler info = new ModuleDatabaseHandler(this);
info.open();
ArrayList<String> data = info.getData();
info.close();
l = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,data);
l.setAdapter(adapter);
l.setOnItemClickListener(this);
}
I want to be able to click the listView
item, and view ALL the information pertaining to the record (in the listView
it only shows certain information). I have attempted to create a new method in the ModuleDatabaseHandler
class called getAllData
.
Below is the OnItemClick
for the ListView
item which launches a new activity, passing through the information from getAllData
using putExtra
:
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView temp = (TextView) view;
Toast.makeText(this, temp.getText() + " " + i, Toast.LENGTH_SHORT)
.show();
ModuleDatabaseHandler info = new ModuleDatabaseHandler(this);
info.open();
String module_info = info.getAllData(); // METHOD TO GET ALL DATA
info.close();
Intent fullModuleDetails = new Intent();
fullModuleDetails.setClassName("com.example.collegetimetable",
"com.example.collegetimetable.ModuleDetails");
fullModuleDetails.putExtra("list1", module_info);
startActivity(fullModuleDetails);
}
Here is the getAllData
method which retrieves all the information from the database. How could I alter this method, to retrieve only the information for the row relating to the particular listView
item that was clicked by the user?
public String getAllData() {
String[] columns = new String[]{ KEY_ROWID, KEY_MODULE_CODE, KEY_MODULE_NAME, KEY_LECTURE_PRACTICAL, KEY_LECTURE_DAY, KEY_START_TIME,KEY_END_TIME, KEY_LOCATION, ADDITIONAL_INFO};
Cursor c = ourDatabase.query(DATABASE_MODULES, columns, null, null, null, null, null);
String results = "";
int indexRow = c.getColumnIndex(KEY_ROWID);
int indexModCode = c.getColumnIndex(KEY_MODULE_CODE);
int indexModName = c.getColumnIndex(KEY_MODULE_NAME);
int indexLectPrac = c.getColumnIndex(KEY_LECTURE_PRACTICAL);
int indexLectDay = c.getColumnIndex(KEY_LECTURE_DAY);
int indexLectStart = c.getColumnIndex(KEY_START_TIME);
int indexLectEnd = c.getColumnIndex(KEY_END_TIME);
int indexLectLoc = c.getColumnIndex(KEY_LOCATION);
int indexLectInfo = c.getColumnIndex(ADDITIONAL_INFO);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
results = results + ( c.getString(indexModCode) + " " + c.getString(indexModName) + " " + c.getString(indexLectPrac)
+ " " + c.getString(indexLectDay) + " " + c.getString(indexLectStart) + " " + c.getString(indexLectEnd) + " "
+ c.getString(indexLectLoc) + " " + c.getString(indexLectInfo));
}
return results;
}
Thanks for any help
Instead of looping through the data, you would do something along the lines:
public String getAllDataForRow(int row) {
String[] columns = new String[]{ KEY_ROWID, KEY_MODULE_CODE, KEY_MODULE_NAME, KEY_LECTURE_PRACTICAL, KEY_LECTURE_DAY, KEY_START_TIME,KEY_END_TIME, KEY_LOCATION, ADDITIONAL_INFO};
Cursor c = ourDatabase.query(DATABASE_MODULES, columns, null, null, null, null, null);
if(c.moveToPosition(row)) {
String result = "";
for(int columnIndex = 0; columnIndex<c.getColumnCount();columnIndex++) {
result += c.getString(columnIndex)+" ";
}
return result;
} else {
throw new IllegalArgumentException("Row " + row + " does not exist");
}
}
Also, if you didn't know: If you want all columns, you actually do not have to specify them in the projection, but can just pass null instead of columns.
Hope that helps.