【发布时间】:2015-07-02 11:02:38
【问题描述】:
我有一个自定义的ListView,其中显示了我从服务器下载的jsonArray 的元素。现在我希望用户能够“喜欢”条目。因此,当他点击ListItem 时,服务器的数据库会更新,因此点赞数会增加。为了避免用户喜欢无限次,我在 SQLite 数据库中存储了一行,其位置为ListItem。在服务器数据库中有新条目并且位置发生变化之前,这可以正常工作。我怎么解决这个问题?是否有可能从 ListItem 获取 id 而不是位置或其他东西?
这是我的代码:
getALlEntrysListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = getLikes(dbh);
cursor.moveToFirst();
if (cursor.moveToFirst()) {
do {
/*check if Item is liked (=1) or not (=0)*/
if (Integer.parseInt(cursor.getString(2)) == 1) {
dbh.updateLike(dbh, position, Integer.parseInt(cursor.getString(2)), 0);
dislike(position);
cursor.close();
} else if (Integer.parseInt(cursor.getString(2)) == 0) {
dbh.updateLike(dbh, position, Integer.parseInt(cursor.getString(2)), 1);
likeUpload(position);
cursor.close();
} else {
dbh.addLike(dbh, position, 1);
likeUpload(position);
cursor.close();
}
} while (cursor.moveToNext());
}
/*increase numer of likes on the Server Database (the dislike method works the same way):*/
public void likeUpload (int position){
try {
JSONObject entryClicked = jsonArray.getJSONObject(position);
entryID = entryClicked.getString("id");
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", entryID));
new AsyncTask<ApiConnector, Long, Boolean>() {
@Override
protected Boolean doInBackground(ApiConnector... apiConnectors) {
return apiConnectors[0].like(params);
}
}.execute(new ApiConnector());
finish();
startActivity(getIntent());
【问题讨论】:
-
而不是使用位置来了解用户是否喜欢它,而是使用此列表视图“后面”的对象的 id。
标签: android sqlite android-listview listitem