خرید بک لینک

Vote count: 0

I Am trying to implement swipe to refresh in my app. When swipe to refresh is triggered first all the data is fetched from JSON and than store in database. after storing the data all the new data will be added to top of the recyclerview. I read few articles and only get the detail about adding only one item at the top of the list. But in the case of my app more than one items may be available to show then what should I do ?

I am adding my codding part.

Here is my code for database table :

@Override
public ArrayList<Post> getAllNewPost(int T) {
    SQLiteDatabase db = this.getReadableDatabase();
    ArrayList<Post> postList = null;
    try {
        postList = new ArrayList<Post>();

        String QUERY = "SELECT * FROM " + TABLE_NAME + " INNER JOIN "+TABLE_FOLLOWER+
                " ON post_table.user_id = follower.user_id WHERE follower.follow = '1' AND post_table.post_id > "+T+" ORDER BY "+POST_ID+" DESC";
        Cursor cursor = db.rawQuery(QUERY, null);
        if (!cursor.isLast()) {
            while (cursor.moveToNext()) {
                Post post = new Post();
                post.setPost_id(cursor.getString(0));
                Log.d("post_id",cursor.getString(0));
                post.setUser_id(cursor.getString(1));
                post.setUser_name(cursor.getString(2));
                post.setImg_link(cursor.getString(3));
                post.setPost_title(cursor.getString(4));
                post.setPost_cat(cursor.getString(6));
                post.setPost_time(cursor.getString(8));
                postList.add(post);
            }
        }
        db.close();
    } catch (Exception e) {
        Log.e("error", e + "");
    }
    retu postList;
}

here is array adapter of recyclerview :

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {

Context context;
ArrayList<Post> listData = new ArrayList<>();
String rpostid,ruserid,ame,rcat,rdate,rtittle,urlThumnail;
private VolleySingleton volleySingleton;
ImageLoader imageLoader = VolleySingleton.getsInstance().getImageLoad();

public PostAdapter(ArrayList<Post> postList) {
    this.listData=postList;
}

public PostAdapter(Context context){
    this.context = context;
}

@Override
public PostAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.post_item, viewGroup, false);
    retu new posts(v);
}

public class posts extends ViewHolder {
    TextView tvTittle,tvPostId,tvUseId,tvName,tvCat,tvDate;
    ImageView row_img;
    public posts(View v) {
        super(v);
        tvTittle = (TextView) v.findViewById(R.id.row_cmnt);
        tvPostId = (TextView) v.findViewById(R.id.row_postid);
        tvUseId = (TextView) v.findViewById(R.id.row_userid);
        tvName = (TextView) v.findViewById(R.id.row_name);
        tvCat = (TextView) v.findViewById(R.id.row_cat);
        tvDate = (TextView) v.findViewById(R.id.row_date);
        row_img = (ImageView) v.findViewById(R.id.row_img);

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent;
                TextView text = (TextView) v.findViewById(R.id.row_postid);
                String lst_txt = text.getText().toString().trim();
                intent = new Intent(v.getContext(), PostView.class);
                intent.putExtra("post_id", lst_txt);
                v.getContext().startActivity(intent);
            }
        });
    }
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    final posts holder = (posts) viewHolder;
    Post post = listData.get(position);
    rpostid = post.getPost_id();
    ruserid = post.getUser_id();
    ame = post.getUser_name();
    rcat = post.getPost_cat();
    rdate = post.getPost_time();
    rtittle = post.getPost_title();
    holder.tvPostId.setText(rpostid);
    holder.tvUseId.setText(ruserid);
    holder.tvName.setText(ame);
    holder.tvCat.setText(rcat);
    holder.tvDate.setText(rdate);
    holder.tvTittle.setText(rtittle);
    urlThumnail = post.getImg_link();
    Log.d("qwerty","link of image lru: "+ urlThumnail);
    if (urlThumnail != null){
        imageLoader.get(urlThumnail, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                holder.row_img.setImageBitmap(response.getBitmap());
                Log.d("qwerty","post attached 2");
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("qwerty","post attached 3");
            }
        });
    }
}

@Override
public int getItemCount() {
    retu (null != listData ? listData.size() : 0);
}

}

and here is the code by which all the new post will be fetched from db:

postList = handler.getAllNewPost(t);
adapter.notifyItemInserted(1);
Hpbar.setVisibility(View.INVISIBLE);
swipeRefreshLayout.setRefreshing(false);

But after refreshing new data will not shown in list. SO please tell me how to do so. I read few article where add is used

listview.add(0, item);

But I don't know how to implement this with multiple rows of data at same time. Thanks in advance.

asked 40 secs ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 9 تير 1395 ساعت: 23:26

صفحه بندی