使用微调器项目时,适配器不会刷新

时间:2017-11-28 08:45:43

标签: android android-recyclerview android-spinner android-adapter

我正在使用微调器来选择和刷新项目。

例如:如果我选择全部,则应显示所有项目。如果我选择“卖入”,它应该只显示卖出物品。因此,根据微调器的选择,项目应该过滤掉并需要显示。

我试图过滤掉,但它没有过滤掉。适配器没有刷新项目。

这是代码。

MainActivity:

public class Activities extends BaseActivity {
    private RecyclerView recyclerView;
    private Spinner spinner;
    String[] countriesList = {"All", "Sell In", "Registrasi", "Re-Registrasi"};
    ActivitiesListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = inflater.inflate(R.layout.activity_activities_list, null);
        insertPoint.addView(contentView);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
         spinner  = (Spinner) findViewById(R.id.spinner);
        adapter = new ActivitiesListAdapter(getData(), this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        CustomSpinnerAdapter customAdapter=new CustomSpinnerAdapter(this,countriesList);
        spinner.setAdapter(customAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String items = adapterView.getItemAtPosition(i).toString();
                // adapter.getFilter().filter(items);
                final List<RetailerActivities> filteredModelList2 = adapter.filter2(getData(), items);
                //adapter.setFilter(filteredModelList2);

            }

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

            }
        });


    }
//the first item to be compared in Spinner. 
    public static List<RetailerActivities> getData() {
        List<RetailerActivities> list = new ArrayList<>();
        list.add(new RetailerActivities("Sell In", "Selli Cell", "13/06/2017 09:10", R.drawable.ic_tag));
        list.add(new RetailerActivities("Registrasi", "Farras Cell", "13/06/2017 09:10", R.drawable.ic_registrasi));
        list.add(new RetailerActivities("Re-Registrasi", "Farras Cell", "13/06/2017 09:10", R.drawable.ic_registrasi));
        list.add(new RetailerActivities("POSM & Visibility", "Farras Cell", "13/06/2017 09:10", R.drawable.ic_poms));
        list.add(new RetailerActivities("Sell In", "Selli Cell", "13/06/2017 09:10", R.drawable.ic_tag));
        return list;
    }

适配器类

public class ActivitiesListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>   {
    private List<RetailerActivities> mList;
    private Context context;
    private List<RetailerActivities> mFilteredList;

    public ActivitiesListAdapter(List<RetailerActivities> list, Context context) {
        this.mFilteredList = this.mList = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activities_row, parent, false);
        return new RetailerActivitiesView(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        RetailerActivities object = mList.get(position);
        //RetailerActivities retailerFilter = mFilteredList.get(position);
        ((RetailerActivitiesView) holder).title.setText(object.getTitle());
        ((RetailerActivitiesView) holder).name.setText(object.getName());
        ((RetailerActivitiesView) holder).dateTime.setText(object.getDateTime());
        ((RetailerActivitiesView) holder).profileIcon.setImageResource(object.getDrawablesIcon());

    }

    @Override
    public int getItemCount() {
        if (mList == null)
            return 0;
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mList != null) {
            RetailerActivities object = mList.get(position);
            if (object != null) {
                //return object.getType();
            }
        }
        return 0;
    }



    public static class RetailerActivitiesView extends RecyclerView.ViewHolder {
        private ImageView profileIcon;
        private TextView title;
        private TextView name;
        private TextView dateTime;

        public RetailerActivitiesView(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.recycle_txt_acc_num);
            name = (TextView) itemView.findViewById(R.id.recycle_txt_acc_name);
            dateTime = (TextView) itemView.findViewById(R.id.timestamp);
            profileIcon = (ImageView) itemView.findViewById(R.id.recycle_profile);
        }
    }
    public List<RetailerActivities> filter2(List<RetailerActivities> models, String filterType) {

        final ArrayList<RetailerActivities> filteredModelList = new ArrayList<>();
        for (RetailerActivities model:models) {
            final String cardTypeTest = model.getTitle(); //title is //where i need to compare from the spinner string..

            if (cardTypeTest.equalsIgnoreCase(filterType)) {
                filteredModelList.add(model);
            }
        }
        return filteredModelList;
    }
}

4 个答案:

答案 0 :(得分:2)

在列表中输入新项目后,为适配器添加此代码 mListAdapter.notifyDataSetChanged();

List<RetailerActivities> list = new ArrayList<>();
 list=  getData();
   recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
         spinner  = (Spinner) findViewById(R.id.spinner);
        adapter = new ActivitiesListAdapter( list, this);// set value here.....
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);

public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) 
{
    String items = adapterView.getItemAtPosition(i).toString();
     list = getFilteredData();// get data based on condition
    adapter.notifyDataSetChanged();
}

答案 1 :(得分:1)

试试代码。

您为同一个微调器实现了2 **适配器OnItemSelectedListener()。所以只有First Lister会一直工作。

<强> MainActivity:

public class Activities extends BaseActivity {
    private RecyclerView recyclerView;
    private Spinner spinner;
    String[] countriesList = {"All", "Sell In", "Registrasi", "Re-Registrasi"};
    ActivitiesListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = inflater.inflate(R.layout.activity_activities_list, null);
        insertPoint.addView(contentView);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
         spinner  = (Spinner) findViewById(R.id.spinner);
        adapter = new ActivitiesListAdapter(getData(), this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);

/* YOU HAD IMPLIMENTED 2 ADAPTERS FOR SPINNER. SO ONLY FIRST OnItemSelectedListener() WILL LISTEN ALWAYS */

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.acitivity_detail_profile_retailer, countriesList);
        dataAdapter.setDropDownViewResource(R.layout.spinner_drop_down);
        spinner.setAdapter(dataAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String items = adapterView.getItemAtPosition(i).toString();
                // adapter.getFilter().filter(items);
                final List<RetailerActivities> filteredModelList2 = adapter.filter2(getData(), items);
                adapter.notifyDataSetChanged();
                //adapter.setFilter(filteredModelList2);

            }

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

            }
        });


    }
//the first item to be compared in Spinner. 
    public static List<RetailerActivities> getData() {
        List<RetailerActivities> list = new ArrayList<>();
        list.add(new RetailerActivities("Sell In", "Selli Cell", "13/06/2017 09:10", R.drawable.ic_tag));
        list.add(new RetailerActivities("Registrasi", "Farras Cell", "13/06/2017 09:10", R.drawable.ic_registrasi));
        list.add(new RetailerActivities("Re-Registrasi", "Farras Cell", "13/06/2017 09:10", R.drawable.ic_registrasi));
        list.add(new RetailerActivities("POSM & Visibility", "Farras Cell", "13/06/2017 09:10", R.drawable.ic_poms));
        list.add(new RetailerActivities("Sell In", "Selli Cell", "13/06/2017 09:10", R.drawable.ic_tag));
        return list;
    }

答案 2 :(得分:0)

这不是您问题的确切代码,我已经发布了自己的解决方案......请查看逻辑

public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {

    View view1;
    ViewHolder viewHolder1;
    Activity act;
    List<UserTask> userTask;
    private int mSelectedPosition;
    private View mSelectedView;

    public RecyclerViewAdapter(Activity act, List<UserTask> userTask) {
        this.userTask = userTask;
        this.act = act;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        view1 = LayoutInflater.from(HomeScreen.this).inflate(R.layout.recyclerview_items, parent, false);

        viewHolder1 = new ViewHolder(view1);

        return viewHolder1;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        holder.textView.setText(userTask.get(position).getTaskName());
        holder.tvSpinnerText.setText(userTask.get(position).getChangedInstanceCount());
        holder.llSpinner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAlertDialog(userTask.get(position).getInstancesArray(), holder, position);
                // recyclerView.smoothScrollToPosition(position);

            }
        });
    }

    @Override
    public int getItemCount() {
        return userTask.size();
    }

}

点击微调器时触发的方法..

private void setAlertDialog(Integer[] values, final ViewHolder view, final int position) {

    AlertDialog.Builder builder = new AlertDialog.Builder(HomeScreen.this);

    final String[] instances = Arrays.toString(values).split("[\\[\\]]")[1].split(", ");
    builder.setItems(instances, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            view.tvSpinnerText.setText(instances[which]);
            userTask.get(position).setChangedInstanceCount(instances[which]);
            // dialog.dismiss();

            reset(position);
        }

    });
    builder.show();
}

答案 3 :(得分:0)

你需要像这样放置adapter.notifyDataSetChanged()方法:

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    String items = adapterView.getItemAtPosition(i).toString();
    // adapter.getFilter().filter(items);
    final List<RetailerActivities> filteredModelList2 = adapter.filter2(getData(), items);
    //adapter.setFilter(filteredModelList2);
    adapter.notifyDataSetChanged();//Put this line.
}