ListView .putExtra from DB Column

我怕爱的太早我们不能终老 提交于 2019-12-18 09:41:47

问题


I am trying to get data from a db column that contains an URL link; The column name is 'goto' w/in the db. I need to pass this into my onItemClick Intent in my ListView to load a webpage into the new/next Activity.

I don't know how to get this to function. If you can please show code modifications with an explanation, this would be very helpful to me learning. Thnx!

REVISED:

I am now getting data passed but my method is returning the wrong row ID. I revised the below Activity. Any help Plz. THNX

Within my Activities .onCreate (REVISED):

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

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

回答1:


Try to use setTag:
At first, we set tag:

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

And when perform onClick event, get tag:

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



回答2:


In OnItemClickListener.onItemClick method, there is an id arguments, while you are using SimpleCursorAdapter, the id, which is _ID column in sqlite database, of selected item will be passed to the id argument, so you can get the selected column from DB with this id value.

Following is the sample code, hope it can help.

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



回答3:


The reason you're pulling data from the wrong row is because you're handling the convertView parameter incorrectly. You're only ever setting the holder to point at the very first set of TextView objects you set up. I think that your holder concept is unnecessary. Lose that, set text fields explicitly each time, use the Cursor as I mentioned in your previous question.



来源:https://stackoverflow.com/questions/5838765/listview-putextra-from-db-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!