我最近在做外汇项目,用的是ListView视图来显示外汇数据,我的数据是从服务器中通过socket得到数据,但是我没有思路在ListView中实时更新外汇数据,服务器发送数据过来,通过过滤自己想要的货币对数据实时更新数据,有的货币对数据是不会变化的,只是哪几对有变化就更新。望大侠提出一些详细的解决思路。我下面的代码可以从服务器中得到显示出来,现在是在 ListView实时更新数据不能得到解决。
代码如下:
package com.jhconn.KVB_android.main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.jhconn.KVB_android.util.CCDB_Helper;
public class ShowCurrencyPair extends Activity {
private final static String[] currencyPair = new String[] { "USDCHF",
"GBPUSD", "USDJPY", "EURUSD", "USDCAD", "EURGBP", "EURJPY",
"GBPJPY", "GBPCHF", "NZDUSD", "NZDJPY", "NZDAUD", "AUDUSD",
"AUDJPY", "AUDNZD", "EURAUD", "NZDCHF", "AUDCHF", "USDCNY" };
private final String DEBUG_TAG = "showcurrencyPair";
private String msg = null;
private PrintWriter out = null;
private BufferedReader br = null;
private String[] s = new String[100];
private int[] to = null;
private String[] from = null;
private Socket socket = null;
private int j = 0;
private Boolean iscurrencypair = false;
private static final int SERVERPORT = 15000;
private static final String SERVERIP = "192.168.9.10";
private ListView lv = null;
private List<HashMap<String, String>> fillMaps = null;
private Cursor cursor = null;
private static final String TABLE_RATE = "rates";
private DataBaseLayer dataBaseLayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currencypair);
setTheme(R.style.Transparent);
lv = (ListView) findViewById(R.id.listview);
from = new String[] { "rowid", "forex", "bid", "ask" };
to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };
fillMaps = new ArrayList<HashMap<String, String>>();
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
String message = "request" + "\r\n";
try {
// 创建Socket
socket = new Socket(SERVERIP, SERVERPORT);
// 向服务器发送消息
out = new PrintWriter(new BufferedWriter(