【问题标题】:Recycler View on Spinner ItemSpinner 项目的回收站视图
【发布时间】:2022-06-14 16:42:08
【问题描述】:

项目目标如下。 Spinner 显示 4 个项目... Men Women Kids Cars 并选择 Men 然后 Recycler View 出现并显示男性姓名,然后您单击 Spinner 中的另一个项目,如 Cars,然后单击相同的 Recycler View 以显示汽车名称。因此,每个微调器项目都会调用 Recycler 视图,该视图将根据您在微调器中选择的项目显示信息。

//Spinner code
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

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

        Spinner spinner = findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);


    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String text = adapterView.getItemAtPosition(i).toString();
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
        if (i==1){
            Toast.makeText(this, "stathis tocks", Toast.LENGTH_SHORT).show();
        }
    }

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

    }
}

//Recycler View sample code
public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;//Bridge between data and image/ arrayList and RecyclerView
    private RecyclerView.LayoutManager mLayoutManager;//allign single items in our list

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

        ArrayList<ExampleItem> exampleList = new ArrayList<>();
        exampleList.add(new ExampleItem("Line 1", "Line 2"));
        exampleList.add(new ExampleItem("Line 3", "Line 4"));
        exampleList.add(new ExampleItem( "Line 5", "Line 6"));
        exampleList.add(new ExampleItem("Line 7", "Line 8"));
        exampleList.add(new ExampleItem( "Line 9", "Line 10"));
        exampleList.add(new ExampleItem("Line 11", "Line 12"));
        exampleList.add(new ExampleItem("Line 13", "Line 14"));
        exampleList.add(new ExampleItem("Line 15", "Line 16"));
        exampleList.add(new ExampleItem("Line 17", "Line 18"));
        exampleList.add(new ExampleItem("Line 19", "Line 20"));
        exampleList.add(new ExampleItem("Line 21", "Line 22"));
        exampleList.add(new ExampleItem("Line 23", "Line 24"));
        exampleList.add(new ExampleItem("Line 25", "Line 26"));
        exampleList.add(new ExampleItem("Line 27", "Line 28"));
        exampleList.add(new ExampleItem("Line 29", "Line 30"));
        exampleList.add(new ExampleItem("Line 1", "Line 2"));
        exampleList.add(new ExampleItem("Line 3", "Line 4"));
        exampleList.add(new ExampleItem( "Line 5", "Line 6"));
        exampleList.add(new ExampleItem("Line 7", "Line 8"));
        exampleList.add(new ExampleItem( "Line 9", "Line 10"));
        exampleList.add(new ExampleItem("Line 11", "Line 12"));
        exampleList.add(new ExampleItem("Line 13", "Line 14"));
        exampleList.add(new ExampleItem("Line 15", "Line 16"));
        exampleList.add(new ExampleItem("Line 17", "Line 18"));
        exampleList.add(new ExampleItem("Line 19", "Line 20"));
        exampleList.add(new ExampleItem("Line 21", "Line 22"));
        exampleList.add(new ExampleItem("Line 23", "Line 24"));
        exampleList.add(new ExampleItem("Line 25", "Line 26"));
        exampleList.add(new ExampleItem("Line 27", "Line 28"));
        exampleList.add(new ExampleItem("Line 29", "Line 30"));
        exampleList.add(new ExampleItem("Line 1", "Line 2"));
        exampleList.add(new ExampleItem("Line 3", "Line 4"));
        exampleList.add(new ExampleItem( "Line 5", "Line 6"));
        exampleList.add(new ExampleItem("Line 70", "Line 8"));
        exampleList.add(new ExampleItem( "Line 9", "Line 10"));
        exampleList.add(new ExampleItem("Line 11", "Line 12"));
        exampleList.add(new ExampleItem("Line 13", "Line 14"));
        exampleList.add(new ExampleItem("Line 105", "Line 16"));
        exampleList.add(new ExampleItem("Line 17", "Line 18"));
        exampleList.add(new ExampleItem("Line 109", "Line 20"));
        exampleList.add(new ExampleItem("Line 21", "Line 22"));
        exampleList.add(new ExampleItem("Line 23", "Line 24"));
        exampleList.add(new ExampleItem("Line 2445", "Line 26"));
        exampleList.add(new ExampleItem("Line 27", "Line 280"));
        exampleList.add(new ExampleItem("Line 29", "Line 300"));

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ExampleAdapter(exampleList);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }
}

//Current code i want merge the ones on top

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;//Bridge between data and image/ arrayList and RecyclerView
    private RecyclerView.LayoutManager mLayoutManager;//allign single items in our list



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

        //Spinner
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        //RecycleView
        ArrayList<ExampleItem> exampleList = new ArrayList<>();
        for(int i=0;i<50;i++){
            String tmp="Text: "+i;
            String tmp2="Text: "+(i+1);
            exampleList.add(new ExampleItem(tmp, tmp2));
        }

        //Recycle View
        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ExampleAdapter(exampleList);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String text = adapterView.getItemAtPosition(i).toString();
        Toast.makeText(adapterView.getContext(), text, Toast.LENGTH_SHORT);
        Log.d("TAG", String.valueOf(i));

        if(i==0){
            //RecycleView
            ArrayList<ExampleItem> exampleList = new ArrayList<>();
            for(int j=0;j<50;i++){
                String tmp="Text: "+j;
                String tmp2="Text: "+(j+1);
                exampleList.add(new ExampleItem(tmp, tmp2));
            }

            //mRecyclerView = findViewById(R.id.recyclerView);
            //mRecyclerView.setHasFixedSize(true);
           // mLayoutManager = new LinearLayoutManager(this);
            mAdapter = new ExampleAdapter(exampleList);
            //mRecyclerView.setLayoutManager(mLayoutManager);
           // mRecyclerView.setAdapter(mAdapter);
        }
        if(i==1){

        }
        if(i==2){

        }
    }

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

    }
}

【问题讨论】:

  • 你的问题是什么?能详细点吗?
  • 我的意思是我得到了 spinner 项目的代码和 recycler view 项目的代码,我想合并它。所以我想要一个显示一些项目的微调器吗?然后您单击其中一项,根据您单击的项目,您将执行 Get 请求,从服务器获取 sama json 数据并在回收视图中显示它们。
  • 你能告诉我你的服务器数据库表,你存储了你的汽车、男人等的名字吗?

标签: java android android-recyclerview android-spinner


【解决方案1】:

我不知道您是如何保存和检索数据的,但我在我的项目中制作了这种类型的程序,因此我可以为您提供示例,您可以根据该示例创建自己的。 好的 !我一直在创建一个电子商务应用程序,其中我需要根据在第一个微调器中选择的类别名称在第二个微调器中显示子类别名称。 所以我所做的是 我创建了一个包含所有类别名称的表 第二个表包含所有子类别名称列以及它们在第二列中对应的类别名称。

现在,我将第一个微调器中选择的类别作为地图传递,并获取类别名称为对应所选类别的所有子类别。

这称为依赖微调器。

代码如下

基于第一个 spinner 从 MySQL 获取数据的 Java 方法

 public void getSubCategory(){
    final String genderText = gender ;
    final String categoryText = category ;
    ArrayList<String> subCateg = new ArrayList<>();

    StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

    if(response.trim().equalsIgnoreCase("No subCategory added")) {
    subCateg.add("No sub category");
    myAdapter3 = new ArrayAdapter<String>(addProductPage.this, R.layout.droptextlayout, subCateg);
    myAdapter3.notifyDataSetChanged();
    subCategoryDrop.setAdapter(myAdapter3);
    }else {
        try {
            JSONObject Jobject = new JSONObject(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1));
            String success = Jobject.getString("success");
            JSONArray Jarray = Jobject.getJSONArray("datas");

            if (success.equals("1")) {
                for (int i = 0; i < Jarray.length(); i++) {
                    JSONObject object = Jarray.getJSONObject(i);
                    String name = object.getString("name");

                    subCateg.add(name);
                }

                myAdapter3 = new ArrayAdapter<String>(addProductPage.this, R.layout.droptextlayout, subCateg);
                myAdapter3.notifyDataSetChanged();
                subCategoryDrop.setAdapter(myAdapter3);
            }

        } catch (JSONException jsonException) {
            jsonException.printStackTrace();
        }
    }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }){

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> map = new HashMap<String, String>();
            System.out.println(categoryText);
            map.put("category", categoryText);
            map.put("gender", genderText);

            return map;
        }
    };

    RequestQueue queue = Volley.newRequestQueue(addProductPage.this);
    queue.add(request);
};

我正在根据两个属性性别和类别获取所有子类别。

PHP 代码在这里...

<?php
include "config.php";

 $gender = $_POST['gender'];
$category = $_POST['category'];


   $sql="SELECT * FROM `$db`.`subCategory` WHERE `category`= '{$category}' 
   AND `gender`='{$gender}'";
   $result=mysqli_query($conn,$sql);
   $categories['datas'] = array();
   if(mysqli_num_rows($result)>0){
       while($row=mysqli_fetch_array($result)){
           $data['id']=$row['0'];
        $data['name']= $row['1'];
        $data['gender']=$row['2'];
        $data['image']=$row['3'];

        array_push($categories['datas'],$data);
       }

       $categories['success']='1';
       echo json_encode($categories);
        mysqli_close($conn);
    }else{ 
        echo "No subCategory added";

    }

?>

永远记住,如果您从服务器获取数据到微调器中,请始终将适配器和设置适配器放在从服务器获取数据的方法中,否则微调器将不会在布局中显示所选项目。

如果有任何困惑或疑问,请询问我。

【讨论】: