0

J'ai vu un tutoriel sur youtube montrant comment vous pouvez ajouter des données à sqlLite db, puis afficher ces données à ListView Video. Après avoir fait cela ... J'ai appliqué DragSortListView à ma listview ... mais chaque fois que je fais glisser une rangée. Cette erreur continue à venir et je ne pouvais pas le résoudre:J'ai "IndexOutOfBoundsException" quand j'essaie de faire glisser une ligne de ma ListView (en utilisant DragSortListView)

FATAL EXCEPTION: main 
Process: com.example.justi.ricksonbar, PID: 29157 
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
    at java.util.ArrayList.get(ArrayList.java:411) 
    at com.example.justi.ricksonbar.ProductAdapter.getItem(ProductAdapter.java:49) 
    at com.example.justi.ricksonbar.BackgroundTask$1.drop(BackgroundTask.java:91) 
    at com.mobeta.android.dslv.DragSortListView.dropFloatView(DragSortListView.java:1501) 
    at com.mobeta.android.dslv.DragSortListView.access$1200(DragSortListView.java:59) 
    at com.mobeta.android.dslv.DragSortListView$DropAnimator.onStop(DragSortListView.java:1293) 
    at com.mobeta.android.dslv.DragSortListView$SmoothAnimator.run(DragSortListView.java:1192) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Voici mon ProductAdapter.java ¨

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Color; 
import android.support.annotation.LayoutRes; 
import android.support.annotation.NonNull; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.TextView; 

import com.mobeta.android.dslv.DragSortController; 
import com.mobeta.android.dslv.DragSortListView; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.List; 

public class ProductAdapter extends ArrayAdapter{ 
    List list = new ArrayList(); 


    public ProductAdapter(Context context,int resource) { 
     super(context, resource); 
    } 

    public void add(Product object) { 
     list.add(object); 
     super.add(object); 
    } 


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

    @Override 
    public Object getItem(int position) { 
     return list.get(position); 
    } 

    @Override 
    public View getView(int position, final View convertView, ViewGroup parent) { 
     View row = convertView; 
     final ProductHolder productHolder; 
     if(row == null){ 
      LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = layoutInflater.inflate(R.layout.product_list, parent, false); 
      productHolder = new ProductHolder(); 
      productHolder.tx_name = (TextView)row.findViewById(R.id.tvProd_name); 
      productHolder.tx_price = (TextView)row.findViewById(R.id.tvProd_price); 
      productHolder.tx_type = (TextView)row.findViewById(R.id.tvProd_type); 
      row.setTag(productHolder); 
     }else{ 
      productHolder = (ProductHolder) row.getTag(); 
     } 
     Product product = (Product) getItem(position); 
     productHolder.tx_name.setText(product.getName().toString()); 
     productHolder.tx_price.setText(Double.toString(product.getPrice())); 
     productHolder.tx_type.setText(product.getType().toString()); 

     return row; 
    } 

    static class ProductHolder{ 
     TextView tx_name, tx_price, tx_type; 
    } 

} 

et mon BackgroundTask.java (où je mets les méthodes DragSortListView)

import android.app.Activity; 
import android.app.VoiceInteractor; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.AsyncTask; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.mobeta.android.dslv.DragSortController; 
import com.mobeta.android.dslv.DragSortListView; 

public class BackgroundTask extends AsyncTask<String, Product, String> { 
    Context ctx; 
    ProductAdapter productAdapter; 
    Activity activity; 
    DragSortListView listView; 

    BackgroundTask(Context ctx){ 
     this.ctx = ctx; 
     activity = (Activity)ctx; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

    } 

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

     String method = params[0]; 
     DatabaseHelper dHelper = new DatabaseHelper(ctx); 

     if (method.equals("add_info")){ 
      String name = params[1]; 
      double price = Double.parseDouble(params[2]); 
      String type = params[3]; 
      SQLiteDatabase db = dHelper.getWritableDatabase(); 
      //Call method of insertion 
      dHelper.addInformations(db, name, price, type); 
      return "One row is inserted...."; 

     }else if (method.equals("get_info")){ 
      listView = (DragSortListView)activity.findViewById(R.id.display_listview); 
      SQLiteDatabase db = dHelper.getReadableDatabase(); 
      Cursor cursor = dHelper.getInformations(db); 
      productAdapter = new ProductAdapter(ctx,R.layout.product_list); 
      String name, type; 
      double price; 
      while (cursor.moveToNext()){ 
       name = cursor.getString(cursor.getColumnIndex(ProductContract.ProductEntry.NAME)); 
       price = cursor.getDouble(cursor.getColumnIndex(ProductContract.ProductEntry.PRICE)); 
       type = cursor.getString(cursor.getColumnIndex(ProductContract.ProductEntry.TYPE)); 
       Product product = new Product(name, price, type); 
       publishProgress(product); 
      } 

      return "get_info"; 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Product... values) { 
     productAdapter.add(values[0]); 

    } 

    private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() 
    { 
     @Override 
     public void drop(int from, int to) 
     { 
      productAdapter = new ProductAdapter(ctx,R.layout.product_list); 
      if (from != to) 
      { 
       Object item = productAdapter.getItem(from); 
       productAdapter.remove(item); 
       productAdapter.insert(item, to); 
      } 
     } 
    }; 

    private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener() 
    { 
     @Override 
     public void remove(int which) 
     { 
      productAdapter = new ProductAdapter(ctx,R.layout.product_list); 
      productAdapter.remove(productAdapter.getItem(which)); 
     } 
    }; 

    @Override 
    protected void onPostExecute(String result) { 

     if (result.equals("get_info")){ 
      listView.setAdapter(productAdapter); 
      listView.setDropListener(onDrop); 
      listView.setRemoveListener(onRemove); 

      DragSortController controller = new DragSortController(listView); 
      controller.setRemoveEnabled(false); 
      controller.setSortEnabled(true); 
      controller.setDragInitMode(1); 

      listView.setFloatViewManager(controller); 
      listView.setOnTouchListener(controller); 
      listView.setDragEnabled(true); 
     }else{ 
      Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

Aidez-moi s'il vous plaît. Merci beaucoup

Répondre

0

Vous essayez de récupérer un objet avant d'ajouter un élément dans un adaptateur. Selon votre méthode add de votre adaptateur, ajoutera des éléments dans l'adaptateur.

public void add(Product object) { 
    list.add(object); 
    super.add(object); 
} 
+0

Oh ok. Ça a du sens. Maintenant, je pense où je peux mettre cette méthode "drop". Il doit être après que les données sont ajoutées – Jha

+0

Ce n'est vraiment pas une bonne idée d'obtenir des éléments de l'adaptateur comme ça, plutôt passant adaptateur, vous pouvez passer la référence de vos articles. – Rahul

+0

Vraiment? Je ne le savais pas, comme vous pouvez le voir évidemment, je suis un débutant à Android. Je viens de voir ce tutoriel et littéralement suivi à partir de zéro. – Jha