【问题标题】:Change ListView element in fragment更改片段中的 ListView 元素
【发布时间】:2018-11-24 16:58:40
【问题描述】:

我想在包含卡片的片段中使用 for 循环(如图所示)更改每个“测试测试”TextView,以便它显示,例如,它在 ArrayAdapter 中的位置。 (所以,Arnaud Dubois 有“1”而不是“test test”,John Snow 有“2”,依此类推)...

我坚持这应该在片段中完成,而不是在 arrayadapter 类中。

似乎我无法从片段本身访问 TextView,只能在 ArrayAdapter 类中访问。我在这里有一些片段代码,但元素没有改变。应该更新每个元素的代码在“onConfirmedFriendsReceivedEvent”中。

public class FriendsFragment extends BaseFragment implements MaterialSearchBar.OnSearchActionListener {

ListView mFriendsList;
ConfirmedFriendsAdapter adapter;

private MaterialSearchBar searchBar;

View root_view;

public FriendsFragment(){

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_friends, container, false);

    root_view = view;

    initUI(view);
    initEvent(view);

    return view;
}

public static FriendsFragment newInstance(@Nullable Bundle params){
    Bundle args = (params == null) ? new Bundle() : params;
    FriendsFragment fragment = new FriendsFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
void initUI(@Nullable final View view){
    mFriendsList = view.findViewById(R.id.friends_list);
    mFriendsList.setEmptyView(view.findViewById(R.id.empty));

    searchBar = (MaterialSearchBar) root_view.findViewById(R.id.friends_searchBar);
    searchBar.setHint("Search friends...");
    //enable searchbar callbacks
    searchBar.setOnSearchActionListener(this);

}

@Override
void initEvent(@Nullable final View view){
    FriendsDataManager.getConfirmedFriends();
}

@Override
public void onSearchStateChanged(boolean enabled) {
    String s = enabled ? "enabled" : "disabled";
    Toast.makeText(getContext(), "Search " + s, Toast.LENGTH_SHORT).show();
    //if (!searchBar.isSearchEnabled()) searchBar.enableSearch();
}

@Override
public void onSearchConfirmed(CharSequence text) {
    String search = text.toString();
    FriendsDataManager.getSearchedFriends(search);
}

@Override
public void onButtonClicked(int buttonCode) {
    switch (buttonCode) {
        case MaterialSearchBar.BUTTON_NAVIGATION:
            Toast.makeText(getContext(), "Machin truc", Toast.LENGTH_SHORT).show();
            break;
        case MaterialSearchBar.BUTTON_SPEECH:
            break;
        case MaterialSearchBar.BUTTON_BACK:
            FriendsDataManager.getConfirmedFriends();
            break;
    }
}

@Subscribe
public void onConfirmedFriendsReceived(ConfirmedFriendsEvent event){
    adapter = new ConfirmedFriendsAdapter(getActivity(), event.mConfirmedFriends);
    mFriendsList.setAdapter(adapter);
    adapter.setNotifyOnChange(true);
    for(int i=0; i<adapter.getCount(); i++){
        View v = getViewByPosition(i, mFriendsList);
        TextView msg = v.findViewById(R.id.friend_message);
        msg.setText(i);
    }
}

public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

这里是 ArrayAdapter 类的代码。请注意,“setMail”和“getMail”方法(到目前为止)无法从片段中访问。我希望能够从片段中访问它。

public class ConfirmedFriendsAdapter extends ArrayAdapter<ConfirmedFriend> {

private final List<ConfirmedFriend> mConfirmedFriends;
private final Context mContext;

private GeoDataClient geoDataClient;
private Place currentPlace;

String mail;

TextView friend_message;
TextView friend_position;

public ConfirmedFriendsAdapter(@NonNull final Context context, @NonNull final List<ConfirmedFriend> objects){
    super(context, R.layout.fragment_friends, objects);
    mConfirmedFriends = objects;
    mContext = context;
}

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent){

    final ConfirmedFriend confirmedFriend = mConfirmedFriends.get(position);
    if(convertView == null){
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_friends_feed, parent, false);
    }

    mail = confirmedFriend.getDest_user().getMail();

    TextView friend_name = convertView.findViewById(R.id.friend_name);
    TextView friend_asl = convertView.findViewById(R.id.friend_asl);
    friend_message = convertView.findViewById(R.id.friend_message);
    friend_position = convertView.findViewById(R.id.friend_position);


    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    String now = dateFormat.format(date);

    try {
        Date birthdate = new SimpleDateFormat("yyyy-MM-dd").parse(confirmedFriend.getDest_user().getBirthday());
        Date nowDate = new SimpleDateFormat("yyyy-MM-dd").parse(now);
        // validate inputs ...
        DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        int d1 = Integer.parseInt(formatter.format(birthdate));
        int d2 = Integer.parseInt(formatter.format(nowDate));
        int age = (d2 - d1) / 10000;
        friend_asl.setText(confirmedFriend.getDest_user().getGender() + ", " + age);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    friend_name.setText(confirmedFriend.getDest_user().getFirstName() + " " + confirmedFriend.getDest_user().getLastName());


    return convertView;

}

public String getMail() {
    return mail;
}

public void setMail(String mail) {
    this.mail = mail;
}

【问题讨论】:

    标签: android listview android-fragments adapter


    【解决方案1】:

    如果您想在 Fragment 中执行此操作,请更新 ArrayAdapter 的列表元素并在适配器上调用 notifyDataSetChanged(),这将刷新列表。

    在你的情况下:

    1. 添加新变量或使用ConfirmedFriend 中的现有变量。
    2. 更新onConfirmedFriendsReceived() 中的变量。
    3. 更新mConfirmedFriends 列表后将其传递给适配器。
    4. 在适配器或片段中调用notifyDatasetChanged()
    5. ArrayAdaptergetView() 方法中,将ConfirmedFriend 的变量设置为TextView。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-14
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多