gpt4 book ai didi

android - 为什么实际的字符串没有显示在 recyclerView 中?

转载 作者:行者123 更新时间:2023-11-30 00:48:56 25 4
gpt4 key购买 nike

大家好,我正在尝试创建一个待办事项 android 应用程序。还有就是recyclerView可以动态添加。我将 float 操作按钮附加到我的布局。当按下晶圆厂时,它会显示一个对话框。该对话框包含一个 EditText 和一个 Done 按钮。当按下完成按钮时,editText 中的值将提供给回收器。我使用 setter 和 getter 将数据分配给 recycler

我的问题:recycler 不显示 String 类型的文本,它显示其他数据,如

enter image description here

HomeFragment 类

package com.example.app.wounderlist.todolist;


import android.app.Dialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.app.wounderlist.todolist.data.CRUD;
import com.example.app.wounderlist.todolist.data.SpaceCraft;
import com.example.app.wounderlist.todolist.data.SpaceCraftCollection;
import com.example.app.wounderlist.todolist.recycler.MyAdapter;

import java.util.ArrayList;

public class HomeFragment extends Fragment {
FloatingActionButton mFab;
RecyclerView mRecyclerView;
MyAdapter mAdapter;
CRUD mCRUD;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);

mFab = (FloatingActionButton) view.findViewById(R.id.fab);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);


mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

mCRUD = new CRUD(SpaceCraftCollection.getSpaceCrafts());
mAdapter = new MyAdapter(getActivity(), mCRUD.getSpaceCrafts());

mFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayDialog();
}
});


return view;
}

//Add Data
//Show Dialog
private void displayDialog() {
Dialog dialog = new Dialog(getActivity());
dialog.setTitle("Add New Group");
dialog.setContentView(R.layout.dialog_layout);

final EditText editText = (EditText) dialog.findViewById(R.id.editField);
Button done = (Button) dialog.findViewById(R.id.doneButton);

done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SpaceCraft s = new SpaceCraft();
s.setTitle(editText.toString());

if (mCRUD.addNew(s)) {

editText.setText("");

mRecyclerView.setAdapter(mAdapter);
}
}
});

dialog.show();

}

}

MyAdapter 类

package com.example.app.wounderlist.todolist.recycler;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.app.wounderlist.todolist.R;
import com.example.app.wounderlist.todolist.data.SpaceCraft;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
Context mContext;
ArrayList<SpaceCraft> mSpaceCrafts;

public MyAdapter(Context context, ArrayList<SpaceCraft> spaceCrafts) {
mContext = context;
mSpaceCrafts = spaceCrafts;
}

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);

return new MyHolder(view);
}

@Override
public void onBindViewHolder(MyHolder holder, int position) {
final String title = mSpaceCrafts.get(position).getTitle();

//BIND
holder.groupTitle.setText(title);

holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(int pos) {
//do click stuff here
}
});


}

@Override
public int getItemCount() {
return mSpaceCrafts.size();
}
}

MyHolder 类

package com.example.app.wounderlist.todolist.recycler;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.app.wounderlist.todolist.R;

public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView groupTitle;
ItemClickListener mItemClickListener;

public MyHolder(View itemView) {
super(itemView);

groupTitle = (TextView) itemView.findViewById(R.id.groupTitle);
}

public void setItemClickListener(ItemClickListener itemClickListener) {

this.mItemClickListener = itemClickListener;

}

@Override
public void onClick(View view) {

this.mItemClickListener.onItemClick(getLayoutPosition());
}
}

CRUD 类

package com.example.app.wounderlist.todolist.data;

import java.util.ArrayList;

public class CRUD {

ArrayList<SpaceCraft> mSpaceCrafts;

public CRUD(ArrayList<SpaceCraft> spaceCrafts) {
mSpaceCrafts = spaceCrafts;
}

//ADD
public boolean addNew(SpaceCraft spaceCraft) {
try {

mSpaceCrafts.add(spaceCraft);

return true;

}
catch (Exception e) {

e.printStackTrace();

}

return false;
}

//Retrieve
public ArrayList<SpaceCraft> getSpaceCrafts() {

return mSpaceCrafts;
}

//Update
public boolean update(int position, SpaceCraft newSpaceCraft) {
try {

mSpaceCrafts.remove(position);
mSpaceCrafts.add(position, newSpaceCraft);

return true;

}
catch (Exception e) {

e.printStackTrace();

}

return false;
}

//Delete
public boolean delete(int pos) {
try {

mSpaceCrafts.remove(pos);

return true;

}
catch (Exception e) {

e.printStackTrace();

}

return false;

}


}

SpaceCraft 类

package com.example.app.wounderlist.todolist.data;

public class SpaceCraft {

String mTitle;

public SpaceCraft() {}

public String getTitle() {
return mTitle;
}

public void setTitle(String title) {
mTitle = title;
}
}

SpaceCraftCollection 类

package com.example.app.wounderlist.todolist.data;

import java.util.ArrayList;

public class SpaceCraftCollection {

//create a array list using the SpaceCraft model
static ArrayList<SpaceCraft> spaceCrafts = new ArrayList<>();

public static ArrayList<SpaceCraft> getSpaceCrafts() {

return spaceCrafts;
}
}

我不知道为什么会这样。

最佳答案

这是罪魁祸首,你忘记了 Edittext 上的 getText()

SpaceCraft s = new SpaceCraft();
s.setTitle(editText.getText().toString());

关于android - 为什么实际的字符串没有显示在 recyclerView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41361100/

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