我的sqlite db(android)中存储了近100-120个数据。 我已经完成了将数据提取到回收站视图并在布局中显示但是如何使回收站视图从数据库中仅加载20个数据,当页面滚动到底部时再次获取接下来的20个项目并追加它与20项并显示40项,直到所有数据加载完成。
这是我的代码。
public class MainActivity extends AppCompatActivity {
RecyclerView recycler;
private LinearLayoutManager mLayoutManager;
private BranchAdapter mAdapter;
private static MyDatabase db;
private static SQLiteDatabase sdb;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycler= (RecyclerView)findViewById(R.id.maiinRecycler);
db= new MyDatabase(this);
sdb= db.getWritableDatabase();
mLayoutManager= new LinearLayoutManager(getApplicationContext());
recycler.setLayoutManager(mLayoutManager);
mAdapter = new BranchAdapter(getApplicationContext(),Branch.getdata());
recycler.setAdapter(mAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView search = (SearchView) MenuItemCompat.getActionView(searchItem);
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText)
{
return false;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
public class BranchAdapter extends RecyclerView.Adapter<BranchAdapter.MyViewHolder> {
private Context context;
private ArrayList<BranchInformation> branch;
private LayoutInflater inflater;
String Name;
public BranchAdapter(Context context, ArrayList<BranchInformation> branch) {
this.context = context;
this.branch = branch;
// inflater = LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_card, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder myViewHolder, final int position) {
final TextView Title= (TextView)myViewHolder.view.findViewById(R.id.titleText);
final TextView Pledge= (TextView)myViewHolder.view.findViewById(R.id.pledgedText);
final TextView Backers= (TextView)myViewHolder.view.findViewById(R.id.backersText);
// final LinearLayout BranchLayout =(LinearLayout)myViewHolder.view.findViewById(R.id.BranchL);
Title.setText(branch.get(position).Title);
Pledge.setText("Pleadge: "+branch.get(position).Pledge+" "+branch.get(position).Currency);
Backers.setText("Backers: "+branch.get(position).Backers);
}
@Override
public int getItemCount() {
return branch.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
public View view;
public MyViewHolder(View itemView) {
super(itemView);
view =itemView;
}
}
// This method adds(duplicates) a Object (item ) to our Branch set as well as Recycler View.
}
public static class Branch {
public static ArrayList<BranchInformation> getdata() {
ArrayList<BranchInformation> branch = new ArrayList<BranchInformation>();
// Select All Query
try {
String selectQuery = "SELECT title, amtpledged, numbackers, currency FROM data";
sdb = db.getWritableDatabase();
Cursor cursor = sdb.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
BranchInformation current = new BranchInformation();
current.Title=cursor.getString(0);
current.Pledge= cursor.getString(1);
current.Backers= cursor.getString(2);
current.Currency= cursor.getString(3);
branch.add(current);
} while (cursor.moveToNext());
}
}catch (Exception e) {
// Toast.makeText(,"Error Finding data. Please sync to Update Data.", Toast.LENGTH_SHORT).show();}
}
// return contact list
return branch;
}
}
public static class BranchInformation {
public String Title;
public String Pledge;
public String Backers;
public String Currency;
}
}
答案 0 :(得分:0)
这称为分页。
public class BranchAdapter extends RecyclerView.Adapter<BranchAdapter.MyViewHolder> {
private Context context;
private ArrayList<BranchInformation> branch;
private LayoutInflater inflater;
String Name;
public BranchAdapter(Context context, ArrayList<BranchInformation> branch) {
this.context = context;
this.branch = branch;
// inflater = LayoutInflater.from(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_card, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder myViewHolder, final int position) {
final TextView Title= (TextView)myViewHolder.view.findViewById(R.id.titleText);
final TextView Pledge= (TextView)myViewHolder.view.findViewById(R.id.pledgedText);
final TextView Backers= (TextView)myViewHolder.view.findViewById(R.id.backersText);
// final LinearLayout BranchLayout =(LinearLayout)myViewHolder.view.findViewById(R.id.BranchL);
Title.setText(branch.get(position).Title);
Pledge.setText("Pleadge: "+branch.get(position).Pledge+" "+branch.get(position).Currency);
Backers.setText("Backers: "+branch.get(position).Backers);
}
public void updateList(ArrayList<BranchInformation> list){
int beforeInsertSize = branch.size();
branch.addAll(list)
notifyItemRangeInserted(beforeInsertSize,list.size);
}
@Override
public int getItemCount() {
return branch.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
public View view;
public MyViewHolder(View itemView) {
super(itemView);
view =itemView;
}
}
}
public class MainActivity extends AppCompatActivity {
RecyclerView recycler;
private LinearLayoutManager mLayoutManager;
private BranchAdapter mAdapter;
private static MyDatabase db;
private static SQLiteDatabase sdb;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycler= (RecyclerView)findViewById(R.id.maiinRecycler);
db= new MyDatabase(this);
sdb= db.getWritableDatabase();
mLayoutManager= new LinearLayoutManager(getApplicationContext());
recycler.setLayoutManager(mLayoutManager);
mAdapter = new
BranchAdapter(getApplicationContext(),Branch.getdata());
recycler.setAdapter(mAdapter);
recycler.addOnScrollListener(mScrollListener);
}
RecyclerView.OnScrollListener mScrollListener = new
RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
int visibleItemCount = gridLayoutManager.getChildCount();
int totalItemCount = gridLayoutManager.getItemCount();
int pastVisibleItems = gridLayoutManager.findFirstVisibleItemPosition();
if (pastVisibleItems + visibleItemCount >= totalItemCount) {
List< BranchInformation > list = Branch.getData();
mAdapter.updateList(list);
}
}
};
}
您的查询现在不同了
int offset; //offset will be the last row number you fetched previously.
//Your query will be something like following.
String selectQuery = "SELECT title, amtpledged, numbackers, currency FROM data LIMIT 20 OFFSET "+offset;