gpt4 book ai didi

php - 每隔一定时间用从 mysql 检索到的数据刷新 ListView

转载 作者:行者123 更新时间:2023-11-30 22:15:32 25 4
gpt4 key购买 nike

我有一个从 MySQL 检索项目并显示它的 ListView ,我的问题是我不知道如何每隔特定时间用 MySQL 表中的新更新值刷新此 ListView

这是我的代码

public class Hospital_Reply extends Activity implements
DownloadResultReceiver.Receiver {
private DownloadResultReceiver mReceiver;
List<Hos> hoses;
String carNumber;
private GoogleApiClient client;


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hospital__reply);
Bundle bundle = this.getIntent().getExtras();
carNumber = bundle.getString("carNumber");
String url =url = "http://192.168.1.102/final/notification/read.php";

if (true) {
mReceiver = new DownloadResultReceiver(new Handler());
mReceiver.setReceiver(this);
Intent intent = new Intent(Intent.ACTION_SYNC, null, this,
DownloadService.class);

intent.putExtra("url", url);
String data = null;
try {
data = URLEncoder.encode("carNumber", "UTF-8") + "=" + URLEncoder.encode(carNumber, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
intent.putExtra("data", data);
intent.putExtra("receiver", mReceiver);
intent.putExtra("requestId", 101);
startService(intent);
} else {
new AlertDialog.Builder(Hospital_Reply.this)
.setTitle("Oops")
.setMessage("No Internet Connection!")
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).show();
}
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

@Override
public void onStart() {
super.onStart();
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, "Car Page", Uri.parse("http://host/path"),
Uri.parse("android-app://com.example.khloud.qery/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
super.onStop();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, "Car Page", Uri.parse("http://host/path"),
Uri.parse("android-app://com.example.khloud.qery/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}



@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
switch (resultCode) {
case DownloadService.STATUS_RUNNING:
break;
case DownloadService.STATUS_FINISHED:
String response = resultData.getString("result");
if (response == null)
Log.d("respooo", "null");
intializeProcess(response);
break;
case DownloadService.STATUS_ERROR:
String error = resultData.getString("error");
Log.d("error", error);
break;
}
}

private void intializeProcess(String response) {
Log.d("response", response);
ItemsRetrieval_hos itemRetrieval = new ItemsRetrieval_hos();
if (itemRetrieval.GetMainAPI(response)) {
hoses = itemRetrieval.getItems();
Log.e("Items Size", hoses.size() + "");
doWork();
}
}





private void doWork() {
populateListView();
registerClickCallback();
}

private void populateListView() {
ArrayAdapter<Hos> adapter = new MyListAdapter(this,hoses);
ListView list = (ListView) findViewById(R.id.items);
list.setAdapter(adapter);
}

private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.items);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(final AdapterView<?> parent, View viewClicked,
final int position, long id) {

}
});
}


public class MyListAdapter extends ArrayAdapter<Hos> {

public List<Hos> appInfoList;
private Context ctx;
private LayoutInflater mInflater;
public MyListAdapter(Context context, List<Hos> myList) {
super(context, NO_SELECTION);
this.appInfoList = myList;
this.ctx=context;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return appInfoList.size();
}



@Override
public View getView(final int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.single_row_reply, null);
TextView textView = (TextView) convertView.findViewById(R.id.textView);
final Hos item = hoses.get(position);
textView.setText(item.getName());

return convertView;
}
}

@Override
protected void onResume() {

super.onResume();
this.onCreate(null);
}

}

最佳答案

为了刷新数据,您需要使用 BaseAdapter.notifyDataSetChanged() .

要定期执行此操作,您可以创建新的 Timer并选择时间间隔:

//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {
//Called each time when 1000 milliseconds (1 second) (the period parameter)
}

},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);

完成后调用t.cancle()取消任务。

关于php - 每隔一定时间用从 mysql 检索到的数据刷新 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38421150/

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