在主要活动

时间:2018-03-03 18:36:58

标签: android android-fragments android-recyclerview recycler-adapter

我不确定我现在缺少什么。当我在视图上尝试draggin时,列表将出现,它有点显示RecyclerView存在,但它没有数据。我没有收到任何错误,但视图中没有填充远程数据。我有一个MainActivity来托管Fragment,其内容是在json的响应中动态生成的。

这是我的片段:RCurrency.java

public class RCurrency extends Fragment {
    RecyclerView mRecyclerView;
    List<CurrencyItem> mCurrencyItems;
    String currencies = "Currency";
    Context mContext;
    public static final String URL_STRING = "https://linktoapi.com/";

    public RCurrency(){
        //required empty constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView( LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        //fragment inflater
        View view = inflater.inflate(R.layout.fragment_currency_home,container, false);

        //recycler view
        mRecyclerView = (RecyclerView) view.findViewById(R.id.currency_recycler_view);

        mCurrencyItems = new ArrayList<CurrencyItem>();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_STRING,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray jsonArray = new JSONArray(response);
                            JSONObject jsonObject;
                            for (int i = 0; i <= jsonArray.length()-1; i++) {
                                jsonObject = jsonArray.getJSONObject(i);

                                CurrencyItem currencyItem = new CurrencyItem();

                                currencyItem.setCurrencyLogo(jsonObject.getString("id"));
                                currencyItem.setSymbol(jsonObject.getString("symbol"));
                                currencyItem.setCurrencyName(jsonObject.getString("name"));
                                mCurrencyItems.add(currencyItem);
                            }
                            CurrencyItemAdapter adapter = new CurrencyItemAdapter(getContext(), mCurrencyItems);

//Problem at this point
                            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
                            mRecyclerView.setLayoutManager(layoutManager);
                            mRecyclerView.setHasFixedSize(true);
                            mRecyclerView.setAdapter(adapter);
                            adapter.notifyDataSetChanged();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Check response", error);
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("name",currencies);

                return params;
            }

            //add request to the volley queue

        };
        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        requestQueue.add(stringRequest);
        return view;
    }
}

我在此处主持片段的主要活动:MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button logInBtn;
    private CurrencyListFragment RCurrency;

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

        // fragment manager to manage fragments and transactions
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_container);
        if (fragment == null){
            fragment = new RCurrency();
            fragmentManager.beginTransaction()
                    .add(R.id.fragment_container, fragment)
                    .commit();
        }

        logInBtn = (Button) findViewById(R.id.log_in_main_page);
        logInBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, info.lansachia.cryptoinvest.LoginActivity.class);
                finish();
                startActivity(intent);
            }
        });
    }
}

我的适配器:CurrencyItemAdapter.java

public class CurrencyItemAdapter extends RecyclerView.Adapter<CurrencyItemAdapter.MyViewHolder> {
    private List<CurrencyItem> mCurrencyItems;
    private Context mContext;

    public CurrencyItemAdapter(Context context, List<CurrencyItem> currencyItems){
        this.mContext = context;
        this.mCurrencyItems = currencyItems;
    }

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

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        CurrencyItem currencyItem = mCurrencyItems.get(position);

        //setting text to currency symbol
        holder.mSymbol.setText(currencyItem.getSymbol());

        holder.mSymbol.setTextSize(17);

        //currency name
        holder.mName.setText(currencyItem.getCurrencyName());
        holder.mName.setTextSize(17);
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView mSymbol;
        TextView mPrice;
        TextView mName;
        TextView mRecommendation;
        ImageView mCurrencyLogo;

        public MyViewHolder(View itemView) {
            super(itemView);

            mSymbol = (TextView)itemView.findViewById(R.id.currency_symbol);
            mPrice = (TextView)itemView.findViewById(R.id.currency_price);
            mName = (TextView)itemView.findViewById(R.id.currency_name);
            mRecommendation = (TextView)itemView.findViewById(R.id.currency_recommendation);
//            mCurrencyLogo= (ImageView)itemView.findViewById(R.id.currency_icon);
        }
    }
}

list_item_currency.xml布局是..

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/currency_icon"
        android:layout_width="75dp"
        android:layout_height="74dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:contentDescription="@string/currecy_logo"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/currency_symbol"
        android:layout_width="177dp"
        android:layout_height="75dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toEndOf="@+id/currency_icon"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/currency_name"
        android:layout_width="176dp"
        android:layout_height="29dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="91dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/currency_symbol"
        app:layout_constraintVertical_bias="0.0"/>

    <TextView
        android:id="@+id/currency_recommendation"
        android:layout_width="101dp"
        android:layout_height="25dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/currency_name"
        app:layout_constraintTop_toBottomOf="@+id/currency_price"/>

    <TextView
        android:id="@+id/currency_price"
        android:layout_width="101dp"
        android:layout_height="74dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/currency_symbol"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

fragment_currency.xml是包含RecyclerView的布局,但最终RecyclerView托管在main_activity.xml

的布局中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/fragment_home_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/currency_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"/>
</LinearLayout>

此处的堆栈跟踪:

3-03 10:07:16.102 5337-5337/? I/zygote: Not late-enabling -Xcheck:jni (already on)
03-03 10:07:16.176 5337-5337/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
03-03 10:07:16.387 5337-5337/info.myapp.currency W/ActivityThread: Application info.myapp.currency is waiting for the debugger on port 8100...
03-03 10:07:16.400 5337-5337/info.myapp.currency I/System.out: Sending WAIT chunk
03-03 10:07:17.018 5337-5346/info.myapp.currency I/zygote: Debugger is active
03-03 10:07:17.032 5337-5337/info.myapp.currency I/System.out: Debugger has connected
03-03 10:07:17.032 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:17.233 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:17.435 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:17.636 5337-5337/info.myapp.currency I/chatty: uid=10085(u0_a85) info.myapp.currency identical 1 line
03-03 10:07:17.838 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:18.039 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:18.241 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:18.443 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:18.645 5337-5337/info.myapp.currency I/System.out: debugger has settled (1460)
03-03 10:07:19.002 5337-5337/info.myapp.currency W/zygote: Skipping duplicate class check due to unrecognized classloader
03-03 10:07:19.008 5337-5337/info.myapp.currency W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
03-03 10:07:19.013 5337-5337/info.myapp.currency W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
03-03 10:07:19.058 5337-5337/info.myapp.currency I/BiChannelGoogleApi: [FirebaseAuth: ] No Fallback module; NOT setting up for lazy initialization
03-03 10:07:19.095 5337-5337/info.myapp.currency D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
03-03 10:07:19.125 5337-5359/info.myapp.currency W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
03-03 10:07:19.156 5337-5359/info.myapp.currency I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
03-03 10:07:19.156 5337-5359/info.myapp.currency I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to gms implementation
03-03 10:07:19.249 5337-5337/info.myapp.currency I/FirebaseInitProvider: FirebaseApp initialization successful
03-03 10:07:19.252 5337-5337/info.myapp.currency I/InstantRun: starting instant run server: is main process
03-03 10:07:19.293 5337-5362/info.myapp.currency I/FA: App measurement is starting up, version: 11910
03-03 10:07:19.293 5337-5362/info.myapp.currency I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
03-03 10:07:19.293 5337-5362/info.myapp.currency I/FA: To enable faster debug mode event logging run:
                                                                 adb shell setprop debug.firebase.analytics.app info.myapp.currency
03-03 10:07:19.293 5337-5362/info.myapp.currency D/FA: Debug-level message logging enabled
03-03 10:07:19.611 5337-5337/info.myapp.currency D/Currency List: Fragment added
03-03 10:07:19.630 5337-5337/info.myapp.currency D/RCurrency: onCreate Called
03-03 10:07:19.715 5337-5366/info.myapp.currency D/NetworkSecurityConfig: No Network Security Config specified, using platform default
03-03 10:07:19.777 5337-5337/info.myapp.currency D/RCurrency: onCreateView Called
03-03 10:07:19.805 5337-5373/info.myapp.currency D/OpenGLRenderer: HWUI GL Pipeline
03-03 10:07:19.806 5337-5362/info.myapp.currency D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-8822734687014467414}]
03-03 10:07:19.884 5337-5373/info.myapp.currency I/OpenGLRenderer: Initialized EGL, version 1.4
03-03 10:07:19.884 5337-5373/info.myapp.currency D/OpenGLRenderer: Swap behavior 1
03-03 10:07:19.884 5337-5373/info.myapp.currency W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
03-03 10:07:19.884 5337-5373/info.myapp.currency D/OpenGLRenderer: Swap behavior 0
03-03 10:07:19.913 5337-5373/info.myapp.currency D/EGL_emulation: eglCreateContext: 0x8edb3ec0: maj 2 min 0 rcv 2
03-03 10:07:19.973 5337-5373/info.myapp.currency D/EGL_emulation: eglMakeCurrent: 0x8edb3ec0: ver 2 0 (tinfo 0x9e4d6200)
03-03 10:07:20.128 5337-5373/info.myapp.currency D/EGL_emulation: eglMakeCurrent: 0x8edb3ec0: ver 2 0 (tinfo 0x9e4d6200)
03-03 10:07:20.254 5337-5362/info.myapp.currency D/FA: Connected to remote service
03-03 10:07:20.385 5337-5337/info.myapp.currency D/RCurrency: parseData Called
03-03 10:07:20.385 5337-5337/info.myapp.currency D/RCurrency: requestData Called
03-03 10:07:20.437 5337-5344/info.myapp.currency I/zygote: Do partial code cache collection, code=30KB, data=21KB
03-03 10:07:20.440 5337-5344/info.myapp.currency I/zygote: After code cache collection, code=30KB, data=21KB
03-03 10:07:20.440 5337-5344/info.myapp.currency I/zygote: Increasing code cache capacity to 128KB
03-03 10:07:46.609 5337-5346/? W/zygote: Debugger told VM to exit with status -1

                                         [ 03-03 10:07:46.610  5337: 5346 D/         ]
                                         HostConnection::get() New Host Connection established 0xa09e4b00, tid 5346

0 个答案:

没有答案