如何刷新android中微调器选择的listview

时间:2017-02-23 08:55:23

标签: android listview android-arrayadapter

我正在尝试刷新我的关于微调项目选择的列表视图。我知道有很多解决方案,但我无法解决它。我已经尝试过notifyDataSetChanged(),但它无法正常工作。 我是android的新手。请帮助我,我坚持下去。

这是我的Adapter类:

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;
}
}

在这堂课中,我正在使用Spinner:

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;
}

}

1 个答案:

答案 0 :(得分:0)

首先,我想说new AdaptersetAdaptergetMonthItem()即使在do...while也不应该onCreate()更好。然后,当您选择项目时,您确定数据列表已更改吗?再次检查,希望它有所帮助。