2 csh 34 csh_34 于 2013.04.16 13:23 提问

如何传递一个字符串到 onItemClick ListView?

如何从cursor中获取一个字符串然后放入 putExtra 。为ListView调用一个onItemClick。我需要从数据库名是'gotoURL' 的数据库中获取字符串,再放进 activity 里的 onItemClick 方法中:

i.putExtra("Url", ???). 

Activity:

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

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

3个回答

caikeyter
caikeyter   2013.04.17 09:34
已采纳

一般不这么写,一般不会直接传Cursor dataCursor这个到适配器里面。你直接在外面把数据查询出来然后放到一个容器里面,然后把容器传进去。
首先在Activity:里面定义全局的适配器和数据容器。
然后再onCreate事件里面加载数据,和通过适配器将数据绑定到listview里面。
public class AppContactActivity extends Activity {

ListView listView;
ContactAdapter cadp;

static ArrayList<ContactEtity> listDatas;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myappcontact);
    listView = (ListView) this.findViewById(R.id.lvAppContacts);
    listView.setDivider(null);
    try {
        listDatas = MyApplication.ContactEtityAll;
    } catch (Exception e) {
        System.out.print(e.getMessage());
    }

    try {

        if (listDatas != null) {
            if (listDatas.size() <= 0) {

            } else {
                cadp = new ContactAdapter(getApplicationContext(),
                        listDatas);

                // 适配器与ListView绑定
                listView.setAdapter(cadp);
                listView.setOnItemClickListener(new ItemClickListener());
            }

        }
    } catch (Exception e) {
        System.out.print(e.getMessage());
    }

}

private class ItemClickListener implements OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
                   //根据下标在适配器中获取你选中的对象数据。
        ContactEtity idContent = (ContactEtity) cadp.getItem(position);
        Intent intent = new Intent();
        Bundle bunle = new Bundle();
        bunle.putStringArray("NamePhone", new String[] { idContent.name,
                idContent.phone });
        intent.putExtras(bunle);
        setResult(RESULT_OK, intent);
        finish();

    }

}

}

baimy1985
baimy1985   2013.04.16 21:02

enter code here如果我没猜做的话,你是想获取dataCursor里"gotoURL"这一列的值,但是现在int goto_index = dataCursor.getColumnIndex("gotoURL");是一个局部变量,外面访问不到吧,而且这个强制转换合适吗?

Object o = lv.getItemAtPosition(position);
            Adapter_AC fullObject = (Adapter_AC)o;

也许你可以在你的ViewHolder 多写一个Textview,当然这个textview的可见属性是“gone”这个Textview用来保存int goto_index = dataCursor.getColumnIndex("gotoURL");,这样在onItemClick里得到每一行的view或cursor都是可以的,再通过Id或cursor获取你想要值应该可行。 也许你的思路凌乱了。

JJMM2009
JJMM2009   2013.04.16 21:58

可以使用setTag的方式在listview的itemview中把想要传递的值存储起来,在onItemClick的时候从itemview中取tag,就可以了

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!