gpt4 book ai didi

php - 如何自动刷新(添加/更新) ListView 项而不重复数据?

转载 作者:行者123 更新时间:2023-11-29 11:54:07 25 4
gpt4 key购买 nike

我编写了一个简单的代码来从 mysql 数据库检索一些数据并将它们放入 android listview 中,一切正常,但旧项目保留在 ListView 中,并添加新项目(数据重复),下面是 php 代码:

$sql = "SELECT * FROM persons";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'address'=>$row[2]
));
}

echo json_encode(array("result"=>$result));

mysqli_close($con);

这里是 android 代码:

    public class FetchData extends Activity{
String myJSON;

private static final String TAG_RESULTS="result";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_ADD ="address";

JSONArray peoples = null;

ArrayList<HashMap<String, String>> personList;

ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
final Handler handler = new Handler();
handler.postDelayed( new Runnable() {

@Override
public void run() {


refreshData();
handler.postDelayed( this, 2 * 100 );
}
}, 2 * 100 );

}
public void refreshData(){

}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{

@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://192.168.1.4/get-data.php");

// Depends on your web service
httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}

@Override
protected void onPostExecute(String result){
myJSON=result;

showdata();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
protected void showdata(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);

for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String address = c.getString(TAG_ADD);

HashMap<String,String> persons = new HashMap<String,String>();

persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);

personList.add(persons);
}

ListAdapter adapter = new SimpleAdapter(
FetchData.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);

list.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}

}


}

如果特定项目的 mysql 数据库数据发生更改,我希望自动更新特定 ListView 项目,并添加新项目而不重复

最佳答案

尝试像这样修改您的 showdata():

    protected void showdata(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);

personList.clear();
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String address = c.getString(TAG_ADD);

HashMap<String,String> persons = new HashMap<String,String>();

persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);

personList.add(persons);
}

ListAdapter adapter = new SimpleAdapter(
FetchData.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);

list.setAdapter(adapter);

} catch (JSONException e) {
e.printStackTrace();
}

}

personList.clear(); - 用于清理旧结果。

关于php - 如何自动刷新(添加/更新) ListView 项而不重复数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33501399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com