gpt4 book ai didi

java - 如何在内部匿名方法中写入 List (class var)?

转载 作者:行者123 更新时间:2023-11-29 02:35:45 25 4
gpt4 key购买 nike

全局思路:从 REST API ( https://jsonplaceholder.typicode.com/users/1) 获取数据,然后将数据添加到我的 Activity 布局上的 ListView。

我在匿名类中获取数据(使用改造库),但我如何将它写入 ArrayList 用户(类 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
}
});
}
}

关于java - 如何在内部匿名方法中写入 List (class var)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47224164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com