点击列表查看项目错误@ ID#0x5?

时间:2011-05-03 00:23:38

标签: android listview webview adapter onitemclicklistener

我收到错误'找不到资源异常:字符串资源ID#0x5。我知道它与i.putExtra(...)有关。我试图从数据库列'gotoURL'中拉出一个字符串,将URL字符串从ListView中的listItem传递给WebView Activity。任何帮助PLZ!日Thnx!

我的活动:

    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {
        // @Override
        public void onItemClick(AdapterView<?> a, View v, int position,long id) 
        {
            Object o = lv.getItemAtPosition(position);
            Toast.makeText(List_AC.this, "Clicked!", Toast.LENGTH_LONG).show();
            Intent i = new Intent(List_AC.this, DocView.class);
            Cursor cursor = Adapter_AC.dataCursor;
            i.putExtra("url", getString(cursor.getColumnIndexOrThrow("gotoURL")));
            startActivity(i);
        }
    });

适配器:

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

    return convertView;
}

static class ViewHolder {
    TextView text1;
    TextView text2;
    TextView text3;
    TextView text4;
    protected static final String KEY_TITLE = "text4";
}

}

1 个答案:

答案 0 :(得分:1)

问题可能在onItemClickListner:

  

i.putExtra(“url”,getString(cursor.getColumnIndexOrThrow(“gotoURL”)));

你要求columnindex(一个整数),用它作为Context.getString()的输入,它希望这是一个(现有的)资源ID。根据您的错误,我可以猜测“gotoURL”的columnIndex为5,并且不存在此类资源ID ...

我想你真正想要放在意图中的是“gotoURL”列的字符串值,就像在适配器中那样?