listview在滚动时蠢蠢欲动

时间:2014-08-26 11:21:41

标签: android listview

我有一个应用程序,我从api获取数据,当数据加载,我试图滚动列表视图,它开始抽搐,我试图找到解决方案,但没有得到任何东西。请帮助我解决它。

InboxActivity.java

    list=(ListView)rootView.findViewById(R.id.list);
     catagery = new ArrayList<ProfileInbox>();

     String fontPath = "font/Roboto-Regular.ttf";

    // Loading Font Face
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), fontPath);
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View arg1,
                int position, long id) {
            // TODO Auto-generated method stub
            //arg0.getp.setBackgroundColor(Color.WHITE);
            //parent.getChildAt(position).setBackgroundColor(Color.BLUE);

         IsRead=true;
         catagery.get(position).setRead(IsRead);


            new Task().execute(url);

            adapter.notifyDataSetChanged();
            msg=catagery.get(position).getMessage();
            dateFrom=catagery.get(position).getSentDate();
            sub=catagery.get(position).getSubject();
            Intent i= new Intent(getActivity(),InboxDetail.class);
            startActivity(i);

        }
    });

    return rootView;
}


 private void loadNextPageOfReviews()
 {
  page_no_count += 1;



  new JSONAsyncTask().execute(loadMoreUrl);
 }  

class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Please Wait, Loading...");

        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            // ------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("inbox");

                String unread_msg= jsono.getString("unread_msg");
                Log.i("unreadMsg", unread_msg);

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject c = jarray.getJSONObject(i);
                    ProfileInbox category = new ProfileInbox();


                    String id = c.getString("msg_id");

                    String sub = c.getString("subject");
                    String name = c.getString("message");
                    String imageSetter=c.getString("sent_on");
                    //Log.i("id", id);
                    //Log.i("name", name);
                    //Log.i("imageSetter", imageSetter);

                    category.setMsgId(((JSONObject) c).getString("msg_id"));
                    if(unread_msg.contains(id)){
                        category.setRead(false);
                    }
                    else{
                        category.setRead(true);
                    }
                    category.setSubject(((JSONObject) c).getString("subject"));
                    category.setMessage(((JSONObject) c).getString("message"));
                    category.setSentDate(((JSONObject) c).getString("sent_on"));
                    //Log.i("category", category.toString());

                    catagery.add(category);
                    //Log.i("category", category.toString());
                }
                return true;
            }

            // ------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        dialog.cancel();

        if (result == false){
            Toast.makeText(getActivity(),
                    "Unable to fetch data from server", Toast.LENGTH_LONG)
                    .show();
            }
        else if(Blank.notice.equals("true")){

                msg=catagery.get(0).getMessage();
                dateFrom=catagery.get(0).getSentDate();
                sub=catagery.get(0).getSubject();
                adapter = new InboxAdaptor(getActivity(),
                        catagery);
                list.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                Intent i= new Intent(getActivity(),InboxDetail.class);
                startActivity(i);
            }
        else if(Blank.notice.equals("false"))
            {
                //adapter.notifyDataSetChanged();
            adapter = new InboxAdaptor(getActivity(),
                    catagery);
            list.setAdapter(adapter);
            adapter.notifyDataSetChanged();

            }

    }

InboxAdapter

public class InboxAdaptor extends BaseAdapter {
    private List<ProfileInbox> originalData;
    private List<ProfileInbox> filteredData;
    private Context context;
    public static String url;
    public static String bussinessId;
    public InboxAdaptor(Context context, ArrayList<ProfileInbox> Data) {
        this.context = context;
        this.originalData = Data;
        //Log.i("originalData", Data.toString());
        filteredData = new ArrayList<ProfileInbox>();
        filteredData.addAll(this.originalData);
        //Log.i("filterData", filteredData.toString());


    }

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

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

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.inboxlist, null,false);
            holder.coloredlay=(RelativeLayout)convertView.findViewById(R.id.coloredlay);
            holder.txtWelcom = (TextView) convertView.findViewById(R.id.txtWelcom);
            holder.dateTime = (TextView) convertView.findViewById(R.id.dateTime);
            holder.txtdetails = (TextView) convertView.findViewById(R.id.txtdetails);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }


        if(filteredData.get(position).getRead()==true)
        {

            holder.coloredlay.setBackgroundColor(Color.WHITE);
             notifyDataSetChanged();
        }else{
            holder.coloredlay.setBackgroundColor(Color.parseColor("#E5E4E2"));
        }

       // holder.img.setTag(position);
        String fontPath = "font/Roboto-Regular.ttf";

        // Loading Font Face
        Typeface tf = Typeface.createFromAsset(convertView.getContext().getAssets(), fontPath);





        holder.txtWelcom.setText(filteredData.get(position).getSubject());
        holder.txtWelcom.setTypeface(tf);
        holder.dateTime.setText(filteredData.get(position).getSentDate());
        holder.dateTime.setTypeface(tf);
        holder.txtdetails.setText(filteredData.get(position).getMessage());
        holder.txtdetails.setTypeface(tf);



 /*       if(Blank.notice.equals("true")){
            holder.coloredlay.setBackgroundColor(Color.WHITE);
             notifyDataSetChanged();
        }
        */
       notifyDataSetChanged();
        return convertView;
    }

    public static class ViewHolder {
        public RelativeLayout coloredlay;
        public TextView txtdetails;
        public TextView dateTime;
        public TextView txtWelcom;

    }

3 个答案:

答案 0 :(得分:2)

从getView()中删除notifyDataSetChanged(),当您为适配器提供的数据发生更改时,notifyDataSetChanged()将更新适配器,但在更改列表视图时使用该错误。< / p>

答案 1 :(得分:0)

从getView()中删除notifyDataSetChanged()。

你需要点到这里。而是在onPostExecute()中调用JSONAsyncTask中删除notifyDataSetChanged()

答案 2 :(得分:0)

使用cachecolorHint来解决问题:

<ListView
    android:id="@+id/listNewsMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="@android:color/transparent" >
</ListView>