2017-02-23 115 views
0

我想刷新我的列表視圖上的微調項目選擇。我知道有很多解決方案,但我無法解決它。我試過notifyDataSetChanged(),但它不工作。 我是Android新手。請幫助我,我堅持下去。如何刷新Android上的微調選擇列表視圖

這裏是我的適配器類:

public class ListDataAdaptar extends ArrayAdapter { 

List<DataProvider> mlist; 

public ListDataAdaptar(Context context, int resource,List<DataProvider> list) { 
    super(context, resource); 
    mlist=list; 
} 
static class LayoutHandler{ 
    TextView amount,date,title; 
    TextView total; 
} 

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

@Nullable 
@Override 
public Object getItem(int position) { 
    return mlist.get(position); 
} 

@NonNull 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View mView= convertView; 
    LayoutHandler layoutHandler; 
    if (mView==null) 
    { 
     LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     mView=layoutInflater.inflate(R.layout.display_income_row,parent,false); 
     layoutHandler=new LayoutHandler(); 
     layoutHandler.title= (TextView) mView.findViewById(R.id.income); 
     layoutHandler.amount=(TextView)mView.findViewById(R.id.income_amount); 
     layoutHandler.date= (TextView) mView.findViewById(R.id.date); 
     mView.setTag(layoutHandler); 
    } 
    else { 
     layoutHandler = (LayoutHandler) mView.getTag(); 
    } 
    DataProvider dataProvider = (DataProvider)this.getItem(position); 
    Double s= new Double(dataProvider.getMoney()); 
    layoutHandler.amount.setText(""+s); 
    layoutHandler.date.setText(dataProvider.getDate()); 
    layoutHandler.title.setText(dataProvider.getName()); 

    return mView; 
} 
} 

而在這個類我使用微調:

public class IncomeReport extends AppCompatActivity { 
ListView list; 
Toolbar toolbar; 
private DatabaseHandler handler; 
private List<DataProvider> amountList; 
private ListDataAdaptar listDataAdapter; 
Spinner spinner; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.income_report); 
    toolbar = (Toolbar) findViewById(R.id.app_bar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    list = (ListView)findViewById(R.id.listView); 
    amountList = new ArrayList<>(); 
    handler = new DatabaseHandler(this); 

} 

public List getMonthItem() { 
    Cursor cursor = handler.displayIncomeMonthReport(); 
    if (cursor.moveToFirst()) { 
     do { 
      double amount; 
      String payer; 
      String note; 
      String date; 
      amount = Double.parseDouble(cursor.getString(cursor.getColumnIndex(handler.AMOUNT))); 
      payer = cursor.getString(cursor.getColumnIndex(handler.PAYER_NAME)); 
      note = cursor.getString(cursor.getColumnIndex(handler.NOTE)); 
      date=cursor.getString(cursor.getColumnIndex(handler.DATE)); 
      DataProvider provider = new DataProvider(amount, payer,note,date); 
      amountList.add(provider); 
      listDataAdapter = new ListDataAdaptar(this, R.layout.display_income_row, amountList); 
      list.setAdapter(listDataAdapter); 
      listDataAdapter.notifyDataSetChanged(); 

     } while (cursor.moveToNext()); 
     cursor.close(); 
     handler.close(); 
    } 
    return null; 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.spinner_menu, menu); 
    MenuItem item = menu.findItem(R.id.spinner); 
    spinner = (Spinner) MenuItemCompat.getActionView(item); 

    final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
      R.array.report, android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter); 
    final int number = getIntent().getExtras().getInt("pos1"); 
    spinner.setSelection(number); 

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) { 
      switch (pos){ 
       case (0): 
        getMonthItem(); 
        listDataAdapter.notifyDataSetChanged(); 
        break; 
       case (1): 
        getYearItem(); 
        listDataAdapter.notifyDataSetChanged(); 
        break; 
       default: 
      } 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> adapterView) { 

     } 
    }); 
    return true; 
} 

}

回答

0

首先,我想說的是,你不應該new AdaptersetAdapter in getMonthItem()即使在do...while,做到onCreate()更好。然後,當您選擇項目時,您確定您的數據列表已更改?再次檢查,希望它有幫助。

+0

是的,當我選擇項目我的數據列表正在改變,但我的新數據顯示舊數據下方。舊數據不會刪除 – Rahid

+0

用於列表頂部的新數據顯示,然後使用此amountList.add(0,provider); – user2025187

+0

您不清除舊數據並將新數據再次添加到列表中。因此它會明確顯示所有數據。 – xiaoyuan