获取ListItem Id

时间:2015-07-02 11:02:38

标签: android sqlite android-listview listitem

我有一个自定义ListView,其中我显示了从服务器下载的jsonArray中的elementss。现在我希望用户能够"喜欢"条目。因此,当他点击ListItem时,服务器的数据库会更新,因此Likes的数量会增加。为了避免用户喜欢infinte times,我在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());

2 个答案:

答案 0 :(得分:0)

列表没有自己的身份证明。它默认为位置。您的基础项必须具有唯一的ID,如果有,您可以覆盖getItemId方法以返回该Id。

 public class YourListAdapter extends ArrayAdapter<CustomPojo> {

 ....
 @Override
        public long getItemId(int position) {
            if(listOfData!=null) {
                return Long.valueOf(listOfData.get(position).getId());
            }
            return -1;
        }
.....

}

答案 1 :(得分:0)

尝试这种方式可以帮助您

@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
        long id) {
    // TODO Auto-generated method stub

    switch (adapter.getId()) {
    case R.id.listView:
            YourVOClass yourVOClass = (YourVOClass) listView
                    .getItemAtPosition(position);
                    }
                }