在运行应用程序

时间:2018-03-31 18:35:26

标签: java android android-activity android-recyclerview recycler-adapter

我的应用程序应该采用字符串数组并在RecyclerView中查看它。一切似乎都没问题,但当我运行应用程序时,它什么也没显示!
首先,我在String.xml文件中声明一个名为“array_countries”的String数组
此字符串文件

<resources>
<string name="app_name">StringArrayWithRecyclerView</string>

<string-array name="array_countries">
    <item>Australia</item>
    <item>Canada</item>
    <item>Cambodia</item>
    <item>China</item>
    <item>England</item>
    <item>France</item>
    <item>Japan</item>
    <item>Rusia</item>
    <item>USA</item>
</string-array>

countries_row.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dummy"
        android:textSize="30sp"
        android:textColor="@color/black"
        />

</LinearLayout>

然后我创建 CountriesAdapter

    package pro.cs_is.com.stringarraywithrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Collections;
import java.util.List;


public class CountriesAdapter extends RecyclerView.Adapter<CountriesAdapter.CountriesHolder> {

    private List<String> list = Collections.emptyList();
    private Context context;

    public CountriesAdapter(Context context, List list) {
        this.context = context;
        this.list = list;

    }

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

    }

    @Override
    public void onBindViewHolder(final CountriesHolder holder, int position) {
        String country = list.get(position);
        holder.textView.setText(country);
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(context, "You clicked " +
                        holder.textView.getText(), Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });

    }

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


    class CountriesHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public CountriesHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
        }
    }
}

最后是 MainActivity

    package pro.cs_is.com.stringarraywithrecyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private CountriesAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<String> countries = Arrays.asList(getResources().getStringArray(R.array.array_countries));
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        adapter = new CountriesAdapter(this, countries);
        adapter.notifyDataSetChanged();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        DividerItemDecoration dividerItemDecoration =
                new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
        recyclerView.addItemDecoration(dividerItemDecoration);
        recyclerView.setAdapter(adapter);


    }
}

1 个答案:

答案 0 :(得分:2)

您忘了添加

recyclerView.setLayoutManager(linearLayoutManager);