gpt4 book ai didi

java - 查询其他类/Activity 中的变量是否已填充

转载 作者:行者123 更新时间:2023-11-30 00:14:12 25 4
gpt4 key购买 nike

我是 Java 和 Android 的初学者。我昨天搜索了一整天的解决方案。我想检查第二个 Activity 中的变量是否已填充。或者询问方法“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());

}

最佳答案

更新的答案

这是您的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());

}
}
}

关于java - 查询其他类/Activity 中的变量是否已填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47589389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com