Domanda

In my MainActivity I have a ListView that shows all the elements from a SQLite Database and it works fine. Now I want to add a context menu so when I press an item, I can modify or delete the element. The problem is that the items of the ListView aren't clickable and I don't know why. What is my problem? Maybe the Deprecations I have in showEvents() and in addPlayer()? Thanks for the help!

MainActivity

public class MainActivity extends ListActivity {
private EventsData events;

private static int[] TO = { R.id.rowid, R.id.nome, R.id.valore, R.id.ruolo, R.id.numero, };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    events = new EventsData(this);
    try {
        Cursor cursor = getEvents();
        showEvents(cursor);
    } finally {
        events.close();
    }
}

private static String[] FROM = { _ID, NAME, VALUE, POSITION, NUMBER };
private static String ORDER_BY = NAME + " ASC" ;

private Cursor getEvents() {
    // Perform a managed query. The Activity will handle closing
    // and re-querying the cursor when needed.
    SQLiteDatabase db = events.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
    startManagingCursor(cursor);
    return cursor;
}

private void showEvents(Cursor cursor) {
    // Set up data 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.item, cursor, FROM, TO);
    setListAdapter(adapter);
}

public void addPlayer(View view) {
    startActivity(new Intent(this, AddingActivity.class));
}
}

EventsData

public class EventsData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "events.db";
private static final int DATABASE_VERSION = 1;

/** Create a helper object for the Events database */ 
public EventsData(Context ctx) {
    super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
    } 
@Override 
public void onCreate(SQLiteDatabase db) {
    db.execSQL( "CREATE TABLE " + TABLE_NAME + " (" + _ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME
            + " TEXT NOT NULL," + VALUE + " TEXT NOT NULL," + POSITION
            + " TEXT NOT NULL," + NUMBER + " TEXT);" );
}

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
            int newVersion) {
        db.execSQL( "DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

Constants

public interface Constants extends BaseColumns {
public static final String TABLE_NAME = "events" ;
// Columns in the Events database 
public static final String NAME = "nome" ;
public static final String VALUE = "valore" ;
public static final String POSITION = "ruolo" ;
public static final String NUMBER = "numero" ;
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffffff" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="addPlayer"
        android:text="@string/add_player"
        android:textSize="20sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/rowid_h1"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:background="#ffaaaaaa"
        android:gravity="center"
        android:paddingLeft="0dp"
        android:text="N°"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/rowidcolon_h1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/rowid_h1"
        android:background="#ffaaaaaa"
        android:text="| "
        android:textSize="15sp" />

    <TextView
        android:id="@+id/nome_h1"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/rowidcolon_h1"
        android:background="#ffaaaaaa"
        android:text="Nome"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/valore_h1"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/nome_h1"
        android:background="#ffaaaaaa"
        android:gravity="center"
        android:text="Valore"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/ruolo_h1"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/valore_h1"
        android:background="#ffaaaaaa"
        android:gravity="center"
        android:text="Ruolo"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/numero_h1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/ruolo_h1"
        android:background="#ffaaaaaa"
        android:ellipsize="end"
        android:gravity="center"
        android:singleLine="true"
        android:text="Telefono"
        android:textSize="15sp" />
</LinearLayout>



<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/empty" />

</LinearLayout>
È stato utile?

Soluzione

in your XML file delete the listview's clickable property. And in your Main Activity change like this.

public class MainActivity extends ListActivity {
private EventsData events;

private static int[] TO = { R.id.rowid, R.id.nome, R.id.valore, R.id.ruolo, R.id.numero, };



 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    ListView mylist = (ListView)findViewById(R.id.yourlist);
    mylist.setonitemclicklistener(New OnitemclickListener){
      @override
       OnitemClick(){
      //Your Codes here
            }
          }

    events = new EventsData(this);
    try {
        Cursor cursor = getEvents();
        showEvents(cursor);
    } finally {
        events.close();
    }
}

private static String[] FROM = { _ID, NAME, VALUE, POSITION, NUMBER };
private static String ORDER_BY = NAME + " ASC" ;

private Cursor getEvents() {
    // Perform a managed query. The Activity will handle closing
    // and re-querying the cursor when needed.
    SQLiteDatabase db = events.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
    startManagingCursor(cursor);
    return cursor;
}

private void showEvents(Cursor cursor) {
    // Set up data 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.item, cursor, FROM, TO);
    setListAdapter(adapter);
}

public void addPlayer(View view) {
    startActivity(new Intent(this, AddingActivity.class));
}
}

I cant use Eclipse now but define your listview on oncreate method and use lise onitemclicklistener.

Altri suggerimenti

you need to implement the onItemCLickListener, and add a selector to the listview so your clicks highlight the row see here

protected void onListItemClick(ListView l, View v, int position, long id) {
        // Do Stuff
     }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top