Commit b7b8e32e authored by fpeterfalvi's avatar fpeterfalvi
Browse files

RecyclerView crash fix.

IndexOutOfBoundsException fixed.
parent 7a4e67d3
......@@ -213,11 +213,11 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
//-----Socket-----//
try {
Socket s = new Socket(hostAddress,hostPort);
ServerModel serverModel = new ServerModel(serviceInfo, s);
final ServerModel serverModel = new ServerModel(serviceInfo, s);
servers.add(serverModel);
runOnUiThread(new Runnable() {
public void run() {
adapter.addItem();
adapter.addItem(serverModel);
}
});
......@@ -309,7 +309,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
case "Hostname":
int serverIndex = servers.indexOf(serverModel);
serverModel.setHostName(params[1]);
updateConversationHandler.post(new updateUIThread(Operation.UPDATE_SERVER, Integer.toString(serverIndex)));
updateConversationHandler.post(new updateUIThread(Operation.UPDATE_SERVER, serverModel));
break;
case "Connect":
if(params[1].equals("Ok!")) {
......@@ -323,7 +323,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
if(server.getSocket() == serverModel.getSocket())
{
int position = servers.indexOf(serverModel);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_SERVER, Integer.toString(position)));
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_SERVER, serverModel));
servers.remove(serverModel);
serverModel.getSocket().close();
threads.remove(Thread.currentThread());
......@@ -337,7 +337,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
if(server.getSocket() == serverModel.getSocket())
{
int position = servers.indexOf(serverModel);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_SERVER, Integer.toString(position)));
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_SERVER, serverModel));
servers.remove(serverModel);
serverModel.getSocket().close();
threads.remove(Thread.currentThread());
......@@ -354,7 +354,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
for (ServerModel server : servers) {
if (server.getSocket() == serverModel.getSocket()) {
int position = servers.indexOf(server);
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_SERVER, Integer.toString(position)));
updateConversationHandler.post(new updateUIThread(Operation.REMOVE_SERVER, serverModel));
servers.remove(server);
break;
}
......@@ -371,24 +371,24 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
class updateUIThread implements Runnable {
private Operation op;
private String msg;
private ServerModel sm;
public updateUIThread(Operation op, String str) {
public updateUIThread(Operation op, ServerModel sm) {
this.op = op;
this.msg = str;
this.sm = sm;
}
@Override
public void run() {
switch (op) {
case ADD_SERVER:
adapter.addItem();
adapter.addItem(sm);
break;
case REMOVE_SERVER:
adapter.removeItem(Integer.parseInt(msg));
adapter.removeItem(sm);
break;
case UPDATE_SERVER:
adapter.updateItem(Integer.parseInt(msg));
adapter.updateItem(sm);
default:
Log.d("updateUIThread", "Unknown operation");
break;
......@@ -399,7 +399,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
private void initRecyclerView() {
recyclerView = (RecyclerView) findViewById(R.id.MainRecyclerView);
adapter = new ServerListAdapter(this, servers);
adapter = new ServerListAdapter(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(adapter);
......
......@@ -7,6 +7,7 @@ import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import onlab.kvizclient.model.ConnectionStatus;
......@@ -14,12 +15,11 @@ import onlab.kvizclient.model.ServerModel;
public class ServerListAdapter extends RecyclerView.Adapter<ServerListAdapter.ServerViewHolder> {
private final List<ServerModel> items;
private final List<ServerModel> items = new ArrayList<>();
private final ServerConnection serverConnection;
public ServerListAdapter(ServerConnection sc, List<ServerModel> servers) {
items = servers;
public ServerListAdapter(ServerConnection sc) {
serverConnection = sc;
}
......@@ -95,7 +95,8 @@ public class ServerListAdapter extends RecyclerView.Adapter<ServerListAdapter.Se
}
}
public void addItem() {
public void addItem(ServerModel sm) {
items.add(sm);
notifyItemInserted(items.size() - 1);
}
......@@ -103,11 +104,14 @@ public class ServerListAdapter extends RecyclerView.Adapter<ServerListAdapter.Se
notifyDataSetChanged();
}
public void updateItem(int index) {
notifyItemChanged(index);
public void updateItem(ServerModel sm) {
int position = items.indexOf(sm);
notifyItemChanged(position);
}
public void removeItem(int position) {
public void removeItem(ServerModel sm) {
int position = items.indexOf(sm);
items.remove(position);
notifyItemRemoved(position);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment