如何在内部匿名方法中写入 List (class var)?
How to write into List (class var) in inner anonymous method?
全局思路:
从 REST API (https://jsonplaceholder.typicode.com/users/1) 获取数据,然后将数据添加到我的 activity 布局上的 ListView。
我正在获取匿名数据 class(使用改装库),但我如何将其写入 ArrayList 用户(class var)以实例化我的 ListViewAdapter?
public class CardFragmentUsers extends Fragment implements CardFragment {
private CardView cardView;
ArrayList<User> users = new ArrayList<User>();
UsersListViewAdapter usersListViewAdapter;
public static Fragment getInstance(int position){
CardFragmentUsers f = new CardFragmentUsers();
Bundle args = new Bundle();
args.putInt("position", position);
f.setArguments(args);
return f;
}
@SuppressLint("DefaultLocale")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_viewpager_users, container, false);
cardView = (CardView) view.findViewById(R.id.usersCardView);
cardView.setMaxCardElevation(cardView.getCardElevation() * CardAdapter.MAX_ELEVATION_FACTOR);
Button usersApplyButton = (Button)view.findViewById(R.id.usersApplyButton);
usersApplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View parent = (View)view.getParent();
final TextView userCardviewGuideText = parent.findViewById(R.id.userCardviewGuideText);
if (InternetConnection.checkConnection(getContext())){
ApiService api = RetroClient.getApiService();
// getting and filling data
// for (int i = 1; i <= 5; i++) {
Call<User> call = api.getUser(1);
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()){
userCardviewGuideText.setVisibility(View.INVISIBLE);
User user = new User();
user.setName(response.body().getName());
user.setUsername(response.body().getUsername());
users.add(user);
}else {
userCardviewGuideText.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), getString(R.string.smth_went_wrong), Toast.LENGTH_SHORT).show();
return;
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
});
// }
usersListViewAdapter = new UsersListViewAdapter(parent.getContext(), users);
ListView usersListView = (ListView) parent.findViewById(R.id.usersListView);
usersListView.setAdapter(usersListViewAdapter);
}else {
Toast.makeText(getActivity(), getString(R.string.no_internet_connection), Toast.LENGTH_SHORT).show();
userCardviewGuideText.setVisibility(View.VISIBLE);
}
}
});
return view;
}
public CardView getCardView() {return cardView;}
}
试试这个:
注意:我删除了一部分我没有更改的代码(为了清楚起见)。
public class CardFragmentUsers extends Fragment implements CardFragment {
private CardView cardView;
ArrayList<User> users = new ArrayList<User>();
UsersListViewAdapter usersListViewAdapter;
TextView userCardviewGuideText;
// ...
// Your code
@SuppressLint("DefaultLocale")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
Bundle savedInstanceState) {
// ...
// Your code
// Define the TextView here
userCardviewGuideText = findViewById(R.id.userCardviewGuideText);
// Create your adapter outside the listener
usersListViewAdapter = new UsersListViewAdapter(getContext(), users);
ListView usersListView = (ListView) view.findViewById(R.id.usersListView);
usersListView.setAdapter(usersListViewAdapter);
usersApplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (InternetConnection.checkConnection(getContext())) {
retrieveUserInfoFromApi(); // Using a separate method make the code clearer
} else {
Toast.makeText(getActivity(), getString(R.string.no_internet_connection), Toast.LENGTH_SHORT).show();
userCardviewGuideText.setVisibility(View.VISIBLE);
}
}
});
return view;
}
public CardView getCardView() {
return cardView;
}
public void retrieveUserInfoFromApi() {
ApiService api = RetroClient.getApiService();
Call<User> call = api.getUser(1);
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
userCardviewGuideText.setVisibility(View.INVISIBLE);
User user = new User();
user.setName(response.body().getName());
user.setUsername(response.body().getUsername());
users.add(user);
// Notify the adapter that the data have changed
usersListViewAdapter.notifyDataSetChanged();
} else {
// ...
// Your code
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
// Do something on error
}
});
}
}
全局思路: 从 REST API (https://jsonplaceholder.typicode.com/users/1) 获取数据,然后将数据添加到我的 activity 布局上的 ListView。
我正在获取匿名数据 class(使用改装库),但我如何将其写入 ArrayList 用户(class var)以实例化我的 ListViewAdapter?
public class CardFragmentUsers extends Fragment implements CardFragment {
private CardView cardView;
ArrayList<User> users = new ArrayList<User>();
UsersListViewAdapter usersListViewAdapter;
public static Fragment getInstance(int position){
CardFragmentUsers f = new CardFragmentUsers();
Bundle args = new Bundle();
args.putInt("position", position);
f.setArguments(args);
return f;
}
@SuppressLint("DefaultLocale")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_viewpager_users, container, false);
cardView = (CardView) view.findViewById(R.id.usersCardView);
cardView.setMaxCardElevation(cardView.getCardElevation() * CardAdapter.MAX_ELEVATION_FACTOR);
Button usersApplyButton = (Button)view.findViewById(R.id.usersApplyButton);
usersApplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View parent = (View)view.getParent();
final TextView userCardviewGuideText = parent.findViewById(R.id.userCardviewGuideText);
if (InternetConnection.checkConnection(getContext())){
ApiService api = RetroClient.getApiService();
// getting and filling data
// for (int i = 1; i <= 5; i++) {
Call<User> call = api.getUser(1);
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()){
userCardviewGuideText.setVisibility(View.INVISIBLE);
User user = new User();
user.setName(response.body().getName());
user.setUsername(response.body().getUsername());
users.add(user);
}else {
userCardviewGuideText.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(), getString(R.string.smth_went_wrong), Toast.LENGTH_SHORT).show();
return;
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
});
// }
usersListViewAdapter = new UsersListViewAdapter(parent.getContext(), users);
ListView usersListView = (ListView) parent.findViewById(R.id.usersListView);
usersListView.setAdapter(usersListViewAdapter);
}else {
Toast.makeText(getActivity(), getString(R.string.no_internet_connection), Toast.LENGTH_SHORT).show();
userCardviewGuideText.setVisibility(View.VISIBLE);
}
}
});
return view;
}
public CardView getCardView() {return cardView;}
}
试试这个:
注意:我删除了一部分我没有更改的代码(为了清楚起见)。
public class CardFragmentUsers extends Fragment implements CardFragment {
private CardView cardView;
ArrayList<User> users = new ArrayList<User>();
UsersListViewAdapter usersListViewAdapter;
TextView userCardviewGuideText;
// ...
// Your code
@SuppressLint("DefaultLocale")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
Bundle savedInstanceState) {
// ...
// Your code
// Define the TextView here
userCardviewGuideText = findViewById(R.id.userCardviewGuideText);
// Create your adapter outside the listener
usersListViewAdapter = new UsersListViewAdapter(getContext(), users);
ListView usersListView = (ListView) view.findViewById(R.id.usersListView);
usersListView.setAdapter(usersListViewAdapter);
usersApplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (InternetConnection.checkConnection(getContext())) {
retrieveUserInfoFromApi(); // Using a separate method make the code clearer
} else {
Toast.makeText(getActivity(), getString(R.string.no_internet_connection), Toast.LENGTH_SHORT).show();
userCardviewGuideText.setVisibility(View.VISIBLE);
}
}
});
return view;
}
public CardView getCardView() {
return cardView;
}
public void retrieveUserInfoFromApi() {
ApiService api = RetroClient.getApiService();
Call<User> call = api.getUser(1);
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
userCardviewGuideText.setVisibility(View.INVISIBLE);
User user = new User();
user.setName(response.body().getName());
user.setUsername(response.body().getUsername());
users.add(user);
// Notify the adapter that the data have changed
usersListViewAdapter.notifyDataSetChanged();
} else {
// ...
// Your code
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
// Do something on error
}
});
}
}