我是新来的Android,正在一个项目中工作,我有一个API,可为我提供SymbolName(例如:GOLD),BID,ASK等值。 因此,我在Recyclerview中调用了此api。 用户可以添加新符号,就像添加添加在API中传递的新符号一样,我们将获得该符号的详细信息。 数据正确显示,但是我想更改从API接收的Bid and Ask文本颜色。 因此,例如,当我调用API BID值为5时,它将以蓝色显示。在大约1秒钟的间隔后,我将再次调用此API,并且如果从API接收到的新值小于先前的值,则文本颜色应更改为红色,或者如果颜色较大,则为蓝色。 我已经为其编写了代码,并且可以正常工作,直到只有5个符号为止。如果我在API中传递了第6个符号,则逻辑将无法正常工作。
我附上代码供您参考。
我需要致电的片段报价
public class Quotes extends Fragment implements View.OnClickListener {
LinearLayout bottomlayout,v1,v2;
EditText search;
TextView cancel;
ArrayList<QuoteData> quotelist = new ArrayList<>();
List<String> SYSMBOL_ARRAY = new ArrayList<>();
RecyclerView recyclerView;
QuoteAdapter adapter;
ProgressBar progressBar;
SwitchCompat proview,onclicktrade;
DatabaseHelper mydb;
ArrayList<String> list;
String symbollist = "";
String usersymbol;
String value;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
boolean REFRESH_SYSMBOL = false;
Intent mIntent;
static BroadcastReceiverData mBroadcastReceiver;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the popup for this fragment
View view = inflater.inflate(R.layout.fragment_quotes, container, false);
sharedPreferences = this.getContext().getSharedPreferences("switch",MODE_PRIVATE);
editor = sharedPreferences.edit();
mydb = new DatabaseHelper(getContext());
list = mydb.getValues("Symbol_Table");
String val = "";
String symbolname = "";
Toast.makeText(getContext(), "List"+list.size(), Toast.LENGTH_SHORT).show();
for (int i = 0; i < list.size(); i++) {
val = list.get(i);
if (val.equalsIgnoreCase(""))
{
symbolname = val ;
}
else
{
symbolname = symbolname +"," + val ;
}
}
value = symbolname.replace(" ","%20");
usersymbol = Dataholder.url + value;
recyclerView = (RecyclerView)view.findViewById(R.id.lists);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new QuoteAdapter(getActivity(),quotelist);
ItemTouchHelper.Callback callback = new SimpleDragHelper(adapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(recyclerView);
adapter.notifyDataSetChanged();
recyclerView.addItemDecoration(new DividerItemDecoration(ContextCompat.getDrawable(getContext(),R.drawable.item_decorator)));
new QuotesData().execute("http://xx.xxx.xxx.xx/mobileapp-webservice/new/Default.aspx?Action=getDashboardT&token="+usersymbol+"&from=1&to=20");
return view;
}
class QuotesData extends AsyncTask < String, Void, String>{
@Override
protected String doInBackground(String... urls) {
return Dataholder.getApi(urls[0]);
}
@Override
protected void onPostExecute(String result) {
SYSMBOL_ARRAY.clear();
quotelist.clear();
try {
JSONObject jsonObject = new JSONObject(result);
String auth = jsonObject.getString("result");
JSONArray jsonArray =new JSONArray(auth);
for (int i = 0; i<jsonArray.length(); i++)
{
JSONObject key = jsonArray.getJSONObject(i);
if (key.getString("message").equals("success"))
{
String symbol = key.getString("symbol");
String bid = key.getString("bid");
String bidqty = key.getString("bidqty");
String ask = key.getString("ask");
String askqty = key.getString("askqty");
String ltp = key.getString("ltp");
String open = key.getString("open");
String close = key.getString("close");
String high = key.getString("high");
String low = key.getString("low");
String vol = key.getString("vol");
String oi = key.getString("oi");
String change = key.getString("change");
String netchange = key.getString("netchange");
String lotsize = key.getString("lotsize");
String ltt = key.getString("ltt");
String lut = key.getString("lut");
String expiry = key.getString("expiry");
String exchange = key.getString("exchange");
SYSMBOL_ARRAY.add(symbol);
quotelist.add(new QuoteData(symbol,bid,bidqty,ask,askqty,ltp,open,close,high,low,vol,oi,change,netchange,lotsize,ltt,lut,expiry,exchange));
adapter.notifyDataSetChanged();
REFRESH_SYSMBOL = true;
}
else if (key.getString("message").equals("fail")) {
quotelist.clear();
adapter.notifyDataSetChanged();
}
}
progressBar.setVisibility(View.GONE);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
if (REFRESH_SYSMBOL) {
mIntent = new Intent(getActivity(), ServiceQuote.class);
mIntent.putExtra("keyWatchUrl", "http://xx.xxx.xxx.xx/mobileapp-webservice/new/Default.aspx?Action=getDashboardT&token="+usersymbol+"&from=1&to=20");
getActivity().startService(mIntent);
try {
mBroadcastReceiver = new BroadcastReceiverData();
IntentFilter intentFilter = new IntentFilter(Dataholder.ACTION_ACTIVE_QUOTE);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
getActivity().registerReceiver(mBroadcastReceiver, intentFilter);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void setQuotefirst(String result){
try {
JSONObject jsonObject = new JSONObject(result);
String auth = jsonObject.getString("result");
JSONArray jsonArray =new JSONArray(auth);
for (int i = 0; i<jsonArray.length(); i++)
{
JSONObject key = jsonArray.getJSONObject(i);
if (key.getString("message").equals("success"))
{
String symbol = key.getString("symbol");
String bid = key.getString("bid");
String bidqty = key.getString("bidqty");
String ask = key.getString("ask");
String askqty = key.getString("askqty");
String ltp = key.getString("ltp");
String open = key.getString("open");
String close = key.getString("close");
String high = key.getString("high");
String low = key.getString("low");
String vol = key.getString("vol");
String oi = key.getString("oi");
String change = key.getString("change");
String netchange = key.getString("netchange");
String lotsize = key.getString("lotsize");
String ltt = key.getString("ltt");
String lut = key.getString("lut");
String expiry = key.getString("expiry");
String exchange = key.getString("exchange");
for(int j=0;j<SYSMBOL_ARRAY.size();j++){
if (SYSMBOL_ARRAY.get(j).equalsIgnoreCase(symbol)){
quotelist.remove(j);
quotelist.add(j,new QuoteData(symbol, bid, ask, high, low, lut));
adapter.notifyDataSetChanged();
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Handler handlerRefresh = new Handler();
public class BroadcastReceiverData extends BroadcastReceiver {
@Override
public void onReceive(Context context,final Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase(Dataholder.ACTION_ACTIVE_QUOTE)){
handlerRefresh.post(new Runnable() {
@Override
public void run() {
String data = intent.getStringExtra(Dataholder.ACTION_KEY);
setQuotefirst(data);
}
});
}
}
}
}
RecyclerView的适配器代码 这是“比较”和“红色/蓝色”显示的逻辑
public class QuoteAdapter extends RecyclerView.Adapter <QuoteAdapter.MyViewHolder>{
Context context;
List<QuoteData> data;
class MyViewHolder extends RecyclerView.ViewHolder{
TextView time,symbol,sellmax,selllow,buymax,buylow,buy,sell,spread,lowtext,hightext;
LinearLayout layout,layoutbid,layoutask;
double currentbid,lastbid,currentask,lastask;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
time = itemView.findViewById(R.id.TVTime);
symbol = itemView.findViewById(R.id.TVSymbol);
sellmax = itemView.findViewById(R.id.TVSELLMAX);
selllow = itemView.findViewById(R.id.TVSELLLOW);
buymax = itemView.findViewById(R.id.TVBUYMAX);
buylow = itemView.findViewById(R.id.TVBUYHIGH);
buy = itemView.findViewById(R.id.TVBUY);
sell = itemView.findViewById(R.id.TVSELL);
spread = itemView.findViewById(R.id.TVSpread1);
lowtext = itemView.findViewById(R.id.low);
hightext = itemView.findViewById(R.id.high);
layout = itemView.findViewById(R.id.layout);
layoutbid = itemView.findViewById(R.id.LLBid);
layoutask = itemView.findViewById(R.id.LLAsk);
}
}
public QuoteAdapter(Context context,List<QuoteData> data)
{
this.data = data;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.quotelist,viewGroup,false);
return new MyViewHolder(view);
}
@SuppressLint("ResourceAsColor")
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
final QuoteData data1 = data.get(i);
if (i % 2 == 1) {
myViewHolder.layout.setBackgroundColor(Color.parseColor("#fff0f0f0"));
} else
{
myViewHolder.layout.setBackgroundColor(Color.WHITE);
}
Spannable bid = new SpannableString(data1.dBid);
bid.setSpan(new RelativeSizeSpan(1.7f),2,3,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Spannable ask = new SpannableString(data1.dAsk);
ask.setSpan(new RelativeSizeSpan(1.7f),2,3,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myViewHolder.time.setText(data1.dLut);
myViewHolder.symbol.setText(data1.dSymbol);
myViewHolder.sellmax.setText(data1.dBid); //Bid
myViewHolder.selllow.setText(data1.dLow);
myViewHolder.buymax.setText(data1.dAsk); //ask
myViewHolder.buylow.setText(data1.dHigh);
myViewHolder.currentbid = Double.parseDouble(data1.dBid);
myViewHolder.currentask = Double.parseDouble(data1.dAsk);
if (myViewHolder.currentbid > myViewHolder.lastbid)
{
myViewHolder.sellmax.setTextColor(Color.BLUE);
}else if (myViewHolder.currentbid < myViewHolder.lastbid)
{
myViewHolder.sellmax.setTextColor(Color.RED);
}
myViewHolder.lastbid = myViewHolder.currentbid;
//myViewHolder.lastask = myViewHolder.currentask;
myViewHolder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog dialog = new Dialog(context); // Context, this, etc.
dialog.setTitle("Action");
dialog.setContentView(R.layout.tradeaction);
dialog.show();
TextView trade = dialog.findViewById(R.id.TVTrade);
TextView chart = dialog.findViewById(R.id.TVChart);
TextView detail = dialog.findViewById(R.id.TVDetail);
detail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent detail = new Intent(context,DetailSymbol.class);
Bundle symbol = new Bundle();
symbol.putString("symbol",data1.dSymbol);
detail.putExtras(symbol);
context.startActivity(detail);
}
});
chart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dataholder.Chart = true;
Intent chart = new Intent(context,MainScreen.class);
context.startActivity(chart);
}
});
trade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent trade = new Intent(context,tradeitem.class);
Bundle symbol = new Bundle();
symbol.putString("lotsize",data1.dLotsize);
symbol.putString("symbol",data1.dSymbol);
trade.putExtras(symbol);
if (mBroadcastReceiver != null){
context.unregisterReceiver(mBroadcastReceiver);
}
context.startActivity(trade);
}
});
}
});
}
@Override
public int getItemCount() {
return data.size();
}
}
用于存储API数据的类
public class QuoteData {
public String datasymbol;
public String dataltp;
public String datachg;
public String datavol;
public QuoteData(String txtsymbol, String txtltp, String txtchg){
this.datasymbol = txtsymbol;
this.dataltp = txtltp;
this.datachg = txtchg;
}
String dSymbol,dBid,dBidqty,dAsk,dLtp,dAskqty,dOpen,dClose,dHigh,dLow,dVol,dOi,dChange,dNetchange,dLotsize,dLtt,dLut,dExpiry,dExchange;
public QuoteData(String dSymbol, String dBid, String dBidqty, String dAsk,String dLtp, String dAskqty,String dOpen,String dClose,String dHigh,String dLow,String dVol,String dOi,String dChange,String dNetchange,String dLotsize,String dLtt,String dLut,String dExpiry,String dExchange){
this.dSymbol = dSymbol;
this.dBid = dBid;
this.dBidqty = dBidqty;
this.dAsk = dAsk;
this.dLtp = dLtp;
this.dAskqty = dAskqty;
this.dOpen = dOpen;
this.dClose = dClose;
this.dHigh = dHigh;
this.dLow = dLow;
this.dVol = dVol;
this.dOi = dOi;
this.dChange = dChange;
this.dNetchange = dNetchange;
this.dLotsize=dLotsize;
this.dLtt = dLtt;
this.dLut = dLut;
this.dExpiry = dExpiry;
this.dExchange = dExchange;
}
public QuoteData(String dSymbol,String dBid,String dAsk,String dLow,String dHigh,String dLut){
this.dSymbol = dSymbol;
this.dBid = dBid;
this.dAsk = dAsk;
this.dLow = dLow;
this.dHigh = dHigh;
this.dLut = dLut;
}
}
所以任何人都可以帮助我,为什么添加第六个符号时逻辑不起作用。
答案 0 :(得分:1)
在适配器中定义两个String ArrayList
public class QuoteAdapter extends RecyclerView.Adapter <QuoteAdapter.MyViewHolder>{
Context context;
List<QuoteData> data;
List<String> olddatabid = new ArrayList<String>();
List<String> newdatabid = new ArrayList<String>();
List<String> olddataask = new ArrayList<String>();
List<String> newdataask = new ArrayList<String>();
class MyViewHolder extends RecyclerView.ViewHolder{
TextView time,symbol,sellmax,selllow,buymax,buylow,buy,sell,spread,lowtext,hightext;
LinearLayout layout,layoutbid,layoutask;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
time = itemView.findViewById(R.id.TVTime);
symbol = itemView.findViewById(R.id.TVSymbol);
sellmax = itemView.findViewById(R.id.TVSELLMAX);
selllow = itemView.findViewById(R.id.TVSELLLOW);
buymax = itemView.findViewById(R.id.TVBUYMAX);
buylow = itemView.findViewById(R.id.TVBUYHIGH);
buy = itemView.findViewById(R.id.TVBUY);
sell = itemView.findViewById(R.id.TVSELL);
spread = itemView.findViewById(R.id.TVSpread1);
lowtext = itemView.findViewById(R.id.low);
hightext = itemView.findViewById(R.id.high);
layout = itemView.findViewById(R.id.layout);
layoutbid = itemView.findViewById(R.id.LLBid);
layoutask = itemView.findViewById(R.id.LLAsk);
}
}
public QuoteAdapter(Context context,List<QuoteData> data)
{
this.data = data;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.quotelist,viewGroup,false);
return new MyViewHolder(view);
}
@SuppressLint("ResourceAsColor")
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
final QuoteData data1 = data.get(i);
myViewHolder.time.setText(data1.dLut);
myViewHolder.symbol.setText(data1.dSymbol);
myViewHolder.sellmax.setText(data1.dBid); //Bid
myViewHolder.selllow.setText(data1.dLow);
myViewHolder.buymax.setText(data1.dAsk); //ask
myViewHolder.buylow.setText(data1.dHigh);
if (newdatabid.size()< data.size())
{
newdatabid.add(data1.dBid); //Insert Value in array for the first time
}
if (olddatabid.size()< data.size())
{
olddatabid.add(data1.dBid); //Insert Value in array for the first time
}
if (newdataask.size()< data.size())
{
newdataask.add(data1.dAsk); //Insert Value in array for the first time
}
if (olddataask.size()< data.size()) //Insert Value in array for the first time
{
olddataask.add(data1.dAsk);
}
newdatabid.set(i,data1.dBid); //Store Value in array
newdataask.set(i,data1.dAsk); //Store Value in array
//Compare and perform Logic
if (Float.valueOf(newdatabid.get(i)) > Float.valueOf(olddatabid.get(i)))
{
myViewHolder.sellmax.setTextColor(Color.BLUE);
}
if (Float.valueOf(newdatabid.get(i)) < Float.valueOf(olddatabid.get(i)))
{
myViewHolder.sellmax.setTextColor(Color.RED);
}
if (Float.valueOf(newdataask.get(i)) > Float.valueOf(olddataask.get(i)))
{
myViewHolder.buymax.setTextColor(Color.BLUE);
}
if (Float.valueOf(newdataask.get(i)) < Float.valueOf(olddataask.get(i)))
{
myViewHolder.buymax.setTextColor(Color.RED);
}
olddatabid.set(i,newdatabid.get(i));
olddataask.set(i,newdataask.get(i));
}
});
}
}
@Override
public int getItemCount() {
return data.size();
}
}