Reputation: 6336
I have been searching through a lot of questions but they don't ask or answer what I'm looking for, so here is what I'm trying to do. So far I have an project in witch I added a pre populated database, with a table that contains 6 columns of data, the main xml just displays the list view, I'm using a Adapter, Cursor, SQLiteHelper etc. all works fine and displays my custom itemlist.xml all good, now in every item of the list I display only 3 things (second, fourth and fifth column data) of each record (they all contain 6 coluns like I mention before), I want to be able with a onclickitemlistener to display in a toast the data from the third column for that item, how do I accomplish that? I'm able to get the list item id displayed, so can someone help me here, Thanks here is som code: Code from y main activity
public class DBlistActivity extends Activity {
private mediosHelper dbmediosHelper = null;
private Cursor ourCursor = null;
FancyAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.mylistView);
dbmediosHelper = new mediosHelper(this);
try {
dbmediosHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
dbmediosHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
try {
ourCursor = dbmediosHelper.getCursor();
startManagingCursor(ourCursor);
}catch(SQLException sqle){
throw sqle;
}
adapter = new FancyAdapter(ourCursor);
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(onListClick);
}
class FancyAdapter extends CursorAdapter {
FancyAdapter(Cursor c) {
super (DBlistActivity.this, c);
}
public void bindView(View row, Context ctxt, Cursor c) {
ViewHolder holder = (ViewHolder) row.getTag();
holder.populateForm(c, dbmediosHelper);
}
public View newView(Context ctxt, Cursor c, ViewGroup parent){
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.listitem, parent, false);
ViewHolder holder = new ViewHolder(row);
row.setTag(holder);
return(row);
}
}
class ViewHolder{
public TextView nombre=null;
public TextView estado=null;
public TextView ciudad=null;
ViewHolder (View row){
nombre = (TextView) row.findViewById(R.id.medioName);
estado = (TextView) row.findViewById(R.id.medioEstado);
ciudad = (TextView) row.findViewById(R.id.medioCiudad);
}
void populateForm(Cursor c, mediosHelper r){
nombre.setText(r.getName(c));
estado.setText(r.getEstado(c));
ciudad.setText(r.getCiudad(c));
}
}
private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Toast.makeText(getApplicationContext(), String.valueOf(id), Toast.LENGTH_SHORT) .show();
}
};
And from my mediosHelper
public Cursor getCursor() {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME);
String[] asColumsToReturn = new String[] { COLUMN_ID, COLUMN_CODE, COLUMN_NAME, COLUMN_URL, COLUMN_ESTADO, COLUMN_CIUDAD};
Cursor mCursor = queryBuilder.query(myDataBase, asColumsToReturn, null, null, null, null, null);
return mCursor;
}
public CharSequence getId(Cursor c) {
return (c.getString(0));
}
public CharSequence getCode(Cursor c) {
return (c.getString(1));
}
public String getName(Cursor c){
return(c.getString(2));
}
public String getUrl(Cursor c) {
return (c.getString(3));
}
public String getEstado(Cursor c) {
return (c.getString(4));
}
public String getCiudad(Cursor c) {
return (c.getString(5));
}
Upvotes: 0
Views: 278
Reputation: 628
you need to get the cursor you used to fill the adapter. once you have that move it to the position you get in the onItemClickedCallback with c.moveToPosition(position). Than you have access to all columns (cursor.getColumnIndex("_id"))
EDIT:
class MyActivity extends Activity implements OnItemClickListener{
FancyAdapter adapter = null;
...
adapter = new FancyAdapter(ourCursor);
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(this);
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
Cursor c = adapter.getCursor();
c.moveToPosition(position);
int id = c.getInt(c.getColumnIndex("_id");
}
}
Upvotes: 1