在我的MainActivity中,我有一个ListView,它显示了SQLite数据库中的所有元素,并且工作正常。现在我想添加一个上下文菜单,所以当我按下一个项目时,我可以修改或删除该元素。问题是ListView的项目不可点击,我不知道为什么。我的问题是什么?也许我在showEvents()和addPlayer()中有过Deprecations?谢谢你的帮助!
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);
}
}
常量
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>
答案 0 :(得分:0)
您需要实现onItemCLickListener,并向listview添加一个选择器,以便您的点击突出显示该行,请参阅here
protected void onListItemClick(ListView l, View v, int position, long id) {
// Do Stuff
}
}
答案 1 :(得分:0)
删除listview的可点击属性。并在您的主要活动中更改此类。
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));
}
}
我现在无法使用Eclipse,但是在oncreate方法上定义了listview并使用了lise onitemclicklistener。