2

I am implementing card view through recycler view in android but my card view is not showing up. I am using custom adapter to populate data to card view. I have tried all things but the card view is not showing up.

MainActivity.java:
public class MainActivity extends Activity {
private String[] mImage = {"sahil", "kunal", "somy", "manav"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //setting adapter
    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler);
    GridLayoutManager manager = new GridLayoutManager(this, 2);
    recyclerView.setAdapter(new Adapter(mImage));
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(manager);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recycler" android:scrollbars="vertical" tools:context=".MainActivity">

'

grid_item.xml:

'

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"/>

</LinearLayout>

'

Adapter.java:
public class Adapter extends RecyclerView.Adapter<Adapter.Holder> {
private String[] mImage;

public Adapter(String[] mImage) {
    this.mImage = mImage;
}

public static class Holder extends RecyclerView.ViewHolder{
  public CardView cardView;
  public TextView textView;

    public Holder(View itemView) {
        super(itemView);
        cardView = (CardView)itemView.findViewById(R.id.card);
        textView = (TextView)itemView.findViewById(R.id.text);
    }
}

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

@Override
public void onBindViewHolder(Holder holder, int position) {
    holder.textView.setText(mImage[position]);
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

@Override
public int getItemCount() {
    return 0;
}

}

1 Answers1

5

Change your getItemCount() method to this:

@Override
public int getItemCount() {
    return mImage.length;
}

Right now you are saying your adapter has 0 items, hence why it's not drawing anything.

telkins
  • 10,440
  • 8
  • 52
  • 79
  • @SahilShokeen Good to hear! Please accept this answer when it lets you. :) – telkins Oct 19 '15 at 03:03
  • I am new so i don;t have 15 reputations –  Oct 19 '15 at 03:27
  • 2
    @SahilShokeen there is no reputation limit for accepting answers. If this answer solves your problem, you can accept it by clicking the checkmark next to the answer. This will help future readers who stumble upon your question. :) – AdamMc331 Oct 26 '15 at 15:45
  • @McAdam331 can you help in my new question: http://stackoverflow.com/questions/33338507/images-loading-very-slowly –  Oct 26 '15 at 16:25