ListView .putExtra จากคอลัมน์ DB

ฉันกำลังพยายามรับข้อมูลจากคอลัมน์ db ที่มีลิงก์ URL ชื่อคอลัมน์คือ 'goto' โดยมีอยู่ใน db ฉันต้องส่งสิ่งนี้ไปยัง onItemClick Intent ใน ListView ของฉันเพื่อโหลดหน้าเว็บไปยังกิจกรรมใหม่/ถัดไป

ฉันไม่รู้ว่าจะทำให้สิ่งนี้ทำงานได้อย่างไร หากคุณสามารถกรุณาแสดงการแก้ไขโค้ดพร้อมคำอธิบาย สิ่งนี้จะมีประโยชน์มากสำหรับฉันในการเรียนรู้ ขอบคุณ!

แก้ไข:

ตอนนี้ฉันได้รับข้อมูลแล้ว แต่วิธีการของฉันส่งคืน ID แถวที่ไม่ถูกต้อง ฉันแก้ไขกิจกรรมด้านล่าง ความช่วยเหลือใด ๆ กรุณา ขอบคุณ

ภายในกิจกรรมของฉัน .onCreate (แก้ไข):

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

อะแดปเตอร์ของฉัน:

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

person Community    schedule 30.04.2011    source แหล่งที่มา
comment
@anticafe มีความคิดเห็นเกี่ยวกับสิ่งที่ฉันผิดไหม? ขอบคุณ   -  person    schedule 03.05.2011


คำตอบ (3)


ลองใช้ setTag:
ตอนแรกเราตั้งค่าแท็ก:

...
holder.text3.setText(description);
holder.text3.setTag(gotoURL);

และเมื่อดำเนินการเหตุการณ์ onClick รับแท็ก:

public void onItemClick(AdapterView<?> a, View v, int position,long id) 
{
    ...
    String url = "";
    if(v != null) {
        TextView tv = (TextView)arg1.findViewById(R.id.caption);
        url = (String) tv.getTag();
    }
    i.putExtra("url", url);
    startActivity(i);
}
person anticafe    schedule 30.04.2011
comment
ขอบคุณที่มีประโยชน์มาก ปัญหาเดียวที่ฉันมีตอนนี้คือการดึงข้อมูลจากแถว DB ที่ไม่ถูกต้อง ไม่ว่าจะเลือก listItem ใด ก็จะส่งกลับคอลัมน์ที่ถูกต้องจากแถวเดียวกัน ฉันแน่ใจว่าฉันขาดอะไรบางอย่าง ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชม ขอบคุณ! - person ; 03.05.2011

ในวิธี OnItemClickListener.onItemClick มีอาร์กิวเมนต์ id ในขณะที่คุณใช้ SimpleCursorAdapter id ซึ่งเป็นคอลัมน์ _ID ในฐานข้อมูล sqlite ของรายการที่เลือกจะถูกส่งผ่านไปยังอาร์กิวเมนต์ id ดังนั้นคุณจึงสามารถรับคอลัมน์ที่เลือกจาก DB ด้วย id นี้ ค่า.

ต่อไปนี้เป็นโค้ดตัวอย่าง หวังว่าจะช่วยได้

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");
}
person Tony    schedule 30.04.2011

เหตุผลที่คุณดึงข้อมูลจากแถวที่ไม่ถูกต้องเป็นเพราะคุณจัดการพารามิเตอร์ convertView ไม่ถูกต้อง คุณจะตั้งค่า holder ให้ชี้ไปที่วัตถุ TextView ชุดแรกสุดที่คุณตั้งค่าไว้เท่านั้น ฉันคิดว่าแนวคิด holder ของคุณไม่จำเป็น แพ้ซะ ตั้งค่าฟิลด์ข้อความให้ชัดเจนในแต่ละครั้ง ใช้ Cursor ตามที่ฉันพูดถึงในคำถามก่อนหน้าของคุณ

person Brian Dupuis    schedule 03.05.2011