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

标签 php android mysql listview

我有一个从 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/

相关文章:

php - 如何在 while 循环中获取某些数据而不重新分配

php - 使用资源在我的 json 响应中返回许多对象

java - android firebase数据库如何从子节点检索所有数据

mysql - SQL - 找到最小的未使用数字

PHP:如何避免在特定情况下使用 extract()

php - 需要检查用户在我的网站 Laravel 5.2 上注册了多长时间

php - 使用 PHP 的 Microsoft Dynamics GP 10 Web 服务

android - Parse.com 身份验证错误 : Unable to respond to any of these challenges: {oauth=www-authenticate: OAuth realm ="https://api.twitter.com"}

java - Android Studio 表格布局中增加列

用于选择修复行数 "families"的 SQL