2016-04-12 12 views
1

最近、私はVolleyライブラリを使ってデータベースから情報を取得する方法を学びました。
私は今、アクティビティのPHPスクリプトからデータを受け取る方法を知っています。データベースからリストビューに新しい項目を追加する

私の問題は、私の活動に関するデータを表示することで、私はそれを説明します:

私はニュース記事を持っているウェブサイトのアプリを作成することですプロジェクトを持っています。 私のデータベースには記事がたくさん含まれているarticleというテーブルがあります。
もちろん、私はアクティビティの開始時にそれらをすべてロードしたくないので、私はアプリケーションに5で記事を送るPHPスクリプトを書いた。スクリプトはOkです。

Volley StringRequestを実行するリクエストクラスでは、String titles[] = new String[numberOfArticlesIWantToLoad]String texts[] = new String[numberOfArticlesIWantToLoad]のような記事を受け取ります。
コールバックを使用してアクティビティに送信し、アダプタを使用してListViewに表示します。

アダプターは正常に機能しますが、データベースからさらにアイテムをロードし、以前にロードした記事の下に表示する方法がわかりません。 5次の記事)と私は再びアダプタは、新しい記事が表示されている呼び出しではなく、私はそう遠くない私は期待して答えからだということを知っているかつての...
...すべての

まず、ある私のスクリプトは私が必要なものに良い? 第2に、前の記事の次の記事を読み込むためにアダプタで何を変更する必要がありますか?

これは、私の問題に取り組んでいた時間のための事前のおかげで、多くの助けになりました!ここで

は私のアダプタです:

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

    public MonAdapter(Context context, String[] values, String[] values2) { 
     super(context, -1, values); 
     this.context = context; 
     this.values = values; 
     this.values2 = values2; 
    } 

    @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.list_layout, parent, false); 
     TextView titre = (TextView) rowView.findViewById(R.id.titre); 
     TextView article = (TextView) rowView.findViewById(R.id.article); 
     titre.setText(values[position]); 
     article.setText(values2[position]); 


     return rowView; 
    } 

    public void addItems(String[] titres, String[] texts){ 
     String[] listTitres = new String[this.values.length + titres.length]; 
     String[] listTexts = new String[this.values2.length + texts.length]; 

     for(int i=0;i<this.values.length;i++){ 
      listTitres[i] = this.values[i]; 
      listTexts[i] = this.values2[i]; 
     } 
     for(int i=0;i<titres.length;i++){ 
      listTitres[this.values.length+i] = titres[i]; 
      listTexts[this.values.length+i] = texts[i]; 
     } 

     this.values = listTitres; 
     this.values2 = listTexts; 
    } 
} 

そしてここでは、私の活動であり、私はStringRequest(MyRequestクラスで)とアダプタを呼び出しどこから:
私は、irrevelantコードを削除するために最善を尽くしてみましたここで

が変化するので、エラーである:私はそれが長すぎる

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

    footerLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.footer_layout, null); 
    pb_charger = (ProgressBar)footerLayout.findViewById(R.id.pb_charger); 
    text = (TextView)findViewById(R.id.text); 
    pb_loader = (ProgressBar)findViewById(R.id.pb_loader); 
    list = (ListView)findViewById(R.id.list); 
    btn_charger = (Button)footerLayout.findViewById(R.id.btn_charger); 



    queue = VolleySingleton.getInstance(this).getRequestQueue(); 
    request = new MyRequest(this, queue); 

    request.getArticles(nombreCharger, NOMBRE_ARTICLE_CHARGER, new MyRequest.GetArticlesCallback() { 
     @Override 
     public void onSuccess(String[] listeTitres, String[] listeArticles, boolean fin) { 

      *Some irrelevant code* 

       final MonAdapter adapterTitres = new MonAdapter(getApplicationContext(), listeTitres, listeArticles); 
       list.setAdapter(adapterTitres); 
       adapterTitres.notifyDataSetChanged(); 
       list.addFooterView(footerLayout); 
       // list.removeFooterView(footerLayout); 

      } 
      nombreCharger++; 

     } 

     @Override 
     public void onError(String error, String id) { 

     } 
    }); 




    btn_charger.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      btn_charger.setVisibility(View.INVISIBLE); 
      pb_charger.setVisibility(View.VISIBLE); 
      list.removeFooterView(footerLayout); 

      request.getArticles(nombreCharger, NOMBRE_ARTICLE_CHARGER, new MyRequest.GetArticlesCallback() { 
       @Override 
       public void onSuccess(String[] listeTitres, String[] listeArticles, boolean fin) { 

        *Some irrelevant code* 

         MonAdapter a = (MonAdapter) list.getAdapter(); 
         a.addItems(listeTitres, listeArticles); 
         list.addFooterView(footerLayout); 
         list.setAdapter(a); 
         a.notifyDataSetChanged(); 

        } 
        nombreCharger++; 
       } 

       @Override 
       public void onError(String error, String id) { 


       } 
      }); 


     } 
    }); 

} 

EDITではありません願っています

04-12 22:47:34.640 12584-12584/com.example.thib.databaseaffichage E/AndroidRuntime: FATAL EXCEPTION: main 
                        Process: com.example.thib.databaseaffichage, PID: 12584 
                        java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.example.thib.databaseaffichage.MonAdapter 
                         at com.example.thib.databaseaffichage.MainActivity$2$1.onSuccess(MainActivity.java:142) 
                         at com.example.thib.databaseaffichage.myrequest.MyRequest$1.onResponse(MyRequest.java:63) 
                         at com.example.thib.databaseaffichage.myrequest.MyRequest$1.onResponse(MyRequest.java:39) 
                         at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67) 
                         at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 
                         at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 
                         at android.os.Handler.handleCallback(Handler.java:808) 
                         at android.os.Handler.dispatchMessage(Handler.java:103) 
                         at android.os.Looper.loop(Looper.java:193) 
                         at android.app.ActivityThread.main(ActivityThread.java:5292) 
                         at java.lang.reflect.Method.invokeNative(Native Method) 
                         at java.lang.reflect.Method.invoke(Method.java:515) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                         at dalvik.system.NativeStart.main(Native Method) 

答えて

0

あなたのプロジェクトのHeaderViewListAdapterはどこですか?

java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.example.thib.databaseaffichage.MonAdapter 
+0

こんにちは!私のプロジェクトにはheaderViewListAdapterがありません...オーバーライドする必要はありますか? –