2016-04-24 13 views
3

Ich möchte eine Liste von Datensätzen mit RecyclerView anzeigen.RecyclerView zeigt nur eine einzige Zeile in Android

Aber wenn ich die Anwendung ausführen, zeigt es nur einen Datensatz.

Ich verifizierte meinen Code, und alles scheint gut.

Adapter Code:

public class AdapterData extends RecyclerView.Adapter<AdapterData.DummyHolder> { 

    private LayoutInflater layoutInflater; 
    private ArrayList<String> mItems = new ArrayList<>(); 
    public Context ThisContext; 

    public AdapterData(Context context) 
    { 
     layoutInflater = LayoutInflater.from(context); 
     mItems = generateValues(); 
     ThisContext = context; 
    } 

    public static ArrayList<String> generateValues(){ 
     ArrayList<String> Dummy = new ArrayList<>(); 
     for(int i=1; i<100; i++) 
     { 
      Dummy.add("Item"+i); 
      Log.d("MTS", String.valueOf(i)); 
     } 
     return Dummy; 
    } 

    @Override 
    public DummyHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view= layoutInflater.inflate(R.layout.row_layout,parent,false); 
     DummyHolder holder=new DummyHolder(view); 
     return holder; 
    } 

    @Override 
    public void onBindViewHolder(DummyHolder holder, int position) { 
     holder.txt_name.setText(mItems.get(position)); 
     Log.d("MAN=",mItems.get(position)); 
    } 

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

    public static class DummyHolder extends RecyclerView.ViewHolder{ 

     TextView txt_name; 

     public DummyHolder(View itemView) { 
      super(itemView); 
      txt_name = (TextView) itemView.findViewById(R.id.tx_name); 
     } 
    } 
} 

Row Code:

<?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="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/tx_name" 
     android:hint="Hello"/> 

</LinearLayout> 

Haupt Actiivity XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    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:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.hogwarts.harrypotter.recyclerdemo.MainActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/rv_list"> 

    </android.support.v7.widget.RecyclerView> 
</RelativeLayout> 

MainActivity Code:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    recyclerView = (RecyclerView) findViewById(R.id.rv_list); 
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(linearLayoutManager); 
    recyclerView.setAdapter(new AdapterData(this)); 
} 
+0

paste xml hier. –

+0

Ich habe den ganzen Code hinzugefügt, bitte überprüfen Sie, wo ich falsch gemacht habe. –

Antwort

16

Werfen Sie einen Blick auf Artikellayout. Das Root-Layout hat android: layout_height = "match_parent". Es bedeutet, nehmen Sie die volle Bildschirmhöhe. Wechseln Sie zu wrap_content und lassen Sie Party. Say oh yeah

+1

Oh ja, danke Kumpel. –

+0

Genie! Hätte nie gedacht, das könnte das Problem sein – pblead26

+0

Danke Kumpel .... es funktioniert für mich – Hasnain

Verwandte Themen