ListView clickEvent 选择错误的数据库行 ID - 帮助!

发布于 2024-11-04 21:16:55 字数 4542 浏览 0 评论 0原文

我有一个 ListView,它将数据从数据库填充到复杂的 ListView 中。数据库的“gotoURL”列中的字符串是使用 putExtra 意图将链接加载到 WebView 的 URL。当我单击 ListItem 转到下一个 Activity 时,我的方法会获取正确的字符串数据,但它会从错误的数据库行中提取字符串。根据我的“log.i(...);”指示在 DDMS 中,选择了正确的 id 行,但列中的字符串是从第一行 id 中提取的,而不是从选定的行 id 中提取的(???) - 它在选定的任何 ListItem 上执行此操作。

我不知道如何编写它才能正确运行。请帮助提供示例代码。谢谢。

我的活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.list_view2);

    activityTitle = (TextView) findViewById(R.id.titleBarTitle);
    activityTitle.setText("ADVISORY CIRCULATORS");

    displayResultList();

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setClickable(true);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> a, View v, int pos,
                long id) {

            String url = "";
            lv.getItemIdAtPosition(pos);
            TextView tv = (TextView) lv.findViewById(R.id.dummy);
            url = (String) tv.getTag();

            LLog.i("tag", "ID:" + id + "URL: " + url + " selected");
            Intent i = new Intent(List_AC.this, DocView.class);
            i.putExtra("url", url);
            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
                + "/Aero-Technologies/flyDroid/dB/flyDroid.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", "gotoURL" }, new int[] {
                        R.id.label, R.id.listTitle, R.id.caption,
                        R.id.dummy });

        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 {


static 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);
        holder.text4 = (TextView) convertView.findViewById(R.id.dummy);

        holder.text4.setVisibility(View.GONE);

        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);
    //holder.text4.setText(gotoURL);
    holder.text4.setTag(gotoURL);

    return convertView;
}

static class ViewHolder {
    TextView text1;
    TextView text2;
    TextView text3;
    TextView text4;
}

}

I have a ListView that populates data from a DB into a complex-ListView. A string in the 'gotoURL' column of the db is a URL to load a link into a WebView using the putExtra intent. When I click on a ListItem to go to the next Activity, my method grabs the correct string data, but it pulls the string from the wrong db row. As per my 'log.i(...);' indicates in the DDMS, the correct id row is selected but the string from the column is pulled from the first row id and not from the row id selected(???) - it does this on any ListItem selected.

I cant figure out how to write this to function correctly. Plz help with example code. Thank-you.

My Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.list_view2);

    activityTitle = (TextView) findViewById(R.id.titleBarTitle);
    activityTitle.setText("ADVISORY CIRCULATORS");

    displayResultList();

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setClickable(true);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> a, View v, int pos,
                long id) {

            String url = "";
            lv.getItemIdAtPosition(pos);
            TextView tv = (TextView) lv.findViewById(R.id.dummy);
            url = (String) tv.getTag();

            LLog.i("tag", "ID:" + id + "URL: " + url + " selected");
            Intent i = new Intent(List_AC.this, DocView.class);
            i.putExtra("url", url);
            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
                + "/Aero-Technologies/flyDroid/dB/flyDroid.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", "gotoURL" }, new int[] {
                        R.id.label, R.id.listTitle, R.id.caption,
                        R.id.dummy });

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

My Adapter:

public class Adapter_AC extends SimpleCursorAdapter {


static 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);
        holder.text4 = (TextView) convertView.findViewById(R.id.dummy);

        holder.text4.setVisibility(View.GONE);

        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);
    //holder.text4.setText(gotoURL);
    holder.text4.setTag(gotoURL);

    return convertView;
}

static class ViewHolder {
    TextView text1;
    TextView text2;
    TextView text3;
    TextView text4;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹沉浮 2024-11-11 21:16:55

好吧,你的 onItemClick 有一些问题(请参阅内联注释):

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int pos,
                long id) {

            String url = "";
            lv.getItemIdAtPosition(pos); //you're never storing this value, but that's not the problem


            //This "lv" is the main problem: you're asking the LISTVIEW for the TextView
            //That's why you always get the first one. You should be asking the current view (v) instead
            TextView tv = (TextView) lv.findViewById(R.id.dummy); 
            url = (String) tv.getTag();

            LLog.i("tag", "ID:" + id + "URL: " + url + " selected");
            Intent i = new Intent(List_AC.this, DocView.class);
            i.putExtra("url", url);
            startActivity(i);
        }
    });

但是,为什么你要尝试使用持有者中的数据而不是获取支持关系?

OK your onItemClick has a few things wrong (see inline comments):

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int pos,
                long id) {

            String url = "";
            lv.getItemIdAtPosition(pos); //you're never storing this value, but that's not the problem


            //This "lv" is the main problem: you're asking the LISTVIEW for the TextView
            //That's why you always get the first one. You should be asking the current view (v) instead
            TextView tv = (TextView) lv.findViewById(R.id.dummy); 
            url = (String) tv.getTag();

            LLog.i("tag", "ID:" + id + "URL: " + url + " selected");
            Intent i = new Intent(List_AC.this, DocView.class);
            i.putExtra("url", url);
            startActivity(i);
        }
    });

However, why are you trying to use the data in the holder instead of getting the backing tiem?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文