2
私は自分のアプリケーションに在庫機能を持っており、ユーザーはその株を検索して価格を見ることができます。なんらかの理由で、ストック名と靴下の略語のあちこちに ""が表示されます(画像のように)。私は引用符を削除するが、コード内で何を変更するかを見つけることはできません。検索結果から ""を削除する
EditText searchQueryEditText;
Button searchButton;
ProgressBar progressBar;
RecyclerView stocksRecyclerView;
MyAdapter recyclerViewAdapter;
ArrayList<StockItem> stockItems;
public static ArrayList<String> stockItemsToSearch;
Timer timer;
TimerTask timerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_market_info);
stockItemsToSearch = new ArrayList<>();
stockItems = new ArrayList<>();
recyclerViewAdapter = new MyAdapter(stockItems);
stocksRecyclerView = (RecyclerView) findViewById(R.id.stockRecyclerView);
searchQueryEditText = (EditText) findViewById(R.id.searchQueryEditText);
searchButton = (Button) findViewById(R.id.searchButton);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
stocksRecyclerView.setAdapter(recyclerViewAdapter);
stocksRecyclerView.setLayoutManager(new LinearLayoutManager(this));
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String searchQuery = searchQueryEditText.getText().toString().toUpperCase();
stockItemsToSearch.add(searchQuery);
searchQueryEditText.setText("");
hideKeyboard();
progressBar.setVisibility(View.VISIBLE);
loadStockData(generateSearchString());
}
});
searchQueryEditText.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
stockItemsToSearch = loadUsersLastStocks();
loadStockData(generateSearchString());
//Real time updating
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
loadStockData(generateSearchString());
}
};
timer.schedule(timerTask, 0, 3000);
}
private ArrayList<String> loadUsersLastStocks() {
progressBar.setVisibility(View.VISIBLE);
ArrayList<String> lastStocks = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(MainActivity.user.get("last_stocks"));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String stockItem = jsonObject.getString("stock_item");
lastStocks.add(stockItem);
}
} catch (Exception e) { e.printStackTrace(); }
return lastStocks;
}
private void checkForNotify() {
for (StockItem stockItem : stockItems) {
try {
if (Double.parseDouble(stockItem.getBid()) > stockItem.getNotifyValue() && stockItem.getNotifyValue() > 0) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Remote hub")
.setContentText("One of stocks reached notify level");
Intent resultIntent = new Intent(this, MarketInfo.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MarketInfo.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(151651, mBuilder.build());
}
} catch (Exception e) { e.printStackTrace(); }
}
}
private String generateSearchString() {
String ss = "";
for (int i = 0; i < stockItemsToSearch.size(); i++) {
ss += "" + stockItemsToSearch.get(i) + "";
if (i != stockItemsToSearch.size() - 1)
ss += "+";
}
return ss;
}
private void loadStockData(final String searchString) {
Thread thread = new Thread() {
public void run() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://finance.yahoo.com/d/quotes.csv?s=" + searchString + "&f=snpb6c");
HttpResponse httpResponse = httpClient.execute(httpGet);
String responce = EntityUtils.toString(httpResponse.getEntity());
String[] sf = responce.split("\n");
stockItems.clear();
for (int i = 0; i < sf.length; i++) {
String[] s = sf[i].split(",");
StockItem stockItem = new StockItem(s[0], s[1], s[2], s[3], s[4]);
stockItems.add(stockItem);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
recyclerViewAdapter.notifyDataSetChanged();
checkForNotify();
progressBar.setVisibility(View.INVISIBLE);
}
});
} catch (Exception e) { Log.d("ERROR", "ERROR"); }
}
}; thread.start();
}
private void hideKeyboard() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
@Override
protected void onPause() {
super.onPause();
SQLiteHandler db = new SQLiteHandler(getApplicationContext());
String uuid = db.getUserDetails().get("uid");
JSONArray jsonArray = new JSONArray();
try {
for (String si : stockItemsToSearch) {
Log.d("ITEMS", si);
JSONObject jsonObject = new JSONObject();
jsonObject.put("stock_item", si);
jsonArray.put(jsonObject);
}
} catch (Exception e) { e.printStackTrace(); }
db.updateUsersLastStocks(jsonArray.toString(), uuid);
MainActivity.user = db.getUserDetails();
updateUsersLastStocks(jsonArray.toString(), uuid);
}
private void updateUsersLastStocks(final String newValue, final String uuid) {
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_UPDATE_LAST_STOCKS, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("RESPONSE", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("RESPONSE", error.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<>();
params.put("uid", uuid);
params.put("newLastStocks", newValue);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, "d");
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<StockItem> dataset;
public class ViewHolder extends RecyclerView.ViewHolder {
public View view;
public ViewHolder(View v) {
super(v);
view = v;
}
}
public MyAdapter(ArrayList<StockItem> dataset) {
this.dataset = dataset;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.stock_info_row_layout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
View view = holder.view;
final StockItem stockItem = dataset.get(position);
TextView nameTextView = (TextView) view.findViewById(R.id.nameTextView);
TextView bidTextView = (TextView) view.findViewById(R.id.bidTextView);
Button deleteRowButton = (Button) view.findViewById(R.id.deleteRowButton);
nameTextView.setText(stockItem.getNameWithFullName());
bidTextView.setText(stockItem.getBid() + "\n" + stockItem.getChange().substring(1, stockItem.getChange().length() - 1));
deleteRowButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataset.remove(stockItem);
timer.cancel();
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
loadStockData(generateSearchString());
}
};
runOnUiThread(new Runnable() {
@Override
public void run() {
stockItemsToSearch.remove(position);
recyclerViewAdapter.notifyItemRemoved(position);
//recyclerViewAdapter.notifyDataSetChanged();
timer.scheduleAtFixedRate(timerTask, 0, 3000);
}
});
}
});
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MarketInfo.this);
final EditText editText = new EditText(MarketInfo.this);
editText.setWidth(300);
editText.setHint("Notify me value");
builder.setView(editText);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (editText.getText().toString().length() > 0) {
try {
int notifyValue = Integer.parseInt(editText.getText().toString());
if (notifyValue <= Double.parseDouble(stockItem.getBid()))
throw new Exception();
else
stockItem.setNotifyValue(notifyValue);
} catch (Exception e) {
Toast.makeText(MarketInfo.this, "Notify value should be bigger than current bid", Toast.LENGTH_SHORT).show();
}
}
}
});
builder.create().show();
return false;
}
});
}
@Override
public int getItemCount() {
return dataset.size();
}
}
}
チェックStockItem.getNameWithFullName:
は、手動で引用符を削除することができます – rell