每隔一段时间用mysql中检索到的数据刷新列表视图

时间:2016-07-17 12:25:45

标签: php android mysql listview

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

这是我的代码

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);
}

}

1 个答案:

答案 0 :(得分:0)

要刷新数据,您需要使用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()取消任务后。