我知道这个问题对于大多数人来说可能是愚蠢的,但是我仍然需要帮助。
我正在尝试使用具有Singleton实例的fragents填充和ArrayList(或者至少是我所理解的我要做的事情)
我有一个填写表格,用户必须在其中填写以下信息:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_formulari, container, false);
titolET = view.findViewById(R.id.titolAnunciET);
cuantaGenteET = view.findViewById(R.id.quantitatGentET);
horaET = view.findViewById(R.id.horaET);
queBirraRB = view.findViewById(R.id.birraRB);
queCafeRB = view.findViewById(R.id.cafeRB);
comidaSW = view.findViewById(R.id.ComerSW);
postBT = view.findViewById(R.id.introBT);
postBT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
postInformation();
}
});
return inflater.inflate(R.layout.fragment_formulari, container, false);
}
@Override
public void onResume() {
super.onResume();
setInformation();
}
public void setInformation() {
tituloString = titolET.getText().toString();
cuantaGenteSrting = cuantaGenteET.getText().toString();
cafe = queCafeRB.isChecked();
birra = queBirraRB.isChecked();
comida = comidaSW.isChecked();
horaString = horaET.getText().toString();
if (cafe) {
cafeSiNo = "Beben cafe o refrescos";
} else {
cafeSiNo = "No beben cafe o refrescos";
}
if (birra) {
birraSiNo = "Beben birra";
} else {
birraSiNo = "No beben birra";
}
if (comida) {
comidaSiNo = "Estan comiendo";
} else {
comidaSiNo = "No estan comiendo";
}
System.out.print(tituloString);
respostesFormulari = new RespostesFormulari(tituloString);
respostesFormulari.setTitol(tituloString);
respostesFormulari.setQuantaGent(cuantaGenteSrting);
respostesFormulari.setHora(horaString);
respostesFormulari.setQueBirra(birraSiNo);
respostesFormulari.setQueCafe(cafeSiNo);
respostesFormulari.setMenjarBool(comidaSiNo);
formulariSingleton = FormulariSingleton.getInstance();
formulariSingleton.add(respostesFormulari);
}
我也有此类RespostesFormulari.java:
public class RespostesFormulari {
private String titol;
private String quantaGent;
private String queBirra;
private String queCafe;
private String menjarBool;
private String hora;
public RespostesFormulari(String titol){this.titol = titol;}
public RespostesFormulari(String titol, String finalquantaGent, String finalqueBirra, String finalqueCafe, String finalmenjarBool, String finalHora) {
this.titol = titol;
this.quantaGent = finalquantaGent;
this.queBirra = finalqueBirra;
this.queCafe = finalqueCafe;
this.menjarBool = finalmenjarBool;
this.hora = finalHora;
}
public String getTitol(){
return this.titol;
}
public void setTitol(String titol){
this.titol = titol;
}
public String getQuantaGent() {
return this.quantaGent;
}
public void setQuantaGent(String quantaGent) {
this.quantaGent = quantaGent;
}
public String getQueBirra() {
return this.queBirra;
}
public void setQueBirra(String queBirra) {
this.queBirra = queBirra;
}
public String getQueCafe() {
return this.queCafe;
}
public void setQueCafe(String queCafe) {
this.queCafe = queCafe;
}
public String getMenjarBool() {
return this.menjarBool;
}
public void setMenjarBool(String menjarBool) {
this.menjarBool = menjarBool;
}
public String getHora() {
return this.hora;
}
public void setHora(String hora) {
this.hora = hora;
}
}
这是我的Singleton,我在这里管理我的RespostesFormulari.java:
FormulariSingleton类{
private RespostesFormulari respostesFormulari;
private static final FormulariSingleton formulariSingletonInstance = new FormulariSingleton();
public ArrayList<RespostesFormulari> respostesFormularisLlista;
public String titolAnunci;
public static FormulariSingleton getInstance() {
return formulariSingletonInstance;
}
private FormulariSingleton(){
respostesFormularisLlista = new ArrayList<RespostesFormulari>();
respostesFormulari = new RespostesFormulari("","","","","","");
//respostesFormularisLlista.add(new RespostesFormulari("","",false,false,false,""));
}
public ArrayList<RespostesFormulari> getLlista(){
return this.respostesFormularisLlista; }
public void setRespostesFormulari(RespostesFormulari rf){
this.respostesFormulari = rf; }
public void add(RespostesFormulari newRespostes){
this.respostesFormularisLlista.add(newRespostes);}
public void delete(RespostesFormulari newRespostes){
this.respostesFormularisLlista.remove(newRespostes);}
public void getTitolAnunci(RespostesFormulari titol) {
titolAnunci = titol.getTitol();}
public void setClickedAnunci(RespostesFormulari anunci){
respostesFormulari = anunci;
}
public RespostesFormulari getClickedAnunci(){
return respostesFormulari;
}
最后,这是我的ListView类:
public class LlistaFragment extends Fragment {
public ListView listview;
ArrayAdapter<RespostesFormulari> adapter;
// ArrayList<> llista
private OnFragmentInteractionListener fListener;
public LlistaFragment() {
}
public static LlistaFragment newInstance(String param1, String param2) {
LlistaFragment fragment = new LlistaFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getArguments() != null) {
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
// return super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_llistat, container, false);
listview = rootView.findViewById(R.id.llistView);
// ArrayList<RespostesFormulari> llista = FormulariSingleton.getInstance().getLlista();
adapter = new ArrayAdapter<RespostesFormulari>(getActivity(), android.R.layout.simple_list_item_1, FormulariSingleton.getInstance().getLlista());
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FormulariSingleton formulariSingleton = FormulariSingleton.getInstance();
RespostesFormulari item = (RespostesFormulari) parent.getItemAtPosition(position);
formulariSingleton.getTitolAnunci(item);
formulariSingleton.setClickedAnunci(item);
Intent intent = new Intent(getActivity(), DetallAnunci.class);
intent.putExtra("Titol", position);
/*intent.putExtra("Titol", item.getTitol());
intent.putExtra("Numero gente", item.getQuantaGent());
intent.putExtra("Beben birra?",item.getQueBirra());
intent.putExtra("Beben cafe o refresco?", item.getQueCafe());
intent.putExtra("Estan comiendo?", item.getMenjarBool());
intent.putExtra("A que hora se van?", item.getHora());*/
startActivity(intent);
}
});
return rootView;
}
当我进入每个项目时,没有显示任何信息,并且与listView标题相同。
有什么主意吗?
非常感谢
答案 0 :(得分:0)
最后,我设法为自己提供了解决方案。也许有人遇到同样的问题。
因此,首先,在我的应用程序表单片段中,我创建了一个按钮来发布信息并在Singleton中设置所有内容。该按钮不起作用,因为我正在用充气机返回视图。我只需要返回视图,所以我替换了这个:
postBT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
postInformation();
}
});
return inflater.inflate(R.layout.fragment_formulari, container, false);
与此:`postBT.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
setInformation();
postInformation();
}
});
return view;
` 我还添加了setInformation();在按钮onClick上。以前,我是在onResum()方法中执行此操作的,该方法在填写表单之前已设置了所有信息。
最后,我意识到我在我的listview.java中而不是我自己的适配器中调用了默认适配器(ArrayAdapter),所以我替换了它:
adapter = new ArrayAdapter<RespostesFormulari>(getActivity(), android.R.layout.simple_list_item_1, FormulariSingleton.getInstance().getLlista());
listview.setAdapter(adapter);
与此:
adapter = new Adapter(getActivity(), FormulariSingleton.getInstance().getLlista());
listview.setAdapter(adapter);
这是我的适配器的代码:
public class Adapter extends BaseAdapter {
private ArrayList<RespostesFormulari> respostesFormularis;
private Context mcontext;
//CONSTRUCTOR
public Adapter(Context mcontext, ArrayList<RespostesFormulari> respostesFormularis){
this.mcontext=mcontext;
this.respostesFormularis=respostesFormularis;
}
@Override
public int getCount(){return respostesFormularis.size();}
@Override
public Object getItem(int position){return respostesFormularis.get(position);}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
convertView=View.inflate(mcontext, R.layout.cella_llista,null);
TextView titol = convertView.findViewById(R.id.titolTV);
titol.setText(FormulariSingleton.getInstance().getLlista().get(position).getTitol());
return convertView;
}
希望有人可以从中获利!