map json android,android - Hashmap to JSON Array - Stack Overflow

I would like to ask if someone here knows how to convert a HashMap to JSON Array to be used on a Adapter. The logic is when you logged in, it will fetch data on a web server using Volley, store it on SQLite database and at the same time it will show the retrieve data on a custom listview. The thing is, if you will move to another fragment on the application and then go back, it will request again on the volley which it will take time. I wanted that if you will go back the data stored on the SQLite database will be retrieved and if the user wanted to update the data using SwipeRefresh, that's the time to update for new data.

Here is my code.

public void LoadMarkets()

{

MaptmpRate = new HashMap();

RateDb db = new RateDb(getActivity().getApplicationContext());

Cursor rs = db.getData();

if(rs!=null && rs.getCount()>0)

{

if(rs.moveToFirst())

{

while(rs.moveToNext())

{

tmpRate.put("rateID",rs.getString(rs.getColumnIndex(RateDb.rateID)));

tmpRate.put("Name",rs.getString(rs.getColumnIndex(RateDb.rateName)));

tmpRate.put("Rate",rs.getString(rs.getColumnIndex(RateDb.rateRate)));

tmpRate.put("Date",rs.getString(rs.getColumnIndex(RateDb.rateDate)));

tmpRate.put("Time",rs.getString(rs.getColumnIndex(RateDb.rateTime)));

tmpRate.put("Ask",rs.getString(rs.getColumnIndex(RateDb.rateAsk)));

tmpRate.put("Bid",rs.getString(rs.getColumnIndex(RateDb.rateBid)));

tmpRate.put("Balance",rs.getString(rs.getColumnIndex(RateDb.rateBalance)));

//Log.d("MyDebug",rs.getString(rs.getColumnIndex(RateDb.rateName)));

}

}

}

rs.close();

db.close();

JSONObject jsonObject = new JSONObject(tmpRate);

JSONArray jsonArray = new JSONArray();

jsonArray.put(jsonObject);

RowAAdapter rowAAdapter = new RowAAdapter(getActivity(),jsonArray);

lvMarkets.setAdapter(rowAAdapter);

swipeRefreshLayout.setRefreshing(false);

}

The problem is that when i load this function, it only shows the last data.

I hope you can help me with my problem.

Thank you very much.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值