2016-04-01 9 views
0

the same cardview data called twice and overlappingrecycleview zeigt die gleichen cardview Daten zweimal an

Ich bin Neuling in der Programmierung. Ich brauche nur Ihre Anleitung zur Recyclerview. Wie Sie auf dem Bild sehen (bitte klicken Sie auf den Link, um das Bild zu sehen, da ich das Bild noch nicht direkt anhängen kann), werden die gleichen Daten zweimal aufgerufen. Ich weiß nicht, was der Grund ist. Bitte helfen Sie.

Hier ist meine activity_main.xml

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

<fragment 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/fragment" 
    android:name="com.georgekurniawan.radiorohaniindonesia.MainActivityFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:layout="@layout/fragment_main" /> 

mein fragment_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.georgekurniawan.radiorohaniindonesia.MainActivityFragment" 
> 
    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical" 
     android:id="@+id/rv" 
     /> 

cardview

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
card_view:cardCornerRadius="4dp" 
card_view:cardElevation="@dimen/cardview_default_elevation" 
android:foreground="?android:attr/selectableItemBackground" 
android:layout_margin="0dp" 
android:id="@+id/cv_radio" 
> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/logo" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginRight="10dp" 
      android:maxWidth="140dp" 
      android:scaleType="center" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/radio_name" 
      android:layout_toRightOf="@+id/logo" 
      android:layout_alignParentTop="true" 
      android:textSize="21sp" 
      android:gravity="center_horizontal|center_vertical" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/radio_gelombang" 
      android:layout_toRightOf="@+id/logo" 
      android:layout_below="@+id/radio_name" 
      android:gravity="center_horizontal|center_vertical" /> 

    </RelativeLayout> 

Mein recycleadapter

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

public ArrayList<StasiunRadio> listradio; 
RecycleAdapter(ArrayList<StasiunRadio> listradio){ 
    this.listradio = listradio; 
} 

public static class ViewHolder extends RecyclerView.ViewHolder { 

    private CardView cv; 
    private TextView mNama; 
    private TextView mGelombang; 
    private ImageView mLogo; 

    public ViewHolder(View v) { 
     super(v); 
     cv=(CardView)v.findViewById(R.id.cv_radio); 
     mNama=(TextView)v.findViewById(R.id.radio_name); 
     mGelombang=(TextView)v.findViewById(R.id.radio_gelombang); 
     mLogo=(ImageView)v.findViewById(R.id.logo); 
    } 
} 

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

@Override 
public RecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) { 

    View v = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.cardview_radio, parent, false); 
    ViewHolder vh = new ViewHolder(v); 
    return vh; 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int i) { 
    StasiunRadio list = listradio.get(i); 
    holder.mNama.setText(list.getNama()); 
    holder.mGelombang.setText(list.getGelombang()); 
    holder.mLogo.setImageResource(list.getLogo()); 
}} 

main_activity_fragment

public class MainActivityFragment extends Fragment { 
private RecycleAdapter rAdapter; 
StasiunRadio radio = new StasiunRadio(); 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View myView = inflater.inflate(R.layout.fragment_main, container, false); 
    RecyclerView rvList = (RecyclerView) myView.findViewById(R.id.rv); 

    rvList.setHasFixedSize(true); 
    RecyclerView.LayoutManager layout; 
    layout = new LinearLayoutManager(getActivity()); 
    rvList.setLayoutManager(layout); 
    ArrayList<StasiunRadio> radiolist; 
    radio.inputradio(); 
    radiolist = radio.getALL(); 
    rAdapter = new RecycleAdapter(radiolist); 
    rvList.setAdapter(rAdapter); 
    rvList.setItemAnimator(new DefaultItemAnimator()); 
    return myView; 
}} 

MainActivity java

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.fragment, new MainActivityFragment()) 
       .commit(); 
    } 
} 

@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); 
}} 
+0

fügen Sie Ihren Hauptaktivitätscode, wo Sie Fragment hinzufügen, scheint es, dass doppelte hinzufügen – Gaurav

+0

@GauravPolekar Ich füge bereits den Code. Bitte werfen Sie einen Blick und helfen Sie mir – onlygrace

Antwort

0

entfernen diese

if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.fragment, new MainActivityFragment()) 
       .commit(); 
    } 

weil Sie hinzugefügt Fragment in xml, das Codefragment über Fragment ist das Hinzufügen, die bereits vorhanden ist.

+0

Danke, es funktioniert jetzt gut. Ich frage mich, warum dieser Code entfernt werden muss, obwohl dieser Code verwendet wird, um das Fragment in die Mainaktivität zu bringen. Irgendeine Erklärung? – onlygrace

+0

überprüfen Sie die bearbeitete Antwort – Gaurav