мой RecyclerView
показывает только 28 предметов. Когда я прокручиваю вниз, он загружает 4 элемента каждый раз. Но это нормально. При достижении 28 предметов он больше не загружается. Данные взяты из PHP-скрипта.
РЕДАКТИРОВАТЬ: я отредактировал скрипт PHP и попробовать / поймать
public class gold extends AppCompatActivity {
private RecyclerView recyclerView;
private GridLayoutManager gridLayoutManager;
private CustomAdapter adapter;
private List<MyData> data_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gold);
//BackButton
if(getSupportActionBar()!=null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
data_list = new ArrayList<>();
load_data_from_server(0);
gridLayoutManager = new GridLayoutManager(this,1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new CustomAdapter(this,data_list);
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
load_data_from_server(data_list.get(data_list.size()-1).getId());
}
}
});
}
private void load_data_from_server(int id) {
AsyncTask<Integer,Void,Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://*************.de/script.php?authkey=******="+integers[0])
.build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
Log.d("Response", response.toString());
for (int i=0; i<array.length(); i++){
JSONObject object = array.getJSONObject(i);
MyData data = new MyData(object.getInt("id"),object.getString("playername"),object.getString("gold"),object.getString("currentUser"),object.getString("currentUid"));
data_list.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
System.out.println("End of content");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
adapter.notifyDataSetChanged();
}
};
task.execute(id);
}
}
Вот PHP-скрипт
$query = "Select * from gold LIMIT 10 OFFSET ($id+1)";
$result = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
Я решил свою проблему.
Это мой PHP-скрипт:
$sql = "SELECT * FROM gold ORDER BY id DESC LIMIT $id, 8";
foreach ($pdo->query($sql) as $row) {
$array[] = $row;
}
header('Content-Type:Application/json');
echo json_encode($array);
Это мой код:
public class gold extends AppCompatActivity {
private RecyclerView recyclerView;
private GridLayoutManager gridLayoutManager;
private CustomAdapter adapter;
private List<MyData> data_list;
private Integer countGold;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gold);
//BackButton
if(getSupportActionBar()!=null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
data_list = new ArrayList<>();
load_data_from_server(0);
gridLayoutManager = new GridLayoutManager(this,1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new CustomAdapter(this,data_list);
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(gridLayoutManager.findLastCompletelyVisibleItemPosition() == data_list.size()-1){
load_data_from_server(data_list.size()-1);
}
}
});
}
private void load_data_from_server(int id) {
AsyncTask<Integer,Void,Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://************.de/script.php?authkey=*********&id="+integers[0])
.build();
try {
Response response = client.newCall(request).execute();
Log.d("Response", response.toString());
JSONArray array = new JSONArray(response.body().string());
for (int i=0; i<array.length(); i++){
JSONObject object = array.getJSONObject(i);
MyData data = new MyData(object.getInt("id"),object.getString("playername"),object.getString("gold"),object.getString("currentUser"),object.getString("currentUid"));
Log.d("Response from Server", object.getString("playername")+" || "+ object.getInt("id"));
data_list.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
System.out.println("End of content");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
adapter.notifyDataSetChanged();
}
};
task.execute(id);
}
Других решений пока нет …