2017-04-03 20 views
0

Ich lerne gerade den RecyclerView und kann nicht herausfinden, warum das imageView mit android: id = "@ + id/im_item_icon" nicht auf dem Bildschirm angezeigt wird. Ich habe sogar eine andere Bildansicht, die funktioniert, aber wenn ich die IDs tausche, funktioniert die ursprüngliche nicht funktionierende Ansicht und die funktionierende wird nicht funktionieren. Im Grunde muss das Problem mit der ID sein, aber ich kann nicht herausfinden warum. BTW habe ich den Code aus einem Blog "http://wiseassblog.com/tutorials/2016/03/04/how-to-build-a-recyclerview/"ImageView kann nicht auf RecyclerView angezeigt werden

DerpAdapter.java

public class DerpAdapter extends RecyclerView.Adapter<DerpAdapter.DerpHolder> { 

private List<ListItem> listData; 
private LayoutInflater inflater; 

public DerpAdapter(List<ListItem> listData, Context c){ 
    inflater = LayoutInflater.from(c); 
    this.listData = listData; 
} 

@Override 
public DerpAdapter.DerpHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = inflater.inflate(R.layout.list_item, parent, false); 
    return new DerpHolder(view); 
} 

@Override 
public void onBindViewHolder(DerpHolder holder, int position) { 
    ListItem item = listData.get(position); 
    holder.title.setText(item.getTitle()); 
    holder.icon.setImageResource(item.getImageResId()); 
} 

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

class DerpHolder extends RecyclerView.ViewHolder { 

    private TextView title; 
    private ImageView icon; 
    private View container; 

    public DerpHolder(View itemView) { 
     super(itemView); 

     title = (TextView)itemView.findViewById(R.id.lbl_item_text); 
     icon = (ImageView)itemView.findViewById(R.id.im_item_icon); 
     //We'll need the container later on, when we add an View.OnClickListener 
     container = itemView.findViewById(R.id.cont_item_root); 
    } 
} 

DerpData.java

public class DerpData { 
private static final String[] titles = {"Nothingness cannot be defined", 
     "Time is like a river made up of the events which happen, and a violent stream; " + 
       "for as soon as a thing has been seen, it is carried away, and another comes" + 
       " in its place, and this will be carried away too,", 
     "But when I know that the glass is already broken, every minute with it is precious.", 
     "For me, it is far better to grasp the Universe as it really is than to persist in" + 
       " delusion, however satisfying and reassuring.", 
     "The seeker after the truth is not one who studies the writings of the ancients and," + 
       " following his natural disposition, puts his trust in them, but rather the" + 
       " one who suspects his faith in them and questions what he gathers from them," + 
       " the one who submits to argument and demonstration, and not to the " + 
       "sayings of a human being whose nature is fraught with all kinds " + 
       "of imperfection and deficiency.", 
     "You must take personal responsibility. You cannot change the circumstances, the" + 
       " seasons, or the wind, but you can change yourself. That is something you" + 
       " have charge of." 
}; 
private static final String[] subTitles = {"Bruce Lee", 
     "Marcus Aurelius", 
     "Meng Tzu", 
     "Ajahn Chah", 
     "Carl Sagan", 
     "Alhazen", 
     "Jim Rohn" 

}; 
private static final int icon = R.drawable.ic_tonality_black_36dp; 

public static List <ListItem> getListData() { 
    List <ListItem> data = new ArrayList <>(); 

    //Repeat process 4 times, so that we have enough data to demonstrate a scrollable 
    //RecyclerView 
    for (int x = 0; x < 4; x++) { 
     //create ListItem with dummy data, then add them to our List 
     for (int i = 0; i < titles.length; i++) { 
      ListItem item = new ListItem(); 
      item.setTitle(titles[i]); 
      item.setSubTitle(subTitles[i]); 
      data.add(item); 
     } 
    } 
    return data; 
} 

ListItem.java

public class ListItem { 
private int imageResId; 
private String subTitle; 
private String title; 
private boolean favourite = false; 

public String getSubTitle() { 
    return subTitle; 
} 

public void setSubTitle(String subTitle) { 
    this.subTitle = subTitle; 
} 

public boolean isFavourite() { 
    return favourite; 
} 

public void setFavourite(boolean favourite) { 
    this.favourite = favourite; 
} 

public int getImageResId() { 
    return imageResId; 
} 

public void setImageResId(int imageResId) { 
    this.imageResId = imageResId; 
} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

DetailActivity.java

public class DetailActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_detail); 
} 

ListActivity.java

public class ListActivity extends AppCompatActivity { 

private RecyclerView recView; 
private DerpAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_list); 

    recView = (RecyclerView)findViewById(R.id.rec_list); 
    //Check out GridLayoutManager and StaggeredGridLayoutManager for more options 
    recView.setLayoutManager(new LinearLayoutManager(this)); 

    adapter = new DerpAdapter(DerpData.getListData(), this); 
    recView.setAdapter(adapter); 
} 

activity_detail.xml

android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="16dp" 
android:paddingLeft="16dp" 
android:paddingRight="16dp" 
android:paddingTop="16dp" 
tools:context="application.binarysoup.jsonpractice.ui.DetailActivity"> 

<TextView 
    android:id="@+id/lbl_quote_text" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fontFamily="sans-serif-medium" /> 

<TextView 
    android:id="@+id/lbl_quote_attribution" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/lbl_quote_text" 
    android:fontFamily="sans-serif-light" 
    android:textStyle="italic" /> 

activity_list.xml

xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="application.binarysoup.jsonpractice.ui.ListActivity" 
tools:layout_editor_absoluteX="0dp" 
tools:layout_editor_absoluteY="81dp"> 

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

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/cont_item_root" 
android:layout_width="match_parent" 
android:layout_height="72dp" 
android:background="@drawable/background_state_drawable" 
android:clickable="true" 
> 

<ImageView 
    android:id="@+id/im_item_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:paddingLeft="16dp" 
    android:src="@drawable/ic_tonality_black_36dp" /> 


<TextView 
    android:id="@+id/lbl_item_text" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/im_item_icon" 
    android:layout_marginLeft="72dp" 
    android:layout_marginRight="48dp" 
    android:ellipsize="end" 
    android:fontFamily="sans-serif" 
    android:singleLine="true" 
    android:text="Sois comme l'eau mon ami" 
    android:textColor="@android:color/black" 
    android:textSize="16sp" /> 

<TextView 
    android:id="@+id/lbl_item_sub_title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/lbl_item_text" 
    android:layout_marginLeft="72dp" 
    android:layout_marginRight="48dp" 
    android:ellipsize="end" 
    android:fontFamily="sans-serif" 
    android:singleLine="true" 
    android:text="Mononc' J" 
    android:textSize="14sp" /> 

<ImageView 
    android:id="@+id/im_item_icon_secondary" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:clickable="true" 
    android:padding="16dp" 
    android:src="@drawable/ic_star_border_black_24dp" 
    android:background="@drawable/background_state_drawable" 
    /> 

+2

'Inhaber .icon.setImageResource (item.getImageResId()); '- Sie setzen 'imageResId' niemals auf Ihre' ListItem's in der erstellten Schleife sie, so ist es 0, und 'setImageResource()' tut einfach nichts. –

+1

Ehrfürchtige Erklärung. Vielen Dank! –

Antwort

0

Kann nicht Image bekommen zu zeigen, bis auf RecyclerView

Sie haben imageResId, um initialisieren es in der recyclerView zu erscheinen

for (int i = 0; i < titles.length; i++) { 
      ListItem item = new ListItem(); 
      item.setTitle(titles[i]); 
      item.setSubTitle(subTitles[i]); 
//put the code to initialise the imageResId here 
      data.add(item); 
     } 
Verwandte Themen