如何通过一个字符串转换成onItemClick的ListView? [英] How to pass a string into onItemClick ListView?

查看:131
本文介绍了如何通过一个字符串转换成onItemClick的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何得到我的光标串到我putExtra呼叫在onItemClick为一个ListView?我需要从我的名为gotoURL到DB列抢字符串:

  i.putExtra(URL,???)。

...我在活动onItemClick的。示例code将是有助于学习。日Thnx!

我的活动有:

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    的setContentView(R.layout.list_view2);    最终的ListView LV = getListView();    activityTitle =(的TextView)findViewById(R.id.titleBarTitle);
    activityTitle.setText(顾问环行);    displayResultList();    lv.setTextFilterEnabled(真);
    lv.setOnItemClickListener(新OnItemClickListener(){
        // @覆盖
        公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长的ID)
        {
            Toast.makeText(List_AC.this,点击了!,Toast.LENGTH_LONG).show();
            对象o = lv.getItemAtPosition(位置);
            Adapter_AC fullObject =(Adapter_AC)O;
            意图I =新意图(List_AC.this,DocView.class);
            //i.putExtra(\"url,Adapter_AC.gotoURL);
            startActivity(ⅰ);
        }
    });
}私人无效displayResultList(){    如果(android.os.Environment.getExternalStorageState()。等于(
            android.os.Environment.MEDIA_MOUNTED)){
        extStorageDirectory = Environment.getExternalStorageDirectory()
                的ToString();        文件DBFILE =新的文件(extStorageDirectory
                +/XXX/XXX/dB/XXX.db);        SQLiteDatabase DB = SQLiteDatabase.openOrCreateDatabase(DBFILE,
                空值);        光标databaseCursor = db.rawQuery(
                SELECT * FROM AC_list ORDER BY`label` ASC,NULL);        Adapter_AC databaseListAdapter =新Adapter_AC(这一点,
                R.layout.list_item,databaseCursor,新的String [] {标签
                        标题,说明},新的INT [] {R.id.label,
                        R.id.listTitle,R.id.caption});        databaseListAdapter.notifyDataSetChanged();
        this.setListAdapter(databaseListAdapter);    }否则如果(android.os.Environment.getExternalStorageState()。等于(
            android.os.Environment.MEDIA_UNMOUNTED)){
        Log.i(标签,SD卡不可写/安装);
        Alerts.sdCardMissing(本);
    }
}
}

和我的适配器:

 公共类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(标题);
    字符串title = dataCursor.getString(title_index);    INT description_index = dataCursor.getColumnIndex(说明);
    字符串描述= dataCursor.getString(description_index);    INT goto_index = dataCursor.getColumnIndex(gotoURL);
    字符串gotoURL = dataCursor.getString(goto_index);    holder.text1.setText(标签);
    holder.text2.setText(职称);
    holder.text3.setText(介绍);    返回convertView;
}静态类ViewHolder {
    TextView的文本1;
    TextView的文本2;
    TextView的文字3;
}
}


解决方案

您可以使用CursorAdapter的的getItem()方法来获取指向当前ID的光标。像这样:

  ...
光标光标= adapter.getItem(ID);
i.putExtra(URL,cursor.getString(cursor.getColumnIndex(gotoURL)));
...

请注意,我怀疑你需要包括列在你的投影,像这样:

  Adapter_AC databaseListAdapter =新Adapter_AC(这一点,
            R.layout.list_item,databaseCursor,新的String [] {标签
                    标题,说明,gotoURL},新的INT [] {R.id.label,
                    R.id.listTitle,R.id.caption,R.id.dummy});

和创建一个虚拟的(View.GONE)为不可见领域,使得数据在光标被拉

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;
}
} 

解决方案

You can use the CursorAdapter's getItem() method to get a Cursor pointing to the current id. Like so:

...
Cursor cursor = adapter.getItem(id);
i.putExtra("url", cursor.getString(cursor.getColumnIndex("gotoURL")));
...

Note that I suspect you'll need to include that column in your projection, like so:

    Adapter_AC databaseListAdapter = new Adapter_AC(this,
            R.layout.list_item, databaseCursor, new String[] { "label",
                    "title", "description", "gotoURL" }, new int[] { R.id.label,
                    R.id.listTitle, R.id.caption, R.id.dummy });

And create a dummy (View.GONE) invisible field for it so that the data is pulled in the cursor.

这篇关于如何通过一个字符串转换成onItemClick的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆