Pregunta

Tengo una vista de lista que poca datos de un DB en una vista compleja de LISTVIEW. Una cadena en la columna 'Gotourl' de la DB es una URL para cargar un enlace en una WebView utilizando la intención de PutExtra. Cuando hago clic en una lista de listem para ir a la siguiente actividad, mi método toma los datos de cadena correctos, pero extrae la cadena de la fila de DB incorrecta. Según mi 'log.i (...);' Indica en el DDMS, se selecciona la fila de identificación correcta, pero la cadena de la columna se extrae de la primera ID de fila y no de la ID de fila seleccionada (???): lo hace en cualquier listitem seleccionado.

No puedo entender cómo escribir esto para funcionar correctamente. Por favor ayuda con el código de ejemplo. Gracias.

Mi actividad:

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

Mi adaptador:

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

}
¿Fue útil?

Solución

Ok, su OnitemClick tiene algunas cosas mal (ver comentarios en línea):

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

Sin embargo, ¿por qué está tratando de usar los datos en el titular en lugar de obtener el TIEM de respaldo?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top