2016-09-06 4 views
0

loadmoreを実装しようとすると問題が発生しました。問題は、AsyncTask内のadapter.notifyDataSetChangedです。私はこのtutsから実装しようとしています 、これは、クラスでの拡張ListActivityを使用して実装しようとしていますが、とにかく、このコードは動作していますが、データがloadmoreからデータを取得しようとすると更新されません。notifyDataSetChangedのときにアダプタがasyncTask内で動作しませんか?

import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.bye.swipetab.adapter.ViewPagerAdapter; 
import com.viewpagerindicator.CirclePageIndicator; 
import com.viewpagerindicator.UnderlinePageIndicator; 

import java.util.Arrays; 
import java.util.LinkedList; 


public class Tour extends Fragment { 
    private static final String TAG = Tour.class.getSimpleName(); 

    View myView; 
    private String[] values ; 
    private static final String brand_value = "brand_value"; 
    // Array of strings... 
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS"}; 

    // list with the data to show in the listview 
    private LinkedList<String> mListItems; 

    // The data to be displayed in the ListView 
    private String[] mNames = { "Fabian", "Carlos", "Alex", "Andrea", "Karla", 
      "Freddy", "Lazaro", "Hector", "Carolina", "Edwin", "Jhon", 
      "Edelmira", "Andres" }; 


    // Declare Variables 
    ViewPager viewPager; 
    PagerAdapter vAdapter; 
    String[] rank; 
    String[] country; 
    String[] population; 
    int[] flag; 
    UnderlinePageIndicator mIndicator; 

    ListView listView; 
    ArrayAdapter adapter; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     myView = inflater.inflate(R.layout.tour_layout, null); 


     View header = inflater.inflate(R.layout.tour_header_layout, null); 
     //View footer = inflater.inflate(R.layout.tour_footer_layout, null); 

     listView = (ListView) myView.findViewById(android.R.id.list); 
     listView.addHeaderView(header, null, false); // header will not be clickable 

     mListItems = new LinkedList<String>(); 
     mListItems.addAll(Arrays.asList(mNames)); 
     adapter = new ArrayAdapter<String>(getActivity(), R.layout.tour_listview, R.id.MobileArray, mNames); 

     listView.setAdapter(adapter); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       // When clicked, show a toast with the TextView text 
       String brand_text = ((TextView) view.findViewById(R.id.MobileArray)).getText().toString(); 
       //Toast.makeText(getApplicationContext(),brand_text, Toast.LENGTH_SHORT).show(); 

       // Starting single contact activity 
       Intent in = new Intent(getActivity(), TourActivity.class); 
       in.putExtra(brand_value, brand_text); 
       in.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
       startActivity(in); 
      } 
     }); 

     population = new String[] { 
         "1,354,040,000", 
         "1,210,193,422", 
         "315,761,000", 
         "315,123,000", 
         "363,752,000" }; 

     flag = new int[] { 
       R.drawable.offline_ide_4, 
       R.drawable.offline_ide_1, 
       R.drawable.offline_ide_2, 
       R.drawable.offline_ide_3, 
       R.drawable.offline_ide_5 }; 

     // Locate the ViewPager in viewpager_main.xml 
     viewPager = (ViewPager) myView.findViewById(R.id.pager); 
     // Pass results to ViewPagerAdapter Class 
     vAdapter = new ViewPagerAdapter(getContext(), rank, country, population, flag); 
     // Binds the Adapter to the ViewPager 
     viewPager.setAdapter(vAdapter); 

     CirclePageIndicator indicator = (CirclePageIndicator)myView.findViewById(R.id.indicator); 
     indicator.setViewPager(viewPager); 

     final float density = getResources().getDisplayMetrics().density; 
     indicator.setRadius(5 * density); 
     //indicator.setBackgroundColor(0x7f0c006a); 
     //indicator.setPageColor(R.color.black); 
     //indicator.setFillColor(R.color.tab_bg_yellow_deep); 
     //indicator.setStrokeColor(R.color.tab_bg_yellow_deep); 
     //indicator.setStrokeWidth(2 * density); 

     // set a listener to be invoked when the list reaches the end 
     ((LoadMoreListView) listView) 
       .setOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() { 
        public void onLoadMore() { 
         // Do the work to load more items at the end of list 
         // here 
         new LoadDataTask().execute(); 
        } 
       }); 

     return myView; 

    } 

    private class LoadDataTask extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 

      if (isCancelled()) { 
       return null; 
      } 

      // Simulates a background task 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
      } 

      for (int i = 0; i < mobileArray.length; i++) { 
       mListItems.add(mobileArray[i]); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      mListItems.add("Added after load more"); 

      // We need notify the adapter that the data have been changed 
      adapter.notifyDataSetChanged(); 

      // Call onLoadMoreComplete when the LoadMore task, has finished 
      ((LoadMoreListView) listView).onLoadMoreComplete(); 

      super.onPostExecute(result); 
     } 

     @Override 
     protected void onCancelled() { 
      // Notify the loading more operation has finished 
      ((LoadMoreListView) listView).onLoadMoreComplete(); 
     } 
    } 

} 

これが機能しない理由を教えてください:自分の言語が悪い場合、私は、これは私のクラスである:)

ごめんなさい?

+0

を行う、あなたが最初getActivity(のようなUIスレッドでadapter.notifyDataSetChanged()を呼び出す 'notifyDataSetChanged' –

+0

TRY)の前に' onLoadMoreComplete'を呼び出す必要がありそうです。 runOnUiThread(new Runnable(){ @Override public void run(){ adapter.notifyDataSetChanged(); } }); – jibysthomas

+0

私はアダプタのglobalvariableを使用して断片的な改善を使用しているため、リストビューへの更新を試しているときに動作していないので、このgetListAdapter()とまったく同じようにグローバル変数を使用していないtutsで、アダプタはまだ動作しません –

答えて

0

mListItemsは使用されません。

mNamesは、アダプターにリンクされて、あなたはAsyncTaskに記入されていないmListItemsは、あなたがそれを見直すべきかもしれません。

mListItems.addAll(Arrays.asList(mNames)); 
adapter = new ArrayAdapter<String>(getActivity(), R.layout.tour_listview, R.id.MobileArray, mNames); 

、その後、あなたのコードによると

 for (int i = 0; i < mobileArray.length; i++) { 
      mListItems.add(mobileArray[i]); 
     } 
+0

ありがとう、最初の値はadapter = new ArrayAdapter (getActivity()、R.layout.tour_listview、R.id.MobileArray、mNames)です。 TOアダプタ=新しいArrayAdapter (getActivity()、R.layout.tour_listview、R.id.MobileArray、mListItems); を追加し、「listView.setAdapter(adapter); adapter.notifyDataSetChanged();」を追加しました。 inside "onPostExecute"、ありがとうございます;) –

+0

助けてくれることをうれしく思います。 –

関連する問題