我如何加载滑动刷新顶部listview android数据

时间:2015-05-22 09:38:28

标签: android listview scroll

目前,我的列表视图显示了当前数据。在滑动时,它将从服务器加载旧数据并存储第一个旧数据和当前数据之后。在刷卡以刷新数据的同时在顶部加载服务器数据。它没有顶级的更新数据。帮我解决这个问题。每次加载数据都在上面。并在刷卡时从上到下刷新数据。

提前谢谢你。我在这里发布我的代码以供审核

public class CommmentActivity extends Activity{

String Curl;
boolean mBuzy;
String UserId;
String VidoId;
String strComment;
EditText etComment;
String mPositionId = "";
String strfriendsSyncDate;
ArrayAdapter<String> mentionList;
ListCommentAdapter commentAdapter;
AutoCompleteTextView aCompletView;
DatabaseHandler db = new DatabaseHandler(this);
List<CommentListInfo> list_CommentInfo = new ArrayList<CommentListInfo>();
ArrayList<MentionBeanClass> mList;
MentionBeanClass addMention;
List<String> SuggetionList;



ImageView IvAddComment;
ListView CommentList;
SessionCreator m_session;
boolean m = false;



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.commentvideo_main);


    SuggetionList =  new ArrayList<String>();
    Curl = GlobalMethod.TokenGenerator() + "&action=getCommentsVideo";

    Intent data = getIntent();
    UserId = data.getStringExtra("userId");
    VidoId = data.getStringExtra("videoId");
    init();

    strfriendsSyncDate = m_session.getfriendsSyncDate();
    new CommentsDetail().execute(UserId,VidoId,strfriendsSyncDate,mPositionId,Curl,"1");
    ArratListBeans.setArMentionList(mList);


    commentAdapter = new ListCommentAdapter(getApplicationContext(), list_CommentInfo);
    CommentList.setAdapter(commentAdapter); 



    CommentList.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

            switch (scrollState) {
            case OnScrollListener.SCROLL_STATE_IDLE:{


                Log.d("ScrollStateCheck", ""+scrollState);



                break;
            }

            case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
            {
                mBuzy = true;
                int count = CommentList.getCount();

                if(CommentList.getFirstVisiblePosition()==0){
                    CommentListInfo c;
                    c = (CommentListInfo) CommentList.getAdapter().getItem(CommentList.getFirstVisiblePosition());
                    mPositionId = c.CommentId;
                    Log.d("mpositionId", mPositionId);
                    String strUrl = GlobalMethod.TokenGenerator() + "&action=getCommentsVideo";
                    new CommentsDetail().execute(UserId,VidoId,strfriendsSyncDate,c.CommentId,strUrl,"1");
                }

                break;
            }

            case OnScrollListener.SCROLL_STATE_FLING:{
                mBuzy = true;
                break;
            }


            default:
                break;
            }
        }


        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub


        }
    });



IvAddComment.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            strComment = etComment.getText().toString();
            etComment.getText().clear();
            String strUrl = GlobalMethod.TokenGenerator() + "&action=addCommentsVideo";

            new CommentsDetail().execute(UserId,VidoId,strComment,strUrl,"0");
            new CommentsDetail().execute(UserId,VidoId,strfriendsSyncDate,Curl,"1");
            commentAdapter.notifyDataSetChanged();
        }
    });



}

private void init() {
    // TODO Auto-generated method stub
    m_session = new SessionCreator(getApplicationContext());
    etComment = (EditText)findViewById(R.id.etComments);
    CommentList = (ListView)findViewById(R.id.lvLatestComments);
    IvAddComment = (ImageView)findViewById(R.id.addComment);
    mList = new ArrayList<MentionBeanClass>();
}

public class CommentsDetail extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub


        ServiceHandler serviceClient;
        String mJsonString;

        try{

            String userId = (String)params[0];
            String vId = (String)params[1];


                String syncdate = (String)params[2];
                String startPosition = (String)params[3];
                String strUrl = (String)params[4];

                List<NameValuePair> paramsNameValuePairs = new ArrayList<NameValuePair>();

                paramsNameValuePairs.add(new BasicNameValuePair("userId", userId));
                paramsNameValuePairs.add(new BasicNameValuePair("videoId", vId));
                paramsNameValuePairs.add(new BasicNameValuePair("startFrom", startPosition));
                paramsNameValuePairs.add(new BasicNameValuePair("friendsSyncDate", syncdate));

                serviceClient = new ServiceHandler();
                mJsonString = serviceClient.makeServiceCall(strUrl,
                        ServiceHandler.POST, paramsNameValuePairs);

                Log.i("---->>>>>>>>>>", paramsNameValuePairs + "");


        Log.d("Response Json-----------------------",mJsonString );


        return mJsonString;

    }catch (Exception e) {
        e.getStackTrace();
    }

    return null;
    }

    @SuppressWarnings({ "unchecked", "unchecked", "unchecked" })
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


        try{
        if (result != null) {
            JSONObject responsJsonObj = new JSONObject(result);
            JSONObject jsonObj = (JSONObject) responsJsonObj
                    .optJSONObject("data");

            try {
                String str = jsonObj.getString("error");
                if ( str != null || str.length() != 0) {
                    // error
                    //Toast.makeText(NewsfeedActivity.this, jsonObj.getString("error"),Toast.LENGTH_LONG).show();
                    return;
                }
            } catch (JSONException je) {
                Log.d("jex ------>", "" + je.getMessage());
                try {
                    String str = jsonObj.getString("message");
                    Log.d("message=", "" + str);
                    if ( str != null || str.length() != 0) {
                        // VALID RESPONSE, NOW PARSE IT
                        if (jsonObj.has("comments")) {
                            JSONArray colArray = jsonObj.getJSONArray("comments");
                            Log.d("Colunm Array", ""+colArray);
                            int nComments = colArray.length();
                            Log.d("# comments", "" + nComments);
                            for(int i=0; i<nComments; i++){
                                JSONObject obj = colArray.getJSONObject(i);
                                Gson Comments = new Gson();
                                String strComments = Comments.toJson(obj);
                                Log.d("# obj=", "" + obj.toString());               
                                CommentListInfo cmtInfo = new CommentListInfo();                
                                cmtInfo.c_userId = obj.getString("userId");
                                cmtInfo.c_name = obj.getString("name");
                                cmtInfo.DateAdded = obj.getString("dateAdded");
                                cmtInfo.CommentId = obj.getString("commentId");
                                cmtInfo.CommentText = obj.getString("text");
                                cmtInfo.ImageUrl = obj.getString("imageLink");
                                list_CommentInfo.add(cmtInfo);  
                            }
                        }
                    }

                    if ( str != null || str.length() != 0) {
                        // VALID RESPONSE, NOW PARSE IT
                        if (jsonObj.has("addPosition")) {
                                Log.d("# obj=", "" + jsonObj.toString());   
                                Log.d("Add Position", jsonObj.getString("addPosition"));
                        }
                    }

                    if ( str != null || str.length() != 0) {
                        // VALID RESPONSE, NOW PARSE IT
                        if (jsonObj.has("mentions")) {
                            JSONArray colArray = jsonObj.getJSONArray("mentions");
                            Log.d("Colunm Array", ""+colArray);
                            int nMention = colArray.length();
                            Log.d("# mentions", "" + nMention);
                            for(int i=0; i<nMention; i++){
                                JSONObject obj = colArray.getJSONObject(i);
                                Log.d("# obj=", "" + obj.toString());   
                                mList.add(new MentionBeanClass(obj.getString("id"), obj.getString("name")));
                                Log.d("Mention Info", "Id : "+obj.getString("id")+"   Name : "+obj.getString("name"));
                            }
                        }
                    }


                    if ( str != null || str.length() != 0) {
                        // VALID RESPONSE, NOW PARSE IT
                        if (jsonObj.has("videoUserData")) {
                                Log.d("# obj=", "" + jsonObj.toString());   
                                Log.d("videoUserData"," "+ jsonObj.getString("videoUserData"));
                        }
                    }



                    if ( str != null || str.length() != 0) {
                        // VALID RESPONSE, NOW PARSE IT
                        if (jsonObj.has("friendsAdded")) {

                            JSONArray colArray = jsonObj.getJSONArray("friendsAdded");
                            String json = colArray.toString();

                            Log.d("Colunm Array", ""+colArray);
                            int nfriendsAdded = colArray.length();
                            Log.d("# friendsAdded", "" + nfriendsAdded);
                            for(int i=0; i<nfriendsAdded; i++){
                                JSONObject obj = colArray.getJSONObject(i);
                                Log.d("# obj=", "" + obj.toString());   

                                String id = obj.getString("userId");
                                String name = obj.getString("name");
                                String imgUrl = obj.getString("imageLink");

                                db.addCustomer(new FriendListInfo(id, name, imgUrl));

                            }

                        }
                    }
                    if ( str != null || str.length() != 0) {
                        // VALID RESPONSE, NOW PARSE IT
                        if (jsonObj.has("friendsDeleted")) {
                            JSONArray colArray = jsonObj.getJSONArray("friendsDeleted");
                            Log.d("Colunm Array", ""+colArray);
                            int nfriendsDeleted = colArray.length();
                            Log.d("# friendsDeleted", "" + nfriendsDeleted);


                            for(int i=0; i<nfriendsDeleted; i++){
                                JSONObject obj = colArray.getJSONObject(i);
                                Log.d("# obj=", "" + obj.toString());   
                            }
                        }else{
                            Log.d("Delete Friends", " No Deleted List");
                        }
                    }
                    if ( str != null || str.length() != 0) {
                        // VALID RESPONSE, NOW PARSE IT
                        if (jsonObj.has("friendsUpdated")) {
                            JSONArray colArray = jsonObj.getJSONArray("friendsUpdated");
                            Log.d("Colunm Array", ""+colArray);
                            int nfriendsUpdated = colArray.length();
                            Log.d("# friendsDeleted", "" + nfriendsUpdated);
                            for(int i=0; i<nfriendsUpdated; i++){
                                JSONObject obj = colArray.getJSONObject(i);
                                Log.d("# obj=", "" + obj.toString());   
                            }
                        }else{
                            Log.d("Update Friends", " No Updated List");
                        }
                    }

                    if ( str != null || str.length() != 0) {
                        // VALID RESPONSE, NOW PARSE IT
                        if (jsonObj.has("friendsSyncDate")) {
                            Log.d("# friendsDeleted", "" + jsonObj);
                                Log.d("# obj=", "" + jsonObj.toString());   

                                String myString = m_session.getfriendsSyncDate();
                                Log.d("myString", myString);

                                m_session.putfriendsSyncDate(jsonObj.getString("friendsSyncDate"));
                                Log.d("Sync Date "," "+jsonObj.getString("friendsSyncDate"));
                        }
                    }

                } catch (JSONException je2) {
                    je2.printStackTrace();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }


    }



}

}

1 个答案:

答案 0 :(得分:0)

添加adapter-listview更新代码

case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
            {
                mBuzy = true;
                int count = CommentList.getCount();

                if(CommentList.getFirstVisiblePosition()==0){
                    CommentListInfo c;
                    c = (CommentListInfo) CommentList.getAdapter().getItem(CommentList.getFirstVisiblePosition());
                    mPositionId = c.CommentId;
                    Log.d("mpositionId", mPositionId);
                    String strUrl = GlobalMethod.TokenGenerator() + "&action=getCommentsVideo";
                    new CommentsDetail().execute(UserId,VidoId,strfriendsSyncDate,c.CommentId,strUrl,"1");
CommentList.setAdapter(commentAdapter); //update code
                }