I am trying to get data from a db column that contains an URL link; The column name is 'goto' w/in the db. I need to pass this into my onItemClick Intent in my ListView to load a webpage into the new/next Activity.
I don't know how to get this to function. If you can please show code modifications with an explanation, this would be very helpful to me learning. Thnx!
REVISED:
I am now getting data passed but my method is returning the wrong row ID. I revised the below Activity. Any help Plz. THNX
Within my Activities .onCreate (REVISED):
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
// @Override
public void onItemClick(AdapterView<?> a, View v, int position,long id)
{
Object o = lv.getSelectedItem();
//Adapter_AC fullObject = (Adapter_AC)o;
String url = "gotoURL";
if(v != null) {
TextView tv = (TextView)lv.findViewById(R.id.dummy);
url = (String) tv.getTag();
}
Toast.makeText(List_AC.this, "Clicked: " + url, Toast.LENGTH_LONG).show();
Intent i = new Intent(List_AC.this, DocView.class);
i.putExtra("url", url);
startActivity(i);
}
});
My Adapter:
public class Adapter_AC extends SimpleCursorAdapter {
private Cursor dataCursor;
private LayoutInflater mInflater;
public Adapter_AC(Context context, int layout, Cursor dataCursor,
String[] from, int[] to) {
super(context, layout, dataCursor, from, to);
this.dataCursor = dataCursor;
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.label);
holder.text2 = (TextView) convertView.findViewById(R.id.listTitle);
holder.text3 = (TextView) convertView.findViewById(R.id.caption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
dataCursor.moveToPosition(position);
int label_index = dataCursor.getColumnIndex("label");
String label = dataCursor.getString(label_index);
int title_index = dataCursor.getColumnIndex("title");
String title = dataCursor.getString(title_index);
int description_index = dataCursor.getColumnIndex("description");
String description = dataCursor.getString(description_index);
int goto_index = dataCursor.getColumnIndex("goto");
String gotoURL = dataCursor.getString(goto_index);
holder.text1.setText(label);
holder.text2.setText(title);
holder.text3.setText(description);
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
TextView text3;
}
}
Try to use setTag:
At first, we set tag:
And when perform onClick event, get tag:
The reason you're pulling data from the wrong row is because you're handling the
convertView
parameter incorrectly. You're only ever setting theholder
to point at the very first set ofTextView
objects you set up. I think that yourholder
concept is unnecessary. Lose that, set text fields explicitly each time, use theCursor
as I mentioned in your previous question.In
OnItemClickListener.onItemClick
method, there is anid
arguments, while you are usingSimpleCursorAdapter
, the id, which is_ID
column in sqlite database, of selected item will be passed to theid
argument, so you can get the selected column from DB with thisid
value.Following is the sample code, hope it can help.