简体   繁体   中英

How to start ListActivity from Activity (Intent) WITHOUT ListView?

I want to start a ListActivity from a normal Activity, but the ListActivity.xml doesn't contain a listview object, instead it has a tablelayout and a tablerow.

Here's the XML:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tLayoutLR"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TableRow
    android:id="@+id/trRowLR"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:ignore="UselessParent" >

    <TextView
        android:id="@+id/tvTextoLR"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ID \t Nombre \t\t\t\t\t\tDescripción"
        tools:ignore="HardcodedText" />

</TableRow>

In my main activity, I try to start the ListActivity, but it fails. Here is the code:

final Button btnListas = (Button) findViewById(R.id.button);
    btnListas.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(ActividadInicial.this, MainActivity.class);
            startActivity(i);
        }
    });

What I'm doing here is filling the TableRows with some information extracted from a SQLite database. I created a class for, in this case songs, and also created a class to implement the array list, so I can easily insert, update, delete, etc. Here's the class I used:

public class C_Lista extends ArrayList <Song> {

private Context _Contexto;

public C_Lista(Context _Contexto) {
    this._Contexto = _Contexto;
}

// Method that fills
public void LoadSongs()
{
    try{
        this.clear();
        String strNombreBD = "BDMusica";
        BDMusica con = new BDMusica(_Contexto,strNombreBD, null, 1);
        SQLiteDatabase db = con.getWritableDatabase();
        String strSQL = "Select ID_Song, Nombre, Cantante, Year, Album From Songs ";

        Cursor cursor = db.rawQuery(strSQL, null);

        if(cursor.moveToFirst())
        {
            do{
                Song objA = new Song(_Contexto);
                objA.setID(cursor.getInt(0));
                objA.setNombre(cursor.getString(1));
                objA.setCantante(cursor.getString(2));
                objA.setYear(cursor.getInt(3));
                objA.setAlbum(cursor.getString(4));

                this.add(objA);
            }while (cursor.moveToNext());
        }
        cursor.close();
        db.close();

    }
    catch(Exception e)
    {
        Log.e("Archivos Leer: ", "JALQ" + e.getMessage(), e);
    }
}

Hope you can help me. Thanks in advance.

The documentation of ListActivity says clearly:

your own view MUST contain a ListView object with the id "@android:id/list"

As your layout doesn't contain that, the Activity crashes, presumely when doing a getViewById(android.R.id.ist)

I suggest you never use ListActivity. The ListActivity just looks for @android:id/list in the xml and helps you get the ListView procedures directly from the Activity.

Instead, use the usual activity - if you want to put a list in it, just put it in the xml, name it anyway you like, find it using Activity.findViewById and use its procedures directly.

What you gain from it? When you use the same Activity type for all your screens, you can make a parent Activity with useful procedures (such as hide/show keyboard etc.) so your class structure would look like Activity -> MyParentActivity-> MyScreenActivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM