简体   繁体   中英

card view not showing up

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;
}

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM