ListView控件.putExtra从数据库列 [英] ListView .putExtra from DB Column
问题描述
我想从一个包含URL链接一个数据库列中的数据;列名是'转到'W /在数据库中。我需要通过这个进我onItemClick意图在我的ListView加载网页到新的/下一个活动。
我不知道如何得到这个功能。如果你能请出示code修改与解释,这将是对我很有帮助学习。日Thnx!
修订版:
我现在得到传递的数据,但我的方法将返回错误的行ID。我修改了以下活动。任何帮助PLZ。日Thnx
在我的活动.onCreate(修订本):
最终的ListView LV = getListView();
lv.setTextFilterEnabled(真正的);
lv.setOnItemClickListener(新OnItemClickListener(){
// @覆盖
公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长ID)
{
对象o = lv.getSelectedItem();
// Adapter_AC fullObject =(Adapter_AC)O;
字符串URL =gotoURL;
如果(V!= NULL){
TextView的电视=(TextView中)lv.findViewById(R.id.dummy);
URL =(字符串)tv.getTag();
}
Toast.makeText(List_AC.this,已点击+网址,Toast.LENGTH_LONG).show();
意图I =新的意图(List_AC.this,DocView.class);
i.putExtra(URL,网址);
startActivity(ⅰ);
}
});
我的适配器:
公共类Adapter_AC扩展SimpleCursorAdapter {
私人光标dataCursor;
私人LayoutInflater mInflater;
公共Adapter_AC(上下文的背景下,INT布局,光标dataCursor,
的String []从,INT []键){
超(背景下,布局,dataCursor,从,到);
this.dataCursor = dataCursor;
mInflater = LayoutInflater.from(上下文);
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder持有人;
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.list_item,NULL);
持有人=新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(保持器);
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
dataCursor.moveToPosition(位置);
INT label_index = dataCursor.getColumnIndex(标签);
字符串标签= dataCursor.getString(label_index);
INT title_index = dataCursor.getColumnIndex(标题);
字符串标题= dataCursor.getString(title_index);
INT description_index = dataCursor.getColumnIndex(说明);
字符串描述= dataCursor.getString(description_index);
INT goto_index = dataCursor.getColumnIndex(GOTO);
字符串gotoURL = dataCursor.getString(goto_index);
holder.text1.setText(标签);
holder.text2.setText(职称);
holder.text3.setText(介绍);
返回convertView;
}
静态类ViewHolder {
TextView的文本1;
TextView的文本2;
TextView的文本3;
}
}
在 OnItemClickListener.onItemClick
方法,有一个 ID
参数,而你正在使用 SimpleCursorAdapter
的ID,这是选择 _ID
列在SQLite数据库,项目将被传递给 ID
参数,这样你可以得到所选列从数据库与此 ID
值。
以下是示例code,希望能有所帮助。
公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长的id){
光标C = queryById(ID);
//获取URL的数据库。
字符串URL = c.getString(URL);
}
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;
}
}
In OnItemClickListener.onItemClick
method, there is an id
arguments, while you are using SimpleCursorAdapter
, the id, which is _ID
column in sqlite database, of selected item will be passed to the id
argument, so you can get the selected column from DB with this id
value.
Following is the sample code, hope it can help.
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Cursor c = queryById(id);
// Get the url from DB.
String url = c.getString("url");
}
这篇关于ListView控件.putExtra从数据库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!