我在网上发现了很多方法,但是他们没有帮助我,我的问题是如何在不获取重复条目的情况下将项目添加到列表视图 我的PHP代码是
$result1 = mysql_query("select * from headlines where categoryID = '$category' and newID = '$hashtagID' order by id DESC", $db->connect());
$count = mysql_num_rows($result1);
if($count != 0){
$response["success"] = 1;
while($row1 = mysql_fetch_assoc($result1)){
//getting user
$result2 = mysql_query("select * from members where id = '".$row1['memberID']."'", $db->connect());
while($row2 = mysql_fetch_assoc($result2)){
$commentAuthor = $row2['last_name'];
}
$response['trends'][] = array('body'=>$row1['body'], 'name'=>$commentAuthor);
}
echo json_encode($response);
}else{
$response["success"] = 0;
$response['trends'][] = array('body'=>'0', 'name'=>'0');
echo json_encode($response);
}
}
以及下面的java代码使用异步任务执行该背景
UserFunctions userfunction = new UserFunctions();
JSONObject json = userfunction.loadhash(currenthash,
categoryID);
try {
if (json.get(KEY_SUCCESS) != null) {
String rs = json.getString(KEY_SUCCESS);
if (Integer.parseInt(rs) == 1) {
istweetempty = true;
JSONArray arrayjson = json.getJSONArray("trends");
for (int i = 0; i < arrayjson.length(); i++) {
JSONObject newobject = arrayjson.getJSONObject(i);
tweepList.add(0, newobject.getString("name")
.toString());
tweetList.add(0, newobject.getString("body")
.toString());
}
} else {
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我的问题就在这里,如果我将list_trending.setAdapter(tweets_adapter);
与tweets_adapter.notifyDataSetChanged();
一起使用,我的列表视图中会出现重复的条目,如果我使用tweets_adapter.notifyDataSetChanged();
,则listview中的任何内容都不会发生服务器不会出现。我哪里可能出错。
@Override
protected void onPostExecute(String[] result) {
if (istweetempty == true) {
tweepArray = tweepList.toArray(new String[tweepList.size()]);
tweetArray = tweetList.toArray(new String[tweetList.size()]);
tweets_adapter = new tweets(Trending.this, tweepArray,
tweetArray);
//list_trending.setAdapter(tweets_adapter);
tweets_adapter.notifyDataSetChanged();
}
list_trending.onRefreshComplete();
super.onPostExecute(result);
}
答案 0 :(得分:1)
tweets_adapter.notifyDataSetChanged();