2016-06-26 72 views
0

我在這方面看過很多其他的帖子,但沒有任何幫助。問題是當我點擊addField按鈕時,對話框內的列表視圖只添加一次新視圖。但在其他點擊它並沒有得到更新,雖然適配器工作正常(我的意思是調用getView,並且適配器中的arrayList的大小也改變了)。更新arraylist後listview不更新?

我已經在適配器類中使用notifyDataSetChanged()。沒有結果!我在活動中使用了adpater類的實例,並調用myAdapter.notifyDataSetChanged()。沒有結果!

這裏是我的代碼:

public class MainActivity extends Activity { 

ListView lvfid; 
FieldAdapter fAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //some code 
    showFid(); 
} 

private void showFiD(){ 
    final ArrayList <HashMap<String,String>> al = new ArrayList<HashMap<String,String>>(); 
    final Dialog dialog = new Dialog(MainActivity.this , R.style.DialogTheme); 
    dialog.setContentView(R.layout.field_dialog); 
    dialog.setCancelable(true); 
    dialog.show(); 
    Button addField = (Button) dialog.findViewById(R.id.btnfidField); 
    lvfid = (ListView) dialog.findViewById(R.id.lvfid); 
    //Another plan i have tested, but no result: 
    //fAdapter = new FieldAdapter(dialog.getContext(),al); 
    //lvfid.setAdapter(fAdapter); 
    addField.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      HashMap<String,String> h = new HashMap<String,String>(); 
      h.put("name", ""); 
      h.put("value", ""); 
      al.add(h); 
      lvfid.setAdapter(new FieldAdapter(dialog.getContext(), al)); 
      //fAdapter.notifyDataSetChanged(); 

     } 
    }); 
} 

public class FieldAdapter extends BaseAdapter{ 

private ArrayList <HashMap <String,String>> arrayList; 
private LayoutInflater inflater; 

public FieldAdapter(Context context, ArrayList<HashMap <String,String>> arrayList) { 
    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    this.arrayList = arrayList; 
} 

@Override 
public int getCount() { 
    return arrayList.size(); 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 


@Override 
public View getView(int position, View convertView, ViewGroup arg2) { 
    View view = null; 
    if (convertView == null) 
     view = inflater.inflate(R.layout.field_inflater, null); 
    else 
     view = convertView; 
    Holder holder = new Holder(); 
    holder.edName  = (EditText) view.findViewById(R.id.edfidName); 
    holder.edValue  = (EditText) view.findViewById(R.id.edfidValue); 
    //holder.edName.setText(arrayList.get(position).get("name")); 
    //holder.edValue.setText(arrayList.get(position).get("value")); 
    return view; 
} 

private class Holder { 
    private EditText edName; 
    private EditText edValue; 
} 


} 

} 

更新: 對不起,我花了大家的時間。愚蠢的問題是,我的列表視圖在滾動視圖內,它不能!我希望這可以幫助那些有同樣問題的人!

+0

每次更新列表時都使用notifyDataSetChanged()。 –

+1

lvfid.setAdapter應該只做一次,我認爲 – user3793589

回答

0

對不起,我把每個人的時間!這個愚蠢的問題只是我的listview是在一個滾動視圖內,它不能!從scrollview中獲得列表視圖後,問題得到解決。

0

每次更新列表時都使用notifyDataSetChanged()。

+0

其實我已經做到了,但沒有結果!我更新了問題以添加notifyDataSetChanged()方法。 – sinfa

0

我有同樣的問題。當我想在onDateChanged()之後更新listView時,我遇到了這個問題。我解決了它與一個額外的ArrayList變量和一個額外的自定義適配器在你的情況FieldAdapter變量。

在執行任何操作後,使用刷新適配器和數組列表變量更新您的listView。例如,點擊按鈕後。

1.定義一個計數器。
2.檢查按鈕被按下時,增加計數器由一個
每次檢查計數器,如果計數器大於1則意味着該按鈕被點擊超過一次,以便更新列表視圖新的變量(arrayList, FeildAdapter)

我建議你定義變量作爲私有類領域,並將其命名,如refreshedArrayListrefreshedAdapter

0
  1. 選項

嘗試使用ArrayAdapter。並且不要在每次點擊按鈕時每 創建一個新的適配器。

只能從ArrayAdapter調用add(T o)函數。

  • 其他選項:
  • 添加一個附加功能,在添加字段適配器您FieldAdapter的對象添加到ArrayList和調用notifiyDataSetChanged(適配器具有ASLO一個和適配器通知他的所有觀察者)

    也不會在每次單擊按鈕時創建新的適配器。

    0
    public class MainActivity extends Activity { 
    
    ListView lvfid; 
    FieldAdapter fAdapter; 
    ArrayList <HashMap<String,String>> al ; 
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        //some code 
    
        al = new ArrayList<HashMap<String,String>>(); 
        lvfid.setAdapter(new FieldAdapter(MainActivity.this , al)); 
    
        showFid(); 
        } 
    
        private void showFiD(){ 
         final Dialog dialog = new Dialog(MainActivity.this , R.style.DialogTheme); 
         dialog.setContentView(R.layout.field_dialog); 
         dialog.setCancelable(true); 
         dialog.show(); 
         Button addField = (Button) dialog.findViewById(R.id.btnfidField); 
         lvfid = (ListView) dialog.findViewById(R.id.lvfid); 
         //Another plan i have tested, but no result: 
         //fAdapter = new FieldAdapter(dialog.getContext(),al); 
         //lvfid.setAdapter(fAdapter); 
         addField.setOnClickListener(new OnClickListener() { 
         @Override 
         public void onClick(View arg0) { 
          HashMap<String,String> h = new HashMap<String,String>(); 
          h.put("name", ""); 
          h.put("value", ""); 
          al.add(h); 
          fAdapter.notifyDataSetChanged(); 
    
         } 
        }); 
    } 
    
    +0

    謝謝,但這是測試?無法在onCreate調用lvfid.setAdapter,因爲它的lvfid位於對話框上下文中! – sinfa

    +0

    使用上面編輯的代碼。 – Drv