2017-09-03 85 views
-2

我最近開始寫一個應用程序,讓我從論壇獲取信息。我用JSoup庫來抓取數據。我的第一部分進展順利。使用viewpager與不同的片段?

當應用程序打開時,它只從網站(子論壇名稱)獲取數據並將其傳遞,以便listview能夠顯示它們。當我在列表視圖中按下一個項目時,它只是在子論壇中獲取線程列表。除了listview沒有正確更新以外,所有的東西都能正常工作。

正如你可以看到,當我按從列表(只需在列表中的第一項)的項目,然後將其設置loadedSub真正然後使用異步任務得到線程列表,然後在postexecute它調用notifyDataSetChanged() on PagerAdapter。問題是它只是完全更新UI,即使只留下1個選項卡,但是問題在於threadList未出現在列表視圖中。它只顯示我的列表視圖與以前的數據,而不是線程列表。

我應該使用其他活動還是有東西丟失?

enter image description here enter image description here

MainActivity.java

package com.ovnis.sscattered.elakiriunofficial; 

import android.app.ProgressDialog; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.support.design.widget.TabLayout; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 

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 android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 

import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.Toast; 

import org.jsoup.Connection; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

import java.io.IOException; 

public class MainActivity extends AppCompatActivity { 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link FragmentPagerAdapter} derivative, which will keep every 
    * loaded fragment in memory. If this becomes too memory intensive, it 
    * may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    public static SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    public static ViewPager mViewPager; 

    public static String[][] forumListSubForumsList = new String[13][]; 
    public static String[][] forumListSubForumListLinks = new String[13][]; 

    public static String[][] threadListLinks = new String[2][]; 

    static Context context; 

    public String[] forumNumbers = {"1", "86", "89", "6", 
            "66", "79", "70", "12", 
            "16", "33", "42", "25", 
            "22"}; 

    public static String subforumLink; 

    static boolean loaded = false; 
    static boolean loadedSub = false; 

    static boolean subForumMode = false; 

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 



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

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     new GetSubForumList().execute(); 

     context = getBaseContext(); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class SubForumsFrag extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 

     static ListView listview; 
     static ForumListAdapter adapter; 

     int pos = 0; 

     public SubForumsFrag() { 

     } 

     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static SubForumsFrag newInstance(int pos) { 
      SubForumsFrag fragment = new SubForumsFrag(); 
      Bundle args = new Bundle(); 
      args.putInt("poskey", pos); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

      View rootView = inflater.inflate(R.layout.list_base, container, false); 
      listview = (ListView) rootView.findViewById(R.id.listview); 

      pos = getArguments().getInt("poskey"); 

      if(loaded){ 
       adapter = new ForumListAdapter(getActivity(), forumListSubForumsList[pos]); 
      } 
      listview.setAdapter(adapter); 

      listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        Toast.makeText(getContext(), forumListSubForumListLinks[pos][position], Toast.LENGTH_SHORT).show(); 
        subForumMode = true; 
        subforumLink = forumListSubForumListLinks[pos][position]; 
        new GetThreadList().execute(); 
       } 
      }); 

      return rootView; 
     } 
    } 

    public static class ThreadListFrag extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 

     static ListView listview; 
     static ForumListAdapter adapter; 

     int pos = 0; 

     public ThreadListFrag() { 

     } 

     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static ThreadListFrag newInstance() { 
      ThreadListFrag fragment = new ThreadListFrag(); 
      return fragment; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

      View rootView = inflater.inflate(R.layout.list_base, container, false); 
      listview = (ListView) rootView.findViewById(R.id.listview); 

      if(loadedSub){ 
       adapter = new ForumListAdapter(getActivity(), threadListLinks[0]); 
      } 
      listview.setAdapter(adapter); 

      listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        //Toast.makeText(getContext(), forumListSubForumListLinks[pos][position], Toast.LENGTH_SHORT).show(); 
       } 
      }); 

      return rootView; 
     } 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a SubForumsFrag (defined as a static inner class below). 

      if(!subForumMode){ 
       return SubForumsFrag.newInstance(position); 
      }else{ 
       return ThreadListFrag.newInstance(); 
      } 
     } 

     @Override 
     public int getCount() { 
      // Show 13 total pages. 

      if(!subForumMode){ 
       return 13; 
      }else{ 
       return 1; 
      } 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      if(!subForumMode){ 
       switch (position) { 
        case 0: 
         return "Elakiri"; 
        case 1: 
         return "Cooking"; 
        case 2: 
         return "B & M"; 
        case 3: 
         return "General"; 
        case 4: 
         return "E. Traveler"; 
        case 5: 
         return "Automobile"; 
        case 6: 
         return "Motorcycles"; 
        case 7: 
         return "Entertainment"; 
        case 8: 
         return "C & I"; 
        case 9: 
         return "S. Piyasa"; 
        case 10: 
         return "Elakiri Magic"; 
        case 11: 
         return "Games"; 
        case 12: 
         return "Mobile"; 
       } 
      }else{ 
       return "Bitch Me"; 
      } 
      return null; 
     } 

     @Override 
     public int getItemPosition(Object obj){ 
      return POSITION_NONE; 
     } 
    } 

    class GetSubForumList extends AsyncTask<Void, Integer, String> 
    { 
     String TAG = getClass().getSimpleName(); 

     ProgressDialog dialog; 

     protected void onPreExecute(){ 
      Log.d(TAG + " PreExceute","On pre Exceute......"); 

      dialog = new ProgressDialog(MainActivity.this); 
      dialog.setTitle("Loading..."); 
      dialog.setIndeterminate(true); 
      dialog.setCancelable(false); 
      dialog.show(); 
     } 

     protected String doInBackground(Void...arg0){ 
      Log.d(TAG + " DoINBackGround","On doInBackground..."); 

      try { 
       Connection.Response response = Jsoup.connect("http://www.elakiri.com/forum/") 
         .execute(); 

       Document parsedDoc = response.parse(); 

       Element doc; 
       Elements doce; 

       for(int i = 0; i < forumNumbers.length; i++){ 
        doc = parsedDoc.select("tbody#collapseobj_forumbit_" + forumNumbers[i]).first(); 
        doce = doc.select("a[href] strong"); 
        forumListSubForumsList[i] = new String[(doce.size()+1)/2]; 
        forumListSubForumListLinks[i] = new String[(doce.size()+1)/2]; 
        for(int q = 0; q < (doce.size()+1)/2; q++){ 
         forumListSubForumsList[i][q] = doce.get(q*2).text(); 
         forumListSubForumListLinks[i][q] = doce.get(q*2).parent().absUrl("href"); 
        } 
       } 

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

      return "Shot"; 
     } 

     protected void onProgressUpdate(Integer...a){ 
      Log.d(TAG + " onProgressUpdate", "You are in progress update ... " + a[0]); 

     } 

     protected void onPostExecute(String result) { 
      Log.d(TAG + " onPostExecute", "" + result); 

      loaded = true; 
      mViewPager.invalidate(); 
      mSectionsPagerAdapter.notifyDataSetChanged(); 

      dialog.dismiss(); 
     } 
    } 

    static class GetThreadList extends AsyncTask<Void, Integer, String> 
    { 
     String TAG = getClass().getSimpleName(); 

     ProgressDialog dialog; 

     protected void onPreExecute(){ 
      Log.d(TAG + " PreExceute","On pre Exceute......"); 

      dialog = new ProgressDialog(context); 
      dialog.setTitle("Loading..."); 
      dialog.setIndeterminate(true); 
      dialog.setCancelable(false); 
//   dialog.show(); 
     } 

     protected String doInBackground(Void...arg0){ 
      Log.d(TAG + " DoINBackGround","On doInBackground..."); 

      try { 
       Connection.Response response = Jsoup.connect(subforumLink) 
         .execute(); 

       Element doc = response.parse().select("tbody#threadbits_forum_2").first(); 

       Elements doce = doc.select("a[id^=thread_title_]"); 

       threadListLinks[0] = new String[doce.size()]; 
       threadListLinks[1] = new String[doce.size()]; 

       for(int i = 0; i < doce.size(); i++){ 
        threadListLinks[0][i] = doce.get(i).text(); 
        threadListLinks[1][i] = doce.get(i).absUrl("href"); 
       } 

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

      return "Shot"; 
     } 

     protected void onProgressUpdate(Integer...a){ 
      Log.d(TAG + " onProgressUpdate", "You are in progress update ... " + a[0]); 

     } 

     protected void onPostExecute(String result) { 
      Log.d(TAG + " onPostExecute", "" + result); 

      loadedSub = true; 
      mViewPager.invalidate(); 
      mSectionsPagerAdapter.notifyDataSetChanged(); 
     } 
    } 
} 

ForumListAdapter.java

public class ForumListAdapter extends ArrayAdapter<String> { 
    private final Context context; 
    private final String[] values; 

    public ForumListAdapter(Context context, String[] values) { 
     super(context, R.layout.main_adapter ,values); 
     this.context = context; 
     this.values = values; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.main_adapter, parent, false); 
     TextView textView = (TextView) rowView.findViewById(R.id.firstLine); 
     textView.setText(values[position]); 

     return rowView; 
    } 
} 

回答

0

好吧,我把它用FragmentStatePageAda解決pter而不是使用FragmentPageAdapter