Android刷卡刷新indexoutofbounds

时间:2016-05-05 08:32:31

标签: android

我有一个保存频道的自定义适配器。我使用刷卡刷新来刷新列表成员。但是当我下拉(刷新列表)并立即向上拉到列表时,我给IndexOutOfBoundsException。这是我的适配器:

public class KanalAdapter extends BaseAdapter implements Filterable {

    Context context;
    String kanaladi;
    String veritabani_id;
    ArrayList<Kanal> channelbaba;
    ArrayList<Kanal> kokchannelbaba;
    int OFFICIAL_KANAL = 0;
    int NORMAL_KANAL = 1;
    LayoutInflater lala;
    private DisplayImageOptions options;
    Filter kanalfilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                results.values = kokchannelbaba;
                results.count = kokchannelbaba.size();
            } else {
                ArrayList<Kanal> sonuclistesi = new ArrayList<>();
                for (Kanal i : kokchannelbaba) {
                    if (i.getKanaladi().toLowerCase().contains(constraint.toString())) {
                        sonuclistesi.add(i);
                    }
                }
                results.values = sonuclistesi;
                results.count = sonuclistesi.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.count == 0) {
                notifyDataSetInvalidated();
            } else {
                channelbaba = (ArrayList<Kanal>) results.values;
                notifyDataSetChanged();
            }
        }
    };

    public KanalAdapter(Context context, ArrayList<Kanal> channelbaba) {
        this.context = context;
        this.channelbaba = channelbaba;
        kokchannelbaba = channelbaba;
        veritabani_id = SharedIdCek();
        kanaldakikisisayilari();
        lala = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.badooooo)
                .showImageForEmptyUri(R.mipmap.badooooo)
                .showImageOnFail(R.mipmap.badooooo)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .postProcessor(new BitmapProcessor() {
                    @Override
                    public Bitmap process(Bitmap bmp) {
                        return Bitmap.createScaledBitmap(bmp, 970, 260, false);
                    }
                })
                .build();
    }

    private String SharedIdCek() {
        SharedPreferences sp = context.getSharedPreferences("kullaniciverileri", Context.MODE_PRIVATE);
        String veritabani_id = sp.getString("serverid", "defaultserverid");
        return veritabani_id;
    }

    private void kanaldakikisisayilari() {
        for (int i = 0; i < channelbaba.size(); i++) {
            ServerKanaldakiKisiSayisi sKKS = new ServerKanaldakiKisiSayisi(i);
            sKKS.execute(channelbaba.get(i).getKanaladi());
        }
    }

    public int getCount() {
        return (channelbaba.size());
    }

    public Object getItem(int i) {
        return channelbaba.get(i);
    }

    public long getItemId(int i) {
        return i;
    }

    public int getItemViewType(int position) {
        Object item = getItem(position-1);
        Kanal kanal = (Kanal) item;
        if (kanal.official) {
            return OFFICIAL_KANAL;
        } else {
            return NORMAL_KANAL;
        }
    }

    public int getViewTypeCount() {
        return 2;
    }

    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        KanalHolder holder = null;
        final int pozisyon = position;
        Object currentKanal = getItem(position);
        Kanal kanal = (Kanal) currentKanal;
        if (convertView == null) {
            holder = new KanalHolder();
            if (kanal.official) {
                convertView = lala.inflate(R.layout.officialkanal, null);
                holder.officialgenel = (ImageView) convertView.findViewById(R.id.imageView5);
                holder.officialkanaladitext = (TextView) convertView.findViewById(R.id.textView4);
                holder.menubutonu = (ImageButton) convertView.findViewById(R.id.button8);
                holder.officialkisisayisi = (TextView) convertView.findViewById(R.id.textView26);
                holder.officiallikesayisi = (TextView) convertView.findViewById(R.id.textView27);
            }
            if (!kanal.official) {
                convertView = lala.inflate(R.layout.normalkanal, null);
                holder.normalgenel = (ImageView) convertView.findViewById(R.id.imageView5);
                holder.normalkanaladi = (TextView) convertView.findViewById(R.id.textView4);
                holder.normalkisisayisi = (TextView) convertView.findViewById(R.id.textView8);
                holder.normallikesayisi = (TextView) convertView.findViewById(R.id.textView28);
            }

            convertView.setTag(holder);
        } else {
            holder = (KanalHolder) convertView.getTag();
        }
        if (kanal.official) {
            holder.officialgenel.setBackgroundResource(R.mipmap.cropped);
            holder.officialkanaladitext.setText(channelbaba.get(position).getKanaladi());
            holder.officiallikesayisi.setText(String.valueOf(channelbaba.get(position).getLikesayisi()));
            holder.officialgenel.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    kanaladi = channelbaba.get(position).getKanaladi();
                    String kanalid = channelbaba.get(position).getId();
                    int likedurumu = channelbaba.get(position).getLikedurumu();
                    Log.i("tago", "adapter like durumu" + likedurumu);
                    KanalaElemanEkle kEE = new KanalaElemanEkle();
                    kEE.execute(kanaladi);
                    Intent intent = new Intent(context, GrupSohbeti.class);
                    intent.putExtra("intentname", "kanaladapter");
                    intent.putExtra("kanalmodu", "o");
                    intent.putExtra("kanaladi", kanaladi);
                    intent.putExtra("kanallikedurumu", likedurumu);
                    intent.putExtra("kanalid", kanalid);
                    context.startActivity(intent);
                }
            });
        }
        if (!kanal.official) {
            Kanal objectt = channelbaba.get(position);
            if(holder.normalgenel.getTag()==null ||!holder.normalgenel.getTag().equals(objectt.getKanalurl())){
                ImageAware imageAvare = new ImageViewAware(holder.normalgenel,false);
                ImageLoader.getInstance().displayImage(channelbaba.get(position).getKanalurl(), imageAvare, options);
                holder.normalgenel.setTag(objectt.getKanalurl());
            }
            holder.normalkisisayisi.setText(channelbaba.get(position).getKisisayisi());
            Log.i("tago" , "getview kisisayisi kanaladapterposition" + position + "kisisayisi" + channelbaba.get(position).getKisisayisi());
            holder.normalkanaladi.setText(channelbaba.get(position).getKanaladi());
            holder.normallikesayisi.setText(String.valueOf(channelbaba.get(position).getLikesayisi()));
            holder.normalgenel.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    kanaladi = channelbaba.get(pozisyon).getKanaladi();
                    String kanalid = channelbaba.get(position).getId();
                    String kanalurl = channelbaba.get(pozisyon).getKanalurl();
                    int likedurumu = channelbaba.get(pozisyon).getLikedurumu();
                    KanalaElemanEkle kEE = new KanalaElemanEkle();
                    kEE.execute(kanaladi);
                    Intent intent = new Intent(context, GrupSohbeti.class);
                    intent.putExtra("intentname", "kanaladapter");
                    intent.putExtra("kanalmodu", "n");
                    intent.putExtra("kanaladi", kanaladi);
                    intent.putExtra("kanalid", kanalid);
                    intent.putExtra("kanalurl", kanalurl);
                    intent.putExtra("kanallikedurumu", likedurumu);
                    context.startActivity(intent);
                }
            });
        }
        return convertView;
    }

    public Filter getFilter() {
        if (kanalfilter == null) {

        }
        return kanalfilter;
    }


    static class KanalHolder {
        public ImageView normalgenel, officialgenel;
        public TextView normalkanaladi, officialkanaladitext, normalkisisayisi, normallikesayisi, officialkisisayisi, officiallikesayisi;
        public ImageButton menubutonu;
    }

这是错误:

java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0
      at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
      at java.util.ArrayList.get(ArrayList.java:308)
      at droxoft.armin.com.shappy.KanalAdapter.getItem(KanalAdapter.java:123)
      at droxoft.armin.com.shappy.KanalAdapter.getItemViewType(KanalAdapter.java:131)
      at com.felipecsl.quickreturn.library.widget.QuickReturnAdapter.getItemViewType(QuickReturnAdapter.java:109)
      at android.widget.AbsListView$RecycleBin.getScrapView(AbsListView.java:8228)
      at android.widget.AbsListView.obtainView(AbsListView.java:2707)
      at android.widget.ListView.makeAndAddView(ListView.java:1811)
      at android.widget.ListView.fillDown(ListView.java:697)
      at android.widget.ListView.fillGap(ListView.java:661)
      at android.widget.AbsListView.trackMotionScroll(AbsListView.java:6686)
      at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:5685)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
      at android.view.Choreographer.doCallbacks(Choreographer.java:614)
      at android.view.Choreographer.doFrame(Choreographer.java:583)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
      at android.os.Handler.handleCallback(Handler.java:733)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:146)
      at android.app.ActivityThread.main(ActivityThread.java:5602)
      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:1283)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
      at dalvik.system.NativeStart.main(Native Method)

编辑部分:

private class KanallariYenidenCek extends AsyncTask<String, Void, String> {
        String charset;
        String query;

        protected String doInBackground(String... params) {
            charset = "utf-8";
            String param1 = "id";
            try {
                query = String.format("param1=%s", URLEncoder.encode(param1, charset));
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                return kanallarigor(params[0]);
            } catch (Exception e) {
                e.printStackTrace();
                return "olmadi";
            }
        }

        private String kanallarigor(String id) {
            channelbaba.clear();
            HttpURLConnection sconnection = null;
            try {
                sconnection = (HttpURLConnection) new URL("http://185.22.184.15/shappy//get_official_channels.php?id=" + id).openConnection();
                Log.i("tago", "Page Fragment1 official kanalları gor bagı kuruldu");
            } catch (IOException e) {
                e.printStackTrace();
            }
            sconnection.setDoOutput(true);
            sconnection.setDoInput(true);
            sconnection.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) ");
            sconnection.setRequestProperty("Accept", "* /*");
            sconnection.setRequestProperty("Accept-Charset", charset);
            sconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

            try {
                OutputStream output = new BufferedOutputStream(sconnection.getOutputStream());
                output.write(query.getBytes(charset));
                output.close();
                try {
                    int a = sconnection.getResponseCode();
                    String b = sconnection.getResponseMessage();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                BufferedReader in;
                if (sconnection.getResponseCode() == 200) {
                    in = new BufferedReader(new InputStreamReader(sconnection.getInputStream()));
                    String inputline = in.readLine();
                    JSONArray jsono = new JSONArray(inputline);
                    for (int i = 0; i < jsono.length(); i++) {
                        JSONObject object = jsono.getJSONObject(i);
                        Kanal kanal = new Kanal(true);
                        kanal.setKanaladi(object.optString("name"));
                        kanal.setDate("date");
                        kanal.setId(object.optString("id"));
                        kanal.setDistance(object.optString("distance"));
                        kanal.setLikesayisi(object.optInt("like_count"));
                        kanal.setLikedurumu(object.optInt("like_status"));
                        channelbaba.add(kanal);
                    }
                } else {
                    in = new BufferedReader(new InputStreamReader(sconnection.getErrorStream()));
                    String inputline = in.readLine();
                    JSONArray jsono = new JSONArray(inputline);
                    for (int i = 0; i < jsono.length(); i++) {
                        JSONObject object = jsono.getJSONObject(i);
                        Kanal kanal = new Kanal(true);
                        kanal.setKanaladi(object.optString("name"));
                        kanal.setDate("date");
                        kanal.setDistance(object.optString("distance"));
                        kanal.setId(object.optString("id"));
                        kanal.setLikedurumu(object.optInt("like_status"));
                        channelbaba.add(kanal);
                    }
                }
                in.close();
                Log.i("tago", "Page Fragment yeniden official gor inputline yazd�m");
            } catch (IOException e) {
                e.printStackTrace();
                Log.i("tago", "Page Fragment yeniden official gor yazamad�m");
            } catch (JSONException e) {
                e.printStackTrace();
                Log.i("tago", "json Exception");
            }

            HttpURLConnection connection = null;
            try {
                connection = (HttpURLConnection) new URL("http://185.22.184.15/shappy/get_channels.php?id=" + id).openConnection();
                Log.i("tago", "Page Fragment1 kanalları yeniden gor bagı kuruldu");
            } catch (IOException e) {
                e.printStackTrace();
            }
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) ");
            connection.setRequestProperty("Accept", "* /*");
            connection.setRequestProperty("Accept-Charset", charset);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

            try {
                OutputStream output = new BufferedOutputStream(connection.getOutputStream());
                output.write(query.getBytes(charset));
                output.close();
                try {
                    int a = connection.getResponseCode();
                    String b = connection.getResponseMessage();
                    Log.i("tago", "rerere1" + a + " " + b);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                BufferedReader in;
                if (connection.getResponseCode() == 200) {
                    in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String inputline = in.readLine();
                    JSONArray jsono = new JSONArray(inputline);
                    for (int i = 0; i < jsono.length(); i++) {
                        JSONObject object = jsono.getJSONObject(i);
                        Kanal kanal = new Kanal(false);
                        kanal.setKanaladi(object.optString("name"));
                        kanal.setKanalurl(object.optString("photo"));
                        kanal.setDate("date");
                        kanal.setDistance(object.optString("distance"));
                        kanal.setLikesayisi(object.optInt("like_count"));
                        kanal.setId(object.optString("id"));
                        channelbaba.add(kanal);
                    }
                } else {
                    in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                    String inputline = in.readLine();
                    JSONArray jsono = new JSONArray(inputline);
                    for (int i = 0; i < jsono.length(); i++) {
                        JSONObject object = jsono.getJSONObject(i);
                        Kanal kanal = new Kanal(false);
                        kanal.setKanaladi(object.optString("name"));
                        kanal.setKanalurl(object.optString("photo"));
                        kanal.setDate("date");
                        kanal.setDistance(object.optString("distance"));
                        kanal.setLikesayisi(object.optInt("like_count"));
                        kanal.setId(object.optString("id"));
                        channelbaba.add(kanal);
                    }
                }
                in.close();
                Log.i("tago", "Page Fragment yeniden kanalları gor inputline yazd�m");
            } catch (IOException e) {
                e.printStackTrace();
                Log.i("tago", "Page Fragment yeniden kanalları gor yazamad�m");
            } catch (JSONException e) {
                e.printStackTrace();
                Log.i("tago", "json Exception");
            }

            return "inputline";
        }

        protected void onPostExecute(String s) {
            kanalAdapter = new KanalAdapter(getActivity(), channelbaba);
            if (viewGroup instanceof AbsListView) {
                int numColumns = (viewGroup instanceof GridView) ? 3 : 1;
                absListView.setAdapter(new QuickReturnAdapter(kanalAdapter, numColumns));
            }

            QuickReturnAttacher quickReturnAttacher = QuickReturnAttacher.forView(viewGroup);
            quickReturnAttacher.addTargetView(bottomTextView, AbsListViewScrollTarget.POSITION_BOTTOM);
            topTargetView = quickReturnAttacher.addTargetView(quickReturnBar,
                    AbsListViewScrollTarget.POSITION_BOTTOM,
                    dpToPx(getActivity(), 50));

            if (quickReturnAttacher instanceof AbsListViewQuickReturnAttacher) {
                AbsListViewQuickReturnAttacher
                        attacher =
                        (AbsListViewQuickReturnAttacher) quickReturnAttacher;
                attacher.addOnScrollListener(PageFragment1.this);
                attacher.setOnItemClickListener(PageFragment1.this);
                attacher.setOnItemLongClickListener(PageFragment1.this);
            }
        }

0 个答案:

没有答案