如何对回收站查看 SQLite 内容进行排序

Posted

技术标签:

【中文标题】如何对回收站查看 SQLite 内容进行排序【英文标题】:How to sort recycler view SQLite content 【发布时间】:2021-06-09 23:32:33 【问题描述】:

我有一个视图,其中显示了一堆我的 Sqlite 数据。我最近在我的数据库中添加了一个名为 location_position 的列。我在我的相对布局中添加了一个按钮,以便在单击它时我希望里面的数据以升序格式排序。我已经尝试在网上按照一些示例进行操作,但我的应用程序设置方式让我很难让它工作。

我有一个 routeView.java 类和一个 routeAdapter

如果有人可以告诉我如何在单击按钮时对内容进行排序,或者任何具有类似解决方案的资源,我会非常感激

public class RouteView extends AppCompatActivity 

RecyclerView recyclerView;
routeAdapter routeAdapter;
ArrayList<String>  location_id, location_name, location_county, location_description, location_route, location_position;
MyDatabaseHelper myDB;
Button createPDFButton, btnNorth, southBtn;

Context mContext = this;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_route_view);

    Button southBtn = findViewById(R.id.button_south);
    
    southBtn.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            Collections.sort(ArrayList);
        
    );
   // btnRemove = findViewById(R.id.btnRemove);
    recyclerView = findViewById(R.id.recyclerView);
    myDB = new MyDatabaseHelper(this);

    createPDFButton = findViewById(R.id.createPDFButton);
    location_id = new ArrayList<>();
    location_name =  new ArrayList<>();
    location_county = new ArrayList<>();
    location_description = new ArrayList<>();
    location_route = new ArrayList<>();
    location_position = new ArrayList<>();

    Cursor cursor = myDB.readAllData();

    createPDFButton.setOnClickListener(new View.OnClickListener() 
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onClick(View v) 
           if (UserActivity.checkAppPermission(RouteView.this, "android.permission.WRITE_EXTERNAL_STORAGE", 1))
               generatePDF(recyclerView);
           

        
    );
    while (cursor.moveToNext())
        if(cursor.getString(4).equals("1")) 
            location_id.add(cursor.getString(0));
            location_name.add(cursor.getString(1));
            location_county.add(cursor.getString(2));
            location_description.add(cursor.getString(3));
            location_route.add(cursor.getString(4));
            location_position.add(cursor.getString(8));
        
    
    routeAdapter = new routeAdapter(this, this, location_id, location_name, location_county, location_description, location_route, location_position);
    recyclerView.setAdapter(routeAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


这是路由适配器类

public class routeAdapter  extends RecyclerView.Adapter<routeAdapter.MyViewHolder> 
//private final ClickListener listener;
private Context context;
MyDatabaseHelper myDB;
Activity activity;
private ArrayList location_id, location_name, location_county, location_description, location_position, location_route, location_image_url;
Button btnRemove;
String id, name, county, description;
routeAdapter(Activity activity, Context context,  ArrayList location_id, ArrayList location_name, ArrayList location_county,
             ArrayList location_description, ArrayList location_route, ArrayList location_position)
    this.activity = activity;
    this.context = context;
    this.location_id = location_id;
    this.location_name = location_name;
    this.location_county = location_county;
    this.location_description = location_description;
    this.location_position = location_position;

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.route_row, parent, false);
    return new MyViewHolder(view);

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) 
    holder.location_id_txt.setText(String.valueOf(location_id.get(position)));
    holder.location_name_txt.setText(String.valueOf(location_name.get(position)));
    holder.location_county_txt.setText(String.valueOf(location_county.get(position)));
    holder.location_description_txt.setText(String.valueOf(location_description.get(position)));



    holder.mainLayout2.setOnClickListener(new View.OnClickListener() 

        @Override

        public void onClick(View view) 
            Intent intent = new Intent(context, RouteView.class);
            intent.putExtra("id", String.valueOf(location_id.get(position)));
            intent.putExtra("name", String.valueOf(location_name.get(position)));
            intent.putExtra("county", String.valueOf(location_county.get(position)));
            intent.putExtra("description",String.valueOf(location_description.get(position)));
            activity.startActivityForResult(intent, 2);
        
    );


@Override
public int getItemCount()   return location_id.size(); 

    class MyViewHolder extends RecyclerView.ViewHolder 

        TextView location_id_txt, location_name_txt, location_county_txt, location_description_txt, added_to_route_txt, location_image_url;
        LinearLayout mainLayout2;
        Button btnRemove;

        public MyViewHolder(@NonNull View itemView) 
            super(itemView);
            location_id_txt = itemView.findViewById(R.id.location_id_txt);
            location_name_txt = itemView.findViewById(R.id.location_name_txt);
            location_county_txt = itemView.findViewById(R.id.location_county_txt); 
            location_description_txt = itemView.findViewById(R.id.location_description_txt);
            mainLayout2 = itemView.findViewById(R.id.mainLayout2);

        
    

【问题讨论】:

为每个属性保留ArrayList 是个坏主意,你最终会拥有一个 Hulk 大小的构造函数。创建一个 POJO 类并在那里设置所有属性并将其用作 POJO 的 ArrayList。另外,如果您想摆脱这种痛苦的完整光标内容,您可能想结帐android-room 【参考方案1】:

这是基本概念:

单击按钮或其他任何东西以按特定顺序对事物进行排序时,您对包含 RecyclerView 中反映的值的 ArrayList 进行排序。然后更新/重新创建 RecyclerView 以注册更改。

【讨论】:

以上是关于如何对回收站查看 SQLite 内容进行排序的主要内容,如果未能解决你的问题,请参考以下文章

linux如何查看系统占用磁盘空间最大的文件及让文件按大小排序

将回收站视图数据中的 onMove() 更改更新到房间数据库

如何使用光标和循环显示来自 sqlite 的片段的 recyclerview

Android:我需要拍照并将其存储在 sqlite 数据库中,然后将其填充到回收站视图中

从回收站视图和 sqlite 中删除一行

回收站视图仅在再次启动活动后才显示更新列表