我有一个由数据库填充的ListView。 ListView包含2列,“name”和“venue”。在我的旋转器中,我有4个选项,“艺术家 - 升序”,“艺术家 - 降序”,“场地 - 升序”,“场地 - 降序”。
我的问题是,是否可以根据在微调器中选择的选项更改列表的打印顺序?
到目前为止,这是我的代码。目前,微调器只显示4个选项。
更新 我根据评论的建议更新了我的代码。我现在在我的DBManager类中有4个不同的getAllConcert()方法,在我的MainActivity中有一个switch语句,根据所选的微调器项调用每个方法。以下是它当前的作用:当您从微调器中选择一个项目时,列表不会更改,但是当您单击列表项并返回到初始列表屏幕时,列表将根据先前选择的微调项目进行更新。
任何人都可以告诉我如何在选择微调项目时更新列表吗?
package com.example.msdproject;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.view.View;
import java.sql.SQLException;
import android.util.Log;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
public class MainActivity extends ListActivity {
DBManager db = new DBManager(this);
String [] sortBy;
Spinner spinner1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
sortBy = getResources().getStringArray(R.array.sortBy);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, sortBy);
spinner1.setAdapter(spinnerAdapter);
ImageButton add = (ImageButton)findViewById(R.id.addConcertButton);
add.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent(MainActivity.this, AddArtist.class);
startActivity(i);
}
});
addData();
}
@Override
public void onResume()
{
super.onResume();
addData();
}
public void addData()
{
long spinnerId = spinner1.getSelectedItemId();
int spinnerInt = (int) spinnerId;
Log.d("test", "Spinner ID = " + spinnerInt);
switch (spinnerInt)
{
case 0:
try
{
db.open();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row_layout,
db.getAllConcertsNameASC(),
new String[] { "name", "venue" },
new int[] { R.id.listName, R.id.listDate });
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
catch (SQLException e)
{
e.printStackTrace();
}
db.close();
break;
case 1:
try
{
db.open();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row_layout,
db.getAllConcertsNameDESC(),
new String[] { "name", "venue" },
new int[] { R.id.listName, R.id.listDate });
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
catch (SQLException e)
{
e.printStackTrace();
}
db.close();
break;
case 2:
try
{
db.open();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row_layout,
db.getAllConcertsVenueASC(),
new String[] { "name", "venue" },
new int[] { R.id.listName, R.id.listDate });
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
catch (SQLException e)
{
e.printStackTrace();
}
db.close();
break;
case 3:
try
{
db.open();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row_layout,
db.getAllConcertsVenueDESC(),
new String[] { "name", "venue" },
new int[] { R.id.listName, R.id.listDate });
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
catch (SQLException e)
{
e.printStackTrace();
}
db.close();
break;
}
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
try
{
db.open();
Cursor c = db.getConcert(id);
String string_id = c.getString(0);
long long_id = Long.parseLong(string_id);
Log.d("test", "id passed = " + long_id);
Intent i = new Intent(MainActivity.this, ViewArtist.class);
i.putExtra("id", long_id);
startActivity(i);
}
catch (SQLException e)
{
e.printStackTrace();
}
db.close();
}
}
DBManager类:
package com.example.msdproject;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import java.sql.SQLException;
public class DBManager {
public static final String COL_ROWID = "_id";
public static final String COL_NAME = "name";
public static final String COL_VENUE = "venue";
public static final String COL_DATE = "date";
public static final String COL_COMMENTS = "comments";
private static final String DB_NAME = "Concerts";
private static final String DB_TABLE = "Concert_Info";
private static final int DB_VERSION = 1;
private static final String DB_CREATE =
"create table " + DB_TABLE +
" (_id integer primary key autoincrement, " +
"name text not null, " +
"venue text not null, " +
"comments text not null, " +
"date text not null);";
private Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBManager(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
public static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
/*
//taken from YouTube Tutorial video
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("drop table if exists Concert_Info");
onCreate(db);*/
}
}
public DBManager (){
}
public DBManager open() throws SQLException
{
db = DBHelper.getWritableDatabase();
//db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE + ";");
return this;
}
public void close()
{
DBHelper.close();
}
public long insertConcert(String name, String venue, String date, String comments)
{
ContentValues initialValues = new ContentValues();
initialValues.put(COL_NAME, name);
initialValues.put(COL_VENUE, venue);
initialValues.put(COL_DATE, date);
initialValues.put(COL_COMMENTS, comments);
return db.insert(DB_TABLE, null, initialValues);
}
public boolean deleteConcert(String name)
{
return db.delete(DB_TABLE, "name='"+ name+"'", null) > 0;
}
public Cursor getAllConcertsNameASC() {
return db.query(DB_TABLE, new String[]
{
COL_ROWID,
COL_NAME,
COL_VENUE,
COL_DATE,
COL_COMMENTS
},
null,
null,
null,
null,
COL_NAME+ " ASC"
);
}
public Cursor getAllConcertsNameDESC() {
return db.query(DB_TABLE, new String[]
{
COL_ROWID,
COL_NAME,
COL_VENUE,
COL_DATE,
COL_COMMENTS
},
null,
null,
null,
null,
COL_NAME+ " DESC"
);
}
public Cursor getAllConcertsVenueASC() {
return db.query(DB_TABLE, new String[]
{
COL_ROWID,
COL_NAME,
COL_VENUE,
COL_DATE,
COL_COMMENTS
},
null,
null,
null,
null,
COL_VENUE+ " ASC"
);
}
public Cursor getAllConcertsVenueDESC() {
return db.query(DB_TABLE, new String[]
{
COL_ROWID,
COL_NAME,
COL_VENUE,
COL_DATE,
COL_COMMENTS
},
null,
null,
null,
null,
COL_VENUE+ " DESC"
);
}
public Cursor getConcert(long ROW_ID) throws SQLException
{
Cursor mCursor =
db.query(DB_TABLE, new String[]
{
COL_ROWID,
COL_NAME,
COL_VENUE,
COL_DATE,
COL_COMMENTS
},
COL_ROWID + "=" + ROW_ID,
null,
null,
null,
null
);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateConcert(String name, String venue, String date, String comments)
{
ContentValues updateValues = new ContentValues();
updateValues.put(COL_NAME, name);
updateValues.put(COL_VENUE, venue);
updateValues.put(COL_DATE, date);
updateValues.put(COL_COMMENTS, comments);
return db.update(DB_TABLE, updateValues, "name='"+ name+"'", null) > 0;
}
public void reset () throws SQLException {
db.delete(DB_TABLE, null, null);
db.close();
this.DBHelper.onCreate(this.db);
}
}