我是Java和Android的初学者。我昨天搜索了一整天的解决方案。如果填充了,我想检查第二个活动中的变量。或者问问方法" loadAngebote"如果它有一个返回值。为获取数据而执行的方法是:
public class loadAngebote extends AsyncTask<String, Void, ArrayList<ArtikelAngebot>>{
String data ="";
@Override
protected ArrayList<ArtikelAngebot> doInBackground(String... params){
try
{
URL url = new URL("http://url/file.php");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while(line != null){
line = bufferedReader.readLine();
data = data + line;
}
ArrayList<String> listdata = new ArrayList<>();
JSONArray jArray = new JSONArray(data);
for(int i =0 ;i <jArray.length(); i++){
listdata.add(jArray.getString(i));
}
JSONArray json = new JSONArray(data);
String[][] matrix = new String[json.length()][6];
for (int i=0; i < json.length(); i++) {
JSONObject obj = json.getJSONObject(i);
matrix[i][0] = String.valueOf(obj.getInt("ID"));
matrix[i][1] = String.valueOf(obj.getInt("art_nr"));
matrix[i][2] = String.valueOf(obj.getDouble("preis"));
matrix[i][3] = obj.getString("von");
matrix[i][4] = obj.getString("bis");
matrix[i][5] = obj.getString("art_link");
}
String[] all_ID = new String[matrix.length];
String[] all_art_nr = new String[matrix.length];
String[] all_preis = new String[matrix.length];
String[] all_von = new String[matrix.length];
String[] all_bis = new String[matrix.length];
String[] all_link = new String[matrix.length];
for (int i = 0; i < matrix.length; i++) {
all_ID[i] = matrix[i][0];
all_art_nr[i] = matrix[i][1];
all_preis[i] = matrix[i][2];
all_von[i] = matrix[i][3];
all_bis[i] = matrix[i][4];
all_link[i] = matrix[i][5];
}
ArrayList<ArtikelAngebot> dataList = new ArrayList<>();
for (int i = 0; i < matrix.length; i++) {
ArtikelAngebot angebote = new ArtikelAngebot(all_art_nr[i], "Für: " + all_preis[i] + " €","Von: " + all_von[i],"Bis: " + all_bis[i], all_link[i]);
dataList.add(angebote);
}
return dataList; <--------------------------------------------
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<ArtikelAngebot> QueryResult){
AngeboteListAdapter adapter = new AngeboteListAdapter(AngeboteActivity.this, R.layout.angebote_list_view_adapter, QueryResult);
mListView.setAdapter(adapter);
}
}
我需要检查dataList是否填充在我的应用程序的其他类中:
static String getLocationResultTitle(Context context, List<Location> locations) {
if(?????dataList_is_filled?????){
String numLocationsReported = "Text1";
return numLocationsReported + " \r\n \r\n :) \r\n \r\n" + DateFormat.getDateTimeInstance().format(new Date());
}else{
String numLocationsReported = "Text2";
return numLocationsReported + " \r\n \r\n :( \r\n \r\n" + DateFormat.getDateTimeInstance().format(new Date());
}
答案 0 :(得分:1)
更新了答案
这是 AngeboteActivity
public class AngeboteActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public enum DataHolder {
INSTANCE;
private ArrayList<ArtikelAngebot> mObjectList;
public static boolean hasData() {
return INSTANCE.mObjectList != null;
}
public static ArrayList<ArtikelAngebot> getData() {
final ArrayList<ArtikelAngebot> retList = INSTANCE.mObjectList;
INSTANCE.mObjectList = null;
return retList;
}
public static void setData(final ArrayList<ArtikelAngebot> objectList) {
INSTANCE.mObjectList = objectList;
}
}
public class loadAngebote extends AsyncTask<String, Void, ArrayList<ArtikelAngebot>> {
String data = "";
@Override
protected ArrayList<ArtikelAngebot> doInBackground(String... params) {
try {
URL url = new URL("http://url/file.php");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (line != null) {
line = bufferedReader.readLine();
data = data + line;
}
ArrayList<String> listdata = new ArrayList<>();
JSONArray jArray = new JSONArray(data);
for (int i = 0; i < jArray.length(); i++) {
listdata.add(jArray.getString(i));
}
JSONArray json = new JSONArray(data);
String[][] matrix = new String[json.length()][6];
for (int i = 0; i < json.length(); i++) {
JSONObject obj = json.getJSONObject(i);
matrix[i][0] = String.valueOf(obj.getInt("ID"));
matrix[i][1] = String.valueOf(obj.getInt("art_nr"));
matrix[i][2] = String.valueOf(obj.getDouble("preis"));
matrix[i][3] = obj.getString("von");
matrix[i][4] = obj.getString("bis");
matrix[i][5] = obj.getString("art_link");
}
String[] all_ID = new String[matrix.length];
String[] all_art_nr = new String[matrix.length];
String[] all_preis = new String[matrix.length];
String[] all_von = new String[matrix.length];
String[] all_bis = new String[matrix.length];
String[] all_link = new String[matrix.length];
for (int i = 0; i < matrix.length; i++) {
all_ID[i] = matrix[i][0];
all_art_nr[i] = matrix[i][1];
all_preis[i] = matrix[i][2];
all_von[i] = matrix[i][3];
all_bis[i] = matrix[i][4];
all_link[i] = matrix[i][5];
}
ArrayList<ArtikelAngebot> dataList = new ArrayList<>();
for (int i = 0; i < matrix.length; i++) {
ArtikelAngebot angebote = new ArtikelAngebot(all_art_nr[i], "Für: " + all_preis[i] + " €", "Von: " + all_von[i], "Bis: " + all_bis[i], all_link[i]);
dataList.add(angebote);
}
DataHolder.setData(dataList);
return dataList;
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<ArtikelAngebot> QueryResult) {
AngeboteListAdapter adapter = new AngeboteListAdapter(AngeboteActivity.this, R.layout.angebote_list_view_adapter, QueryResult);
mListView.setAdapter(adapter);
}
}
}
以下是 Utils 类
public class Utils {
static String getLocationResultTitle(Context context, List<Location> locations) {
if (AngeboteActivity.DataHolder.hasData()) {
//if hasData do your stuff what you want
String numLocationsReported = "Text1";
return numLocationsReported + " \r\n \r\n :) \r\n \r\n" + DateFormat.getDateTimeInstance().format(new Date());
} else {
String numLocationsReported = "Text2";
return numLocationsReported + " \r\n \r\n :( \r\n \r\n" + DateFormat.getDateTimeInstance().format(new Date());
}
}
}
答案 1 :(得分:0)
更新的答案我的Angebote活动:
public class AngeboteActivity extends AppCompatActivity {
public static TextView data;
public static ListView mListView;
private static final String TAG = "AngeboteActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_angebote);
data = (TextView) findViewById(R.id.data);
mListView = (ListView) findViewById(R.id.listView);
new loadAngebote().execute();
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "On Start .....");
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object Inhalt = parent.getAdapter().getItem(position);
final Intent intent = new Intent(AngeboteActivity.this, AngeboteRequestInfoActivity.class);
Bundle bundle = new Bundle();
//Objekt Serialisieren
bundle.putSerializable("object", (Serializable) Inhalt);
//Objekt in Intent Extras packen
intent.putExtras(bundle);
AngeboteActivity.this.startActivity(intent);
}
});
}
private class QueryResult {
ArrayList<ArtikelAngebot> dataList;
public QueryResult(ArrayList<ArtikelAngebot> dataList) {
this.dataList = dataList ;
}
}
public enum DataHolder {
INSTANCE;
private ArrayList<ArtikelAngebot> mObjectList;
public static boolean hasData() {
return INSTANCE.mObjectList != null;
}
public static void setData(final ArrayList<ArtikelAngebot> objectList) {
INSTANCE.mObjectList = objectList;
}
public static ArrayList<ArtikelAngebot> getData() {
final ArrayList<ArtikelAngebot> retList = INSTANCE.mObjectList;
INSTANCE.mObjectList = null;
return retList;
}
}
public class loadAngebote extends AsyncTask<String, Void, ArrayList<ArtikelAngebot>>{
String data ="";
@Override
protected ArrayList<ArtikelAngebot> doInBackground(String... params){
try
{
URL url = new URL("http://url/request.php");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while(line != null){
line = bufferedReader.readLine();
data = data + line;
}
ArrayList<String> listdata = new ArrayList<>();
JSONArray jArray = new JSONArray(data);
for(int i =0 ;i <jArray.length(); i++){
listdata.add(jArray.getString(i));
}
JSONArray json = new JSONArray(data);
String[][] matrix = new String[json.length()][6];
for (int i=0; i < json.length(); i++) {
JSONObject obj = json.getJSONObject(i);
matrix[i][0] = String.valueOf(obj.getInt("ID"));
matrix[i][1] = String.valueOf(obj.getInt("art_nr"));
matrix[i][2] = String.valueOf(obj.getDouble("preis"));
matrix[i][3] = obj.getString("von");
matrix[i][4] = obj.getString("bis");
matrix[i][5] = obj.getString("art_link");
}
String[] all_ID = new String[matrix.length];
String[] all_art_nr = new String[matrix.length];
String[] all_preis = new String[matrix.length];
String[] all_von = new String[matrix.length];
String[] all_bis = new String[matrix.length];
String[] all_link = new String[matrix.length];
for (int i = 0; i < matrix.length; i++) {
all_ID[i] = matrix[i][0];
all_art_nr[i] = matrix[i][1];
all_preis[i] = matrix[i][2];
all_von[i] = matrix[i][3];
all_bis[i] = matrix[i][4];
all_link[i] = matrix[i][5];
}
ArrayList<ArtikelAngebot> dataList = new ArrayList<>();
for (int i = 0; i < matrix.length; i++) {
ArtikelAngebot angebote = new ArtikelAngebot(all_art_nr[i], "Für: " + all_preis[i] + " €","Von: " + all_von[i],"Bis: " + all_bis[i], all_link[i]);
dataList.add(angebote);
}
DataHolder.setData(dataList);
return dataList;
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<ArtikelAngebot> QueryResult){
AngeboteListAdapter adapter = new AngeboteListAdapter(AngeboteActivity.this, R.layout.angebote_list_view_adapter, QueryResult);
mListView.setAdapter(adapter);
}
}
我的Utils.java类
public class Utils {
final static String KEY_LOCATION_UPDATES_REQUESTED = "location-updates-requested";
final static String KEY_LOCATION_UPDATES_RESULT = "location-update-result";
static void setRequestingLocationUpdates(Context context, boolean value) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean(KEY_LOCATION_UPDATES_REQUESTED, value)
.apply();
}
static boolean getRequestingLocationUpdates(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(KEY_LOCATION_UPDATES_REQUESTED, false);
}
/**
* Posts a notification in the notification bar when a transition is detected.
* If the user clicks the notification, control goes to the MainActivity.
*/
static void sendNotification(Context context, String notificationDetails) {
// Create an explicit content Intent that starts the main Activity.
Intent notificationIntent = new Intent(context, AngeboteActivity.class);
notificationIntent.putExtra("from_notification", true);
// Construct a task stack.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Add the main Activity to the task stack as the parent.
stackBuilder.addParentStack(AngeboteActivity.class);
// Push the content Intent onto the stack.
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack.
PendingIntent notificationPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
// Notification Einstellungen
builder.setSmallIcon(R.drawable.ic_launcher)
// In a real app, you may want to use a library like Volley
// to decode the Bitmap.
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher))
.setColor(Color.RED)
.setContentTitle("Ihre Apotheke vor Ort")
.setContentText(notificationDetails)
.setContentIntent(notificationPendingIntent);
// Dismiss notification once the user touches it.
builder.setAutoCancel(true);
// Get an instance of the Notification manager
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
}
/**
* Returns the title for reporting about a list of {@link Location} objects.
*
* @param context The {@link Context}.
*/
static String getLocationResultTitle(Context context, List<Location> locations) {
if (AngeboteActivity.DataHolder.hasData()) {
//if hasData do your stuff what you want
String numLocationsReported = "Text1";
return numLocationsReported + " \r\n \r\n :) \r\n \r\n" + DateFormat.getDateTimeInstance().format(new Date());
}else{
String numLocationsReported = "Text2";
return numLocationsReported + " \r\n \r\n :( \r\n \r\n" + DateFormat.getDateTimeInstance().format(new Date());
}
}
/**
* Returns te text for reporting about a list of {@link Location} objects.
*
* @param locations List of {@link Location}s.
*/
private static String getLocationResultText(Context context, List<Location> locations) {
if (locations.isEmpty()) {
return "Unbekannte Position";
}
StringBuilder sb = new StringBuilder();
for (Location location : locations) {
sb.append("(");
sb.append(location.getLatitude());
sb.append(", ");
sb.append(location.getLongitude());
sb.append(")");
sb.append("\n");
}
return sb.toString();
}
static void setLocationUpdatesResult(Context context, List<Location> locations) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(KEY_LOCATION_UPDATES_RESULT, getLocationResultTitle(context, locations)
+ "\n" + getLocationResultText(context, locations))
.apply();
}
static String getLocationUpdatesResult(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getString(KEY_LOCATION_UPDATES_RESULT, "");
}
}
答案 2 :(得分:0)
好的,你的活动看起来不错..
现在在 Utils类中将if(loadAngebote.DataHolder.hasData())
更改为if (AngeboteActivity.DataHolder.hasData())
<强>这里: - 强>
public class Utils {
static String getLocationResultTitle(Context context, List<Location> locations) {
if (AngeboteActivity.DataHolder.hasData()) {
//if hasData do your stuff what you want
String numLocationsReported = "Text1";
return numLocationsReported + " \r\n \r\n :) \r\n \r\n" + DateFormat.getDateTimeInstance().format(new Date());
} else {
String numLocationsReported = "Text2";
return numLocationsReported + " \r\n \r\n :( \r\n \r\n" + DateFormat.getDateTimeInstance().format(new Date());
}
}
}