Witam!
Mam taki problem bardzo irytujący. Szukałem odpowiedzi ale nie znalazłem, pasującej do mojego przypadku.
Mam taką aktywność.
Mam zmienna "globalną" w tej aktywności String json_string chce do niej przypisać wartość result z Metody onPostExecute(), ale jej nie przypisuje.
Co dziwne jak dam w onPostExecute() tak:
json_string = result;
json_textview.setText(json_string);
To wtedy wszystko śmiga, ale ja nie chce tej zmiennej jako textview tylko potrzebują ją mieć w stringu, aby potem jej użyć w DisplayListView(), no ale nie chce przypisać mi tej cholernej zmiennej. Przeszukałem troche internetu, sam stracilem duzo czasu i za nic nie mam pomysłu aby to rozwiązac.
Prosze o pomoc.
public class AfterLoginActivity extends AppCompatActivity {
private TextView textView;
private TextView json_textview;
private String json_string;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
textView = (TextView)findViewById(R.id.textView);
json_textview = (TextView)findViewById(R.id.json_textview);
SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
String login = sharedPreferences.getString(Config.LOGIN_SHARED_PREF, "Coś się popsuło");
textView.setText("Current user: " + login);
new BackgroundLoadData().execute();
DisplayListView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// kod usuniety aby nie zaciemniac
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// kod usuniety aby nie zaciemniac
}
private void logout(){
// kod usuniety aby nie zaciemniac
}
});
}
class BackgroundLoadData extends AsyncTask<Void,Void,String>{
private String JSON_STRING;
Context context;
public BackgroundLoadData() {
super();
}
BackgroundLoadData(Context ctx){
context = ctx;
}
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL(Config.GET_JSON_DATA_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while((JSON_STRING = bufferedReader.readLine())!= null){
stringBuilder.append(JSON_STRING + "\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// json_textview.setText(result);
json_string = result;
// json_textview.setText(json_string);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
public void DisplayListView(){
String name, date, time;
JSONObject jsonObject;
JSONArray jsonArray;
ItemAdapter itemAdapter = new ItemAdapter(this,R.layout.textview_element);
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(itemAdapter);
try {
jsonObject = new JSONObject(json_string);
jsonArray = jsonObject.getJSONArray("server_response");
for(int i = 0 ; i < jsonArray.length();i++){
JSONObject JO = jsonArray.getJSONObject(i);
name = JO.getString("name");
date = JO.getString("date");
time = JO.getString("time");
Items items = new Items(name, date, time);
itemAdapter.add(items);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}