我正在开发一个ionic2项目并尝试在应用中显示连接状态消息。这是我的代码:
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ListView lv;
//public String term = null;
public String url;
public EditText editTextInput;
ArrayList<HashMap<String, String>> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextInput = (EditText) findViewById(R.id.editTextInput);
itemList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
//new GetSearchItems().execute();
}
public void onSearchClick(View v)
{
// String term = editTextInput.getText().toString();
// url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=";
if (lv== null) {
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
else {
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
}
public void page1(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page2(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=11";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page3(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=21";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page4(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=31";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page5(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=41";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page6(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=51";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
public void page7(View v)
{
itemList.clear();
String term = editTextInput.getText().toString();
url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=&start=61";
url = url.replaceAll(" ", "%20");
new GetSearchItems().execute();
}
// public void onShareClick(View v) {
//
// Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
// sharingIntent.setType("text/plain");
// String shareSub = String.valueOf(v.findViewById(R.id.title));
// String shareBody = String.valueOf(v.findViewById(R.id.link));
//
// sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
// sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
// startActivity(Intent.createChooser(sharingIntent, "Share using"));
// }
public class GetSearchItems extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this,"SearchResults are downloading",Toast.LENGTH_LONG).show();
}
@Override
public Void doInBackground(Void... arg0)
{
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
//String url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyA_Ee1WVjaHltF6B6wr2mkONXetYsy-ogU&cx=016799632979308504173:buxbhwd9nxm&q=" + term + "&gsc.sort=";
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null)
{
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray items = jsonObj.getJSONArray("items");
// looping through All results
for (int i = 0; i < items.length(); i++) {
JSONObject c = items.getJSONObject(i);
String title = c.getString("title");
String link = c.getString("link");
String displayLink = c.getString("displayLink");
String formattedUrl = c.getString("formattedUrl");
String snippet = c.getString("snippet");
// tmp hash map for single result
HashMap<String, String> item = new HashMap<>();
// adding each child node to HashMap key => value
item.put("title", title);
item.put("link", link);
item.put("displayLink", displayLink);
item.put("formattedUrl", formattedUrl);
item.put("snippet", snippet);
// adding contact to result list
itemList.add(item);
}
} catch (final JSONException e)
{
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else
{
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(MainActivity.this, itemList,
R.layout.list_item,
new String[] { "title","link", "snippet" }, new int[] {
R.id.title, R.id.link, R.id.snippet });
lv.setAdapter(adapter);
//setListAdapter(adapter);
// selecting single ListView item
//ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
//@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// getting values from selected ListItem
String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String link = ((TextView) view.findViewById(R.id.link)).getText().toString();
String snippet = ((TextView) view.findViewById(R.id.snippet)).getText().toString();
Button shareButton = (Button) view.findViewById(R.id.sharebutton);
shareButton.setOnClickListener((View.OnClickListener) shareButton);
{
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
shareIntent.putExtra(Intent.EXTRA_TEXT, link + " " + snippet );
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
}
});
}
}
}
我想要实现的是:如果离线,toast msg会一直待在那里,直到它在线。但对于在线消息,它只会显示3秒钟。一切正常,除非它在线并且在显示在线消息后,离线的Toast msg保持在那里并且即使我调用dismiss()也没有消失。我在这里错过了什么吗?
我的环境是:&#34;离子角&#34;:&#34; 3.3.0&#34;,&#34;离子&#34;:&#34; 3.6.0&#34;, &#34;打字稿&#34;:&#34; 2.3.3&#34;
任何帮助将不胜感激!