0
ユーザがグループに属しているかどうかに応じて、オブジェクトリストに異なる文字列を設定する必要があります。私はToStringをオーバーライドしてRequestクラスを持っていますが、別のリターンで別のクラスが必要です。私はtoStringをパラメーターとスイッチケースで内部的に行うことを考えましたが、アダプターを介してtoStringを呼び出す方法はわかりません。この目的のためにリストビュー用の複数のtoString()<object>アダプタ
package com.example.krzysztof.mywallet;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import com.example.krzysztof.DBCorrespondents.AcceptRequest;
import com.example.krzysztof.DBCorrespondents.GetInvites;
import com.example.krzysztof.DBCorrespondents.GetMembers;
import com.example.krzysztof.DBCorrespondents.GetRequests;
import com.example.krzysztof.DBCorrespondents.IgnoreRequest;
import com.example.krzysztof.DBCorrespondents.MemberDelete;
import com.example.krzysztof.Models.Request;
import com.example.krzysztof.Models.User;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class PendingRequests extends AppCompatActivity {
Request request;
RequestQueue queue;
SharedPreferences sharedPreferences;
ArrayList <Request> listItems = new ArrayList <> ();
ArrayAdapter <Request> adapter;
ListView listview;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_pending_requests);
listview = (ListView) findViewById (R.id.requestslist);
adapter = new ArrayAdapter <> (this,
android.R.layout.simple_list_item_1,
listItems);
listview.setAdapter (adapter);
sharedPreferences = getSharedPreferences (getString (R.string.PREFERENCES), Context.MODE_PRIVATE);
final String GroupId = sharedPreferences.getString (getString (R.string.P_GROUPID), null);
final String Username= sharedPreferences.getString (getString (R.string.P_USERNAME),null);
if(GroupId==null)
{
Response.Listener <String> responseListener = new Response.Listener <String> () {
@Override
public void onResponse (String response) {
try {
JSONArray jsonResponse = new JSONArray (response);
if(jsonResponse.length()==0)
{
Context context = getApplicationContext ();
CharSequence text = "Brak zgłoszeń!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText (context, text, duration);
toast.show ();
}
else {
for (int i = 0 ; i < jsonResponse.length () ; i++) {
JSONObject jsonobject = jsonResponse.getJSONObject (i);
int Id = jsonobject.getInt ("Id");
String Username = jsonobject.getString ("Username");
int Status = jsonobject.getInt ("Status");
String Date = jsonobject.getString ("Date");
request = new Request (Id, GroupId, Username, Status, Date);
listItems.add (request);
adapter.notifyDataSetChanged ();
}
}
} catch (JSONException e) {
e.printStackTrace ();
}
}
};
queue = Volley.newRequestQueue (PendingRequests.this);
GetInvites getInvites = new GetInvites (Username, responseListener);
queue.add (getInvites);
registerForContextMenu (listview);
}
else
{
Response.Listener <String> responseListener = new Response.Listener <String> () {
@Override
public void onResponse (String response) {
try {
JSONArray jsonResponse = new JSONArray (response);
if(jsonResponse.length()==0)
{
Context context = getApplicationContext ();
CharSequence text = "Brak zgłoszeń!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText (context, text, duration);
toast.show ();
}
else {
for (int i = 0 ; i < jsonResponse.length () ; i++) {
JSONObject jsonobject = jsonResponse.getJSONObject (i);
int Id = jsonobject.getInt ("Id");
String Username = jsonobject.getString ("Username");
int Status = jsonobject.getInt ("Status");
String Date = jsonobject.getString ("Date");
request = new Request (Id, GroupId, Username, Status, Date);
listItems.add (request);
adapter.notifyDataSetChanged ();
}
}
} catch (JSONException e) {
e.printStackTrace ();
}
}
};
queue = Volley.newRequestQueue (PendingRequests.this);
GetRequests getRequests = new GetRequests (GroupId, responseListener);
queue.add (getRequests);
registerForContextMenu (listview);
}
}
@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu (menu, v, menuInfo);
MenuInflater inflater = getMenuInflater ();
inflater.inflate (R.menu.request_menu, menu);
}
@Override
public boolean onContextItemSelected (MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo ();
switch (item.getItemId ()) {
case R.id.accept:
accept (info);
return true;
case R.id.ignore:
ignore(info);
return true;
default:
return super.onContextItemSelected (item);
}
}
と要求クラス
package com.example.krzysztof.Models;
/**
* Created by Krzysztof on 10.11.2017.
*/
public class Request {
private int Id, Status;
private String Username, GroupId, Date;
public Request(int Id, String GroupId, String Username, int Status, String Date)
{
this.Id=Id;
this.GroupId=GroupId;
this.Username=Username;
this.Status=Status;
this.Date=Date;
}
public int getId() {
return Id;
}
public String getDate() {
return Date;
}
public String getGroupId() {
return GroupId;
}
public int getStatus() {
return Status;
}
public String getUsername() {
return Username;
}
@Override
public String toString() {
return this.Username+" wysłał prośbę o dołączenie";
}
}