-2

我開始在Android的編程,這是我迄今爲止最大的項目。請嘗試在你的答案非常具體,因爲這對我來說是非常新的。 我想要做什麼是我的TextView在OneFragment.`如何在異步任務類完成後更改textview(在片段中)的值?

顯示天氣(說明)的值

這裏是MainActivity

package com.example.dell.myop; 

import android.content.Intent; 
import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 


import android.os.Bundle; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 

import com.example.dell.myop.R; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.widget.TextView; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 


import com.example.dell.myop.fragments.OneFragment; 

import com.example.dell.myop.fragments.ThreeFragment; 
import com.example.dell.myop.fragments.TwoFragment; 

public class MainActivity extends AppCompatActivity { 

    private Toolbar toolbar; 
    private TabLayout tabLayout; 
    private ViewPager viewPager; 
    String description; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     DownloadTask task = new DownloadTask(); 
     task.execute("http://api.openweathermap.org/data/2.5/weather?q=hamirpur,india&appid=41352ca35dd1be72cc7d9f47351d1b41"); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     viewPager = (ViewPager) findViewById(R.id.viewpager); 
     setupViewPager(viewPager); 

     tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(viewPager); 
    } 

    private void setupViewPager(ViewPager viewPager) { 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     adapter.addFragment(new OneFragment(), "Today"); 
     adapter.addFragment(new TwoFragment(), "Tomorrow"); 
     adapter.addFragment(new ThreeFragment(), "Day After"); 
     viewPager.setAdapter(adapter); 
    } 

    class ViewPagerAdapter extends FragmentPagerAdapter { 
     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

     public ViewPagerAdapter(FragmentManager manager) { 
      super(manager); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return mFragmentList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return mFragmentList.size(); 
     } 

     public void addFragment(Fragment fragment, String title) { 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      return mFragmentTitleList.get(position); 
     } 
    } 
} 

DownloadTask

package com.example.dell.myop; 

import android.content.Intent; 
import android.os.AsyncTask; 
import android.util.Log; 

import com.example.dell.myop.fragments.OneFragment; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

import static android.support.v4.app.ActivityCompat.startActivity; 

public class DownloadTask extends AsyncTask<String,Void,String> { 

    @Override 
    protected String doInBackground(String... urls) { 

     String result = ""; 
     URL url; 
     HttpURLConnection urlConnection= null; 

     try { 
      url = new URL(urls[0]); 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      InputStream in = urlConnection.getInputStream(); 
      InputStreamReader reader = new InputStreamReader(in); 
      int data = reader.read(); 
      while(data!=-1){ 
       char current = (char)data; 
       result += current; 
       data = reader.read(); 
      } 
      return result; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }  

     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     try { 
      JSONObject jObject = new JSONObject(result); 
      JSONArray jArray = jObject.getJSONArray("weather"); 
      for (int i=0; i < jArray.length(); i++) 
      { 
       try { 
        JSONObject oneObject = jArray.getJSONObject(i); 
        // Pulling items from the array 
        String main = oneObject.getString("main"); 
        String description = oneObject.getString("description"); 
        Log.i("main",main); 
        Log.i("description",description); 

       } catch (JSONException e) { 
        // Oops 
       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

這裏是OneFragment

package com.example.dell.myop.fragments; 

/** 
* Created by Dell on 13-09-2016. 
*/ 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.example.dell.myop.DownloadTask; 
import com.example.dell.myop.MainActivity; 
import com.example.dell.myop.R; 

import com.example.dell.myop.R; 


public class OneFragment extends Fragment{ 

    public OneFragment() { 

    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_one, container, false); 
     TextView textView = (TextView) view.findViewById(R.id.textView); 

     textView.setText(description); 
     return view; 
    }  
} 
+0

無論下面的答案更好地executeDowloadTask基本上遵循這個答案http://stackoverflow.com/questions/12575068/how -to-GET-了對結果的-onpostexecute到主活動 - 因爲-的AsyncTask-is-a的 –

回答

1

我建議你將下載任務的邏輯移動到喲你的frargment,如果你只會在這個使用它。 這將是很容易的,這樣,只得到了描述,並設置:

textView.setText(description); in your fragment, 

如果你想使用這個downloadTask您的活動的其他組件,你要實現你的活動有一定回調並使用它們任何片段來更新用戶界面。 因此,讓您在活動說你聲明一些接口

interface OnDownloadTaskFinish { 
onFinish(String description); 
} 

在您的片段用它

onPostExecute(...){ 
//...some logic here 
getFragment..... .onFinish(description); 
} 

你應該實現這個接口和

void onFinish(String description){ 
textView.setText(description); 
} 
0

1)創建一個interface您的DownloadTask,其setternotification code作爲

private Listener listener; 

public void setListener(Listener listener){ 
    this.listener = listener; 
} 

public interface Listener{ 
    onCompleted(String description); 
} 

@Override 
protected void onPostExecute(String result) { 
    // Your Current Code 

    if(listener != null) listener.onCompleted(description); 
} 

2)實現此接口的OneFragment

public class OneFragment extends Fragment implements DownloadTask.Listener{ 

    private TextView textView; 

    @Override 
    onCompleted(String description){ 
     if(description != null && textView !=null) 
      textView.setDescription(description); 
    } 

    // Other code 
} 

3)創建global referenceOneFragmentMainActivity作爲

private OneFragment oneFragment = new OneFragment(); 

4)adapter.addFragment(oneFragment, "Today");更換adapter.addFragment(new OneFragment(), "Today");設置爲ListenerDownloadTask

5)設置監聽器作爲

DownloadTask task = new DownloadTask(); 
task.setListener(oneFragment); 
task.execute("http://api.openweathermap.org/data/2.5/weather?q=hamirpur,india&appid=41352ca35dd1be72cc7d9f47351d1b41"); 

6)這將是後viewPager.setAdapter(adapter);