我有问题,我想请你帮忙。我想使用一个简单的游标适配器,从几列中的listView中的SQLite数据库数据中获取视图。我自己做了一个列表视图(row_players.xml):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form></form>
<div id="something"></div>
然后在DatabaseHelper中我创建了一个从数据中收集的扩展列表:
FeedbackForm
最后,我尝试使用简单光标适配器显示列表中的数据:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/id"
android:layout_column="0"
android:padding="0dp"
android:layout_span="2"
/>
<TextView
android:id="@+id/playerNameListView"
android:layout_column="1"
android:padding="0dp"
/>
</TableRow>
</TableLayout>
当我尝试运行活动时,我遇到以下错误:
public ArrayList<HashMap<String, String>> getAllPlayers() {
try {
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
SQLiteDatabase db = this.getReadableDatabase();
String sqlSelect = "SELECT " + _id + ", " +
columnPlayerName + " FROM " + TABLE_NAME;
Cursor select = db.rawQuery(sqlSelect, null);
if(select != null) {
if(select.moveToFirst()) {
do {
map = new HashMap<String, String>();
map.put("id", select.getString(0));
map.put("name", select.getString(1));
arraylist.add(map);
}
while(select.moveToNext());
}
}
select.close();
db.close();
return arraylist;
}
catch (Exception E) {
return null;
}
}
请帮助:(
--- --- EDIT activity_manage_players.xml:
public class ManagePlayers extends AppCompatActivity {
DatabaseHelperPlayer playerDb = new DatabaseHelperPlayer(this);
ListView playersList;
ArrayList<HashMap<String, String>> playerList = playerDb.getAllPlayers();
private void playersListView() {
playersList = (ListView) findViewById(R.id.listPlayer);
SimpleAdapter adt = new SimpleAdapter(this, playerList, R.layout.row_players, new String[]{"id", "name"}, new int[]{R.id.id, R.id.playerNameListView});
playersList.setAdapter(adt);
//adapter = new ArrayAdapter<PlayersList>(this, R.layout.row_players, R.id.id, playerDb.playersList());
//playersList.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_players);
playersListView();
}
}