【问题标题】:RecyclerView inside fragmen there is no error with my codeFragmen 中的 RecyclerView 我的代码没有错误
【发布时间】:2017-12-22 11:27:59
【问题描述】:

我是android studio的初学者,

我在 Fragment 中使用 RecyclerView 时遇到一些问题。我无法在我的代码中找到错误。当我运行我的应用程序时,它会运行,但每当我打开特定片段时,它都会显示 click me for screen shot

添加我的代码如下

ListFragment.java

 public class ListFragment extends Fragment {
//this is the JSON Data URL
//make sure you are using the correct ip else it will not work
final String URL_PRODUCTS = "http://192.168.0.12/Api.php";

//a list to store all the products
List<Person> personList;

//the recyclerview
RecyclerView recyclerView;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Toast.makeText(MainActivity.appContext, "Created", Toast.LENGTH_SHORT).show();
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_list, container, false);

    Toast.makeText(MainActivity.appContext, "Created in View", Toast.LENGTH_SHORT).show();
    //getting the recyclerview from xml
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recylcerView);



    //initializing the productlist
    personList = new ArrayList<>();

    //this method will fetch and parse json
    //to display it in recyclerview
    loadProducts();
    return rootView;

}

private void loadProducts() {

    /*
    * Creating a String Request
    * The request type is GET defined by first parameter
    * The URL is defined in the second parameter
    * Then we have a Response Listener and a Error Listener
    * In response listener we will get the JSON response as a String
    * */
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_PRODUCTS,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        //converting the string to json array object
                        JSONArray array = new JSONArray(response);

                        //traversing through all the object
                        for (int i = 0; i < array.length(); i++) {

                            //getting product object from json array
                            JSONObject product = array.getJSONObject(i);

                            //adding the product to product list
                            personList.add(new Person(
                                    product.getInt("id"),
                                    product.getString("title"),
                                    product.getString("shortdesc"),
                                    product.getDouble("rating"),
                                    product.getDouble("price"),
                                    product.getString("image")
                            ));
                        }

                        //creating adapter object and setting it to recyclerview
                        PersonsAdapter adapter = new PersonsAdapter(MainActivity.appContext, personList);
                        recyclerView.setAdapter(adapter);
                        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.appContext));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //adding our stringrequest to queue

    Volley.newRequestQueue(MainActivity.appContext).add(stringRequest);


}


}

我用来打开片段的以下代码

fragmentManager.beginTransaction().replace(R.id.frame_container, new ListFragment(), "ListFragment").commit();

【问题讨论】:

  • 尝试调试。我们无法在没有服务器的情况下调试您的代码
  • 我该怎么办??@Tomin B
  • 添加 Beakpoint 和调试应用程序
  • 使用调试@TominB 找不到任何错误
  • 调试帮助你找出你困在哪里。检查您是否收到 Volley 的回复。

标签: android-volley android-fragmentactivity android-recyclerview


【解决方案1】:

修改代码

fragmentManager.beginTransaction().replace(R.id.frame_container, new MyListFragment(), "MyListFragment").commit();

第二个参数应该是 Fragments 。

第三个参数是用于识别片段的标签。

第一个参数是 Activity 中的 FragmeLayout 或容器,Fragment 将被充气。

您不能使用名称列出片段,因为它是 Android 本身的默认片段。有时当您调用 ListFragment 时,可能会调用该函数

【讨论】:

  • 是的,我使用那个代码只是为了打开那个片段。
  • 我没找到你。如果不打开 ListFragment 怎么获取 Info Fragment 中的 ListFragment
  • 我只打开 Listfragment...!
  • 嘿@Tomin B
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 2020-07-23
  • 2020-09-01
相关资源
最近更新 更多