RecyclerView不显示 - Android

问题描述 投票:1回答:4

我在Android中的RecyclerView遇到了一些问题。当我运行项目时,没有显示,只有白色屏幕。请帮忙..

主要活动代码:

public class MainActivity extends AppCompatActivity {


RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private MyAdapter mViewAdapter;

List<News> news_list = new ArrayList<>();

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


    new ParseNewsJSON().execute();

    mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    //mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    mViewAdapter= new MyAdapter(news_list);
    mRecyclerView.setAdapter(mViewAdapter);

}

}

MyAdapter代码:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private List<News> newsArticleList;

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView title;

    //description, genre;

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

        title = (TextView) itemView.findViewById(R.id.textView_articleHeading);
//            description = (TextView) itemView.findViewById(R.id.textView_description);
//            image = (TextView) itemView.findViewById(R.id.rating);
    }
}

public MyAdapter (List<News> newsArticleList){

    this.newsArticleList = newsArticleList;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_main, parent, false);
    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {

    News news = newsArticleList.get(position);


    holder.title.setText(news.getHeading());


}

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

JSON解析代码:

public class ParseNewsJSON extends AsyncTask<String, Void, String> {

private final String JSON_URL = "https://newsapi.org/v2/top-headlines?sources=cnn&apiKey=c80ddd850a524fe5975cad881d6f4aba";

String result ="";

ArrayList<String> article_heading = new ArrayList<>();

@Override
protected String doInBackground(String... strings) {

    try {
        URL url = new URL(JSON_URL);

        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

        InputStream inputStream = httpURLConnection.getInputStream();

        InputStreamReader reader = new InputStreamReader(inputStream);

        int data = reader.read();

        while(data !=-1){

            char current = (char) data;

            result += current;

            data = reader.read();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

@Override
protected void onPostExecute(String s) {

    try {
        JSONObject jsonObject = new JSONObject(result);

        JSONArray jsonArray = jsonObject.getJSONArray("articles");

        for(int i = 0; i<jsonArray.length(); i++){

            News news = new News();

            news.setHeading(String.valueOf(article_heading.add(jsonArray.getJSONObject(i).optString("title"))));

           // article_heading.add(jsonArray.getJSONObject(i).optString("title"));


            Log.d("news_JSON", article_heading.toString());

            Log.d("news", news.getHeading().toString());
            //article_heading.add(jsonArray.getJSONObject(i).optString("title"));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    super.onPostExecute(s);
}
}

news.Java:

public class News {

private String heading;
//private String descrpiton;
private String img;


public String getHeading() {
    return heading;
}

public void setHeading(String heading) {
    this.heading = heading;
}

public String getImg() {
    return img;
}

public void setImg(String img) {
    this.img = img;
}
}

我读了几个线程然而他们还没有解决我的问题This is the output when I run the emulator

activity_main.xml中:

android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.RecyclerView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/recyclerView"
    xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.v7.widget.RecyclerView>

card_layout.xml:

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_marginTop="15dp"
android:layout_height="200dp"
app:cardCornerRadius="25dp"


>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"



    >


    <TextView
        android:id="@+id/textView_articleHeading"
        android:layout_marginLeft="5dp"
        android:textStyle="bold"
        android:textColor="#000000"
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1.0"
        android:id="@+id/rating"
        android:scaleType="center"

       />

    <TextView
        android:id="@+id/textView_description"
        android:textSize="15sp"
        android:textColor="#000000"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

java android json android-studio
4个回答
1
投票

我认为问题是你没有将任何数据添加到你的news_list ArrayList检查你的ParseNewsJSON onPostExecute()方法

像下面的代码一样在ParseNewsJSON中进行更改

@Override
protected void onPostExecute(String s) {

    try {
        JSONObject jsonObject = new JSONObject(result);

        JSONArray jsonArray = jsonObject.getJSONArray("articles");

        for(int i = 0; i<jsonArray.length(); i++){

            News news = new News();

            news.setHeading(String.valueOf(article_heading.add(jsonArray.getJSONObject(i).optString("title"))));

           // article_heading.add(jsonArray.getJSONObject(i).optString("title"));


            Log.d("news_JSON", article_heading.toString());

            Log.d("news", news.getHeading().toString());
            news_list.add(news);

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    mViewAdapter= new MyAdapter(news_list);
    mRecyclerView.setAdapter(mViewAdapter);
    mViewAdapter.notifyDataSetChanged().
    super.onPostExecute(s);
}

0
投票

你必须在onPostExecute()类的ParseNewsJSON中为“news_list”添加值。然后通知适配器。

@Override
protected void onPostExecute(String s) {

            try {
                JSONObject jsonObject = new JSONObject(result);
                JSONArray jsonArray = jsonObject.getJSONArray("articles");
                for (int i = 0; i < jsonArray.length(); i++) {

                    News news = new News();
                    news.setHeading(String.valueOf(article_heading.add(jsonArray.getJSONObject(i).optString("title"))));

                    news_list.add(news);
                    Log.d("news_JSON", article_heading.toString());

                    Log.d("news", news.getHeading().toString());
                }

                mViewAdapter.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            super.onPostExecute(s);
        }

0
投票

将您的解析数据添加到onPostExecute()中的arraylist并通知适配器。

@Override
protected void onPostExecute(String s) {

            try {
                JSONObject jsonObject = new JSONObject(result);
                JSONArray jsonArray = jsonObject.getJSONArray("articles");
                for (int i = 0; i < jsonArray.length(); i++) {
                    News news = new News();
                    news.setHeading(String.valueOf(article_heading.add(jsonArray.getJSONObject(i).optString("title"))));
                    news_list.add(news);                       
                }
                mViewAdapter.notifyDataSetChanged();//add this line
            } catch (JSONException e) {
                e.printStackTrace();
            }
            super.onPostExecute(s);
        }

0
投票

在主要活动中

mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
//mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager( new LinearLayoutManager(getApplicationContext()));
mViewAdapter= new MyAdapter(new ArrayList<News>);
mRecyclerView.setAdapter(mViewAdapter);

在post执行方法中

@Override
protected void onPostExecute(String s) {

 try {
        JSONObject jsonObject = new JSONObject(result);

        JSONArray jsonArray = jsonObject.getJSONArray("articles");

        for(int i = 0; i<jsonArray.length(); i++){

        news_list.add(jsonArray.getJSONObject(i));
 news.setHeading(String.valueOf(article_heading.add(jsonArray.getJSONObject(i).optString("title"))));

    //article_heading.add(jsonArray.getJSONObject(i).optString("title"))

           Log.d("news_JSON", article_heading.toString());
            Log.d("news", news.getHeading().toString());
        }
mViewAdapter.add(news_list);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    super.onPostExecute(s);
}

在适配器中改变这个

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
    if (viewHolder instanceof ViewHolder){
        ViewHolder holder = (ViewHolder) viewHolder;
        holder.displayDetails(newsArticleList.get(position));
    }

}

在适配器中添加方法

 public void addItems(List<News> items){
        if ( items != null && items.size() > 0 ) {
            for (int i = 0; i < items.size(); i++) {
                newsArticleList.add(items.get(i));
                notifyItemInserted(newsArticleList.size() - 1);
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.