次のコードでは、2 つの ListView を表示するフラグメントを設定しています。
メイン アクティビティが Fragment メソッドを呼び出す場合:
prendiListaBest
と
prendiListaWorst
アダプタで notifyDataSetChanged() を呼び出します。
したがって、リストは ArrayLists の新しい内容で更新されます。
しかし、これは起こりません。
歓迎以上の助けと提案をお願いします
public class WorstBest extends Fragment {
public ListView lvBest;
public ListView lvWorst;
public MyAdapter myAdap;
public MyAdapter myAdap2;
public ArrayList<Stock> best;
public ArrayList<Stock> worst;
public View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v= inflater.inflate(R.layout.listaworstbest, container, false);
lvBest= (ListView)v.findViewById(R.id.listbest);
lvWorst=(ListView)v.findViewById(R.id.listworst);;
myAdap= new MyAdapter(best, getActivity());
myAdap2= new MyAdapter(worst, getActivity());
lvBest.setAdapter(myAdap);
lvWorst.setAdapter(myAdap2);
return v;
}
public void prendiListaBest(ArrayList<Stock> al){
Log.e("", "passo per prendilitsa del fragment worstbest");
best=al;
Log.e("", "nel fragment best è" + Integer.toString(best.size()));
myAdap.notifyDataSetChanged();
}
public void prendiListaWorst(ArrayList<Stock> al){
Log.e("", "passo per prendilitsa del fragment worstbest");
worst=al;
Log.e("", "nel fragment worst è" + Integer.toString(worst.size()));
myAdap2.notifyDataSetChanged();
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
}
これは私のカスタム アダプターのコードです。リクエスト通りに投稿しましたが、本当にぐちゃぐちゃで申し訳ありません。
public class MyAdapter extends BaseAdapter {
ArrayList<Stock> l;
Activity a;
public MyAdapter(ArrayList<Stock> li, Activity ac) {
// TODO Auto-generated constructor stub
l = li;
a=ac;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
if (row==null) {
LayoutInflater inflater=a.getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
TextView tw=(TextView)row.findViewById(R.id.textView1);
String st=l.get(position).simbolo;
String st1=st;
String st2=st;
String st3=st;
String st4=st;
String st5=st;
String st6=st;
String st7=st;
String st8=st;
st8=st;
if(st.length()<=7){st1=st.concat(" ");st2=st1;st3=st1;st4=st1;st5=st1;st6=st1;st7=st1;st8=st1;}
if(st1.length()<=7){st2=st1.concat(" ");st3=st2;st4=st2;st5=st2;st6=st2;st7=st2;st8=st2;}
if(st2.length()<=7){st3=st2.concat(" ");st4=st3;st5=st3;st6=st3;st7=st3;st8=st3;}
if(st3.length()<=7){st4=st3.concat(" ");st5=st4;st6=st4;st7=st4;st8=st4;}
if(st4.length()<=7){st5=st4.concat(" ");st6=st5;st7=st5;st8=st5;}
if(st5.length()<=7){st6=st5.concat(" ");st7=st6;st8=st6;}
if(st6.length()<=7){st7=st6.concat(" ");st8=st7;}
if(st7.length()<=7){st8=st7.concat(" ");}
tw.setText(st8);
String allora=(String) tw.getText();
TextView tw2=(TextView)row.findViewById(R.id.nome);
///aggiungiamo numero minimo caratteri
tw2.setText((l.get(position).nome));
///
TextView tw3=(TextView)row.findViewById(R.id.prezzo);
tw3.setText((l.get(position).prezzo));
TextView tw4=(TextView)row.findViewById(R.id.cambiamento);
tw4.setText((l.get(position).cambiamento).concat(" %"));
try {
double value = Double.parseDouble(l.get(position).cambiamento);
if(value<0)tw4.setTextColor(Color.RED);
if(value>0)tw4.setTextColor(Color.GREEN);
TextView tw5=(TextView)row.findViewById(R.id.freccia);
if(value<0)tw5.setText("▼");
if(value>0)tw5.setText("▲");
if(value<0)tw5.setTextColor(Color.RED);
if(value>0)tw5.setTextColor(Color.GREEN);
} catch (Exception exc) {}
return row;
}
public int getCount() {
// TODO Auto-generated method stub
return l .size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return l .get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 編集*編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 編集 *
どうもありがとうございました。答えは完全に理にかなっています。**
しかし、同じアダプターが次のフラグメントで正常に機能する理由がわかりません。説明します: 次の「listfragment」フラグメントには同様のロジックがあり、
prendiLista
の最新バージョンを取得するメソッド
ListArray lt
前の "WorstBest" フラグメントで行われたのとまったく同じ方法です。しかし、この場合、
public void prendiLista(ArrayList<Stock> al){
lt=al;
myAdap.notifyDataSetChanged();
}
ビューが更新されました!!!
違いは何ですか?:-)
私の理解では、notifyDataSetChanged() への呼び出しはビューを更新するのに十分でした!!
とにかくどうもありがとう!(明日の朝に回答を受け付けます)。
public class listafragment extends Fragment {
public ListView lv;
public MyAdapter myAdap;
public ArrayList<Stock> lt;
public View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.lista, container, false);
lv= (ListView) v.findViewById(android.R.id.list);
if(lv==null){Log.e("","nel fragment la lista è nulla");}
if(lv!=null){Log.e("","nel fragment la lista NON è nulla");}
myAdap= new MyAdapter(lt, getActivity());
lv.setAdapter(myAdap);
return v;
}
public void prendiLista(ArrayList<Stock> al){
Log.e("", "passo per prendilitsa del fragment");
lt=al;
if(myAdap==null){Log.e("","nel prendilista del fragment myadapt è nullo");};
myAdap.notifyDataSetChanged();
}
public void addToFavorites(int pos){
Quotes.addToFavorites(pos);
}
@Override
public void onCreate(Bundle savedInstanceState){
lt=Quotes.dammi();
super.onCreate(savedInstanceState);
}
}