如何将字符串传递到 onItemClick ListView 中?
如何将光标中的字符串放入 ListView 的 onItemClick 中的 putExtra 调用中?我需要将名为“gotoURL”的数据库列中的字符串抓取到
i.putExtra("Url", ???).
活动中的 onItemClick 的:...中。示例代码对学习有帮助。谢谢!!
我的活动有:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_view2);
final ListView lv = getListView();
activityTitle = (TextView) findViewById(R.id.titleBarTitle);
activityTitle.setText("ADVISORY CIRCULATORS");
displayResultList();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
// @Override
public void onItemClick(AdapterView<?> a, View v, int position,long id)
{
Toast.makeText(List_AC.this, "Clicked!", Toast.LENGTH_LONG).show();
Object o = lv.getItemAtPosition(position);
Adapter_AC fullObject = (Adapter_AC)o;
Intent i = new Intent(List_AC.this, DocView.class);
//i.putExtra("url", Adapter_AC.gotoURL);
startActivity(i);
}
});
}
private void displayResultList() {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File dbfile = new File(extStorageDirectory
+ "/XXX/XXX/dB/XXX.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
null);
Cursor databaseCursor = db.rawQuery(
"SELECT * FROM AC_list ORDER BY `label` ASC", null);
Adapter_AC databaseListAdapter = new Adapter_AC(this,
R.layout.list_item, databaseCursor, new String[] { "label",
"title", "description" }, new int[] { R.id.label,
R.id.listTitle, R.id.caption });
databaseListAdapter.notifyDataSetChanged();
this.setListAdapter(databaseListAdapter);
} else if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_UNMOUNTED)) {
Log.i("tag", "SDCard is NOT writable/mounted");
Alerts.sdCardMissing(this);
}
}
}
我的适配器:
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("gotoURL");
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;
}
}
How do I get a string from my cursor into my putExtra call in an onItemClick for a ListView? I need to grab the string from my DB column named 'gotoURL' into the:
i.putExtra("Url", ???).
...of my onItemClick in the Activity. Sample code would be helpful for the learning. Thnx!!
My Activity has:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_view2);
final ListView lv = getListView();
activityTitle = (TextView) findViewById(R.id.titleBarTitle);
activityTitle.setText("ADVISORY CIRCULATORS");
displayResultList();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
// @Override
public void onItemClick(AdapterView<?> a, View v, int position,long id)
{
Toast.makeText(List_AC.this, "Clicked!", Toast.LENGTH_LONG).show();
Object o = lv.getItemAtPosition(position);
Adapter_AC fullObject = (Adapter_AC)o;
Intent i = new Intent(List_AC.this, DocView.class);
//i.putExtra("url", Adapter_AC.gotoURL);
startActivity(i);
}
});
}
private void displayResultList() {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File dbfile = new File(extStorageDirectory
+ "/XXX/XXX/dB/XXX.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
null);
Cursor databaseCursor = db.rawQuery(
"SELECT * FROM AC_list ORDER BY `label` ASC", null);
Adapter_AC databaseListAdapter = new Adapter_AC(this,
R.layout.list_item, databaseCursor, new String[] { "label",
"title", "description" }, new int[] { R.id.label,
R.id.listTitle, R.id.caption });
databaseListAdapter.notifyDataSetChanged();
this.setListAdapter(databaseListAdapter);
} else if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_UNMOUNTED)) {
Log.i("tag", "SDCard is NOT writable/mounted");
Alerts.sdCardMissing(this);
}
}
}
And 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("gotoURL");
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用 ListView 中的“标签”字段:在创建和读取它时将其设置为正确的值,以便在单击时获得正确的值...
You might try using the 'Tag' field in the ListView: set it to the correct value when creating and reading it to get the right value when clicked...
您可以使用 CursorAdapter 的 getItem() 方法来获取指向当前 id 的 Cursor。像这样:
请注意,我怀疑您需要在投影中包含该列,像这样:
并为其创建一个虚拟(View.GONE)不可见字段,以便将数据拉入光标。
You can use the CursorAdapter's
getItem()
method to get a Cursor pointing to the current id. Like so:Note that I suspect you'll need to include that column in your projection, like so:
And create a dummy (View.GONE) invisible field for it so that the data is pulled in the cursor.