程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> adapter-如何傳遞一個字符串到 onItemClick ListView?

adapter-如何傳遞一個字符串到 onItemClick ListView?

編輯:編程綜合問答
如何傳遞一個字符串到 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;
}
} 

最佳回答:


一般不這麼寫,一般不會直接傳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();

    }

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved