我已经编写了代码来制作卡片查看并创建了一个Recycler View和Card View布局并制作了自定义适配器类但是在主屏幕卡上没有显示
这是我的适配器类 BankAdapter.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.vikas.financemanager.R;
import com.example.vikas.financemanager.model.Bank_details;
import java.util.Collections;
import java.util.List;
/**
* Created by vikas on 28/9/17.
*/
public class BankAccountAdapter extends RecyclerView.Adapter<BankAccountAdapter.BankAccountViewHolder> {
private List<Bank_details> bank_details;
private Context mContext;
public BankAccountAdapter(Context mContext,List<Bank_details> bank_details)
{
this.mContext= mContext;
this.bank_details=bank_details;
}
@Override
public BankAccountViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_list,parent,false);
return new BankAccountViewHolder(itemView);
}
@Override
public void onBindViewHolder(BankAccountViewHolder holder, int position) {
Bank_details details= bank_details.get(position);
holder.BankName.setText(details.getBank_name());
holder.Bank_Balance.setText(details.getBank_balance());
holder.Account_number.setText(details.getAccount_number());
holder.Transaction_date.setText(details.getDate_of_transaction());
}
@Override
public int getItemCount() {
return 0;
}
public class BankAccountViewHolder extends RecyclerView.ViewHolder {
public TextView BankName;
public TextView Bank_Balance;
public TextView Transaction_date;
public TextView Account_number;
public TextView Title;
public BankAccountViewHolder(View v) {
super(v);
BankName = (TextView) v.findViewById(R.id.BankName);
Bank_Balance = (TextView) v.findViewById(R.id.bank_balance);
Transaction_date = (TextView) v.findViewById(R.id.transaction_date);
Account_number = (TextView) v.findViewById(R.id.account_number);
Title = (TextView) v.findViewById(R.id.title);
}
}
}
这是我的activity_main.xml
<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
</LinearLayout>
<include
android:id="@+id/cardList"
layout="@layout/card_recycle_view" />
<!-- <FrameLayout
android:id="@+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />-->
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.example.vikas.financemanager.activity.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_nav_drawer"
tools:layout="@layout/fragment_nav_drawer" />
</android.support.v4.widget.DrawerLayout>
这是我的Card_list.xml
<ScrollView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="center_vertical"
android:text="Account Summary"
android:background="@color/colorPrimary"
android:textColor="@android:color/white"
android:textSize="14dp"/>
<TextView
android:id="@+id/BankName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="UCO BANK"
android:textSize="10dp" />
<TextView
android:id="@+id/account_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/BankName"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="**1032"
android:textSize="10dp" />
<TextView
android:id="@+id/bank_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/BankName"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="Rs 20000"
android:textSize="10dp"
android:textStyle="bold" />
<TextView
android:id="@+id/transaction_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/bank_balance"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:text="12/10/1996"
android:textSize="10dp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</ScrollView>
这是我的主要活动
package com.example.vikas.financemanager.activity;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.example.vikas.financemanager.R;
import com.example.vikas.financemanager.adapter.BankAccountAdapter;
import com.example.vikas.financemanager.model.Bank_details;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
private RecyclerView recList;
private BankAccountAdapter adapter;
private LinearLayoutManager llm;
List<Bank_details> details = new ArrayList<Bank_details>();
private String TAG=MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
// mToolbar.setTitleTextColor(0xFFFFF);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("DashBoard");
getSupportActionBar().setDisplayShowHomeEnabled(true);
details = new ArrayList<>();
recList = (RecyclerView) findViewById(R.id.cardList);
llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
adapter = new BankAccountAdapter(getApplicationContext(),details);
bank_details();
recList.setAdapter(adapter);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
}
private void bank_details() {
Bank_details di = new Bank_details("UCO BANK","**1032","Rs 2000","12/10/1996");
details.add(di);
adapter.notifyDataSetChanged();
Log.e(TAG,"size "+ details.size());
}
@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);
}
@Override
public void onDrawerItemSelected(View view, int position) {
}
}
根据我的说法,我认为我已经做了一切正确但我无法理解为什么卡片无法显示。请帮帮我
答案 0 :(得分:1)
我弄错了我在Adapter类中的getItemCount方法中返回0而不是details.size()。