row layout not set on on Main Activity Recycler View - java

I am just parse some data and display on my ListProperty Class. I am using RecyclerView and CardView as a row layout. Somehow all is working fine i don't see any error in verbose. But the Data and row layout dosent display in my main activity. Activity is showing blank. I am trying from hours but kind find the error. Please someone help
My MainActivity.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<android.support.v7.widget.RecyclerView
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
This is row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageViewFront" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_marginLeft="100dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="New Text"
android:textColor="#0055CC"
android:id="#+id/textViewPropertyname" />
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#0055CC"
android:layout_columnWeight="1"
android:id="#+id/textViewPropertyDescription"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
This is myAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
List<Bean> DataList;
public MyAdapter(List<Bean> List){
super();
DataList = List;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView propertyName;
public TextView propertyDesc;
public ImageView ImageFront;
public MyViewHolder(View v) {
super(v);
propertyName = (TextView) v.findViewById(R.id.textViewPropertyname);
propertyDesc = (TextView) v.findViewById(R.id.textViewPropertyDescription);
ImageFront = (ImageView) v.findViewById(R.id.imageViewFront);
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.list_card_view,parent,false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
Bean bean = DataList.get(position);
holder.propertyName.setText(bean.getPropertyName());
holder.propertyDesc.setText(bean.getPropertyDescription());
holder.ImageFront.setImageResource(R.drawable.agriculture);
}
#Override
public int getItemCount() {
return DataList.size();
}
}
And this is my MainActivity.java
public class ListProperty extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
Bean bean;
AQuery aQuery;
JSONObject jsonObject;
List<Bean> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_property);
aQuery= new AQuery(getApplicationContext());
String URL = "http://192.168.0.107/propertyapp/service/simpleservice.php";
list = new ArrayList<Bean>();
mRecyclerView = (RecyclerView) findViewById(R.id.cardList);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(list);
mRecyclerView.setAdapter(mAdapter);
aQuery.ajax(URL, JSONObject.class,new AjaxCallback<JSONObject>(){
#Override
public void callback(String url, JSONObject object, AjaxStatus status) {
try {
bean = new Bean();
list = new ArrayList<Bean>();
String string = object.getString("data");
JSONArray jsonArray = new JSONArray(string);
for (int i=0;i<jsonArray.length();i++) {
jsonObject = jsonArray.getJSONObject(i);
bean.setPropertyName("property_name");
bean.setPropertyDescription("property_desc");
list.add(bean);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}

Try to remove parent LinearLayout. Because you have CardView which is acting as list item.
Try this row.xml -
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageViewFront" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_marginLeft="100dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="New Text"
android:textColor="#0055CC"
android:id="#+id/textViewPropertyname" />
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#0055CC"
android:layout_columnWeight="1"
android:id="#+id/textViewPropertyDescription"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Hope it will help :)

Just add notifyDataSetChanged() to your Activity as below:
aQuery.ajax(URL, JSONObject.class,new AjaxCallback<JSONObject>(){
#Override
public void callback(String url, JSONObject object, AjaxStatus status) {
try {
bean = new Bean();
list = new ArrayList<Bean>();
String string = object.getString("data");
JSONArray jsonArray = new JSONArray(string);
for (int i=0;i<jsonArray.length();i++) {
jsonObject = jsonArray.getJSONObject(i);
bean.setPropertyName("property_name");
bean.setPropertyDescription("property_desc");
list.add(bean);
}
//here notify your adapter
//is to Notify any registered observers that the data set has changed.
mAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
});

Because on your callback you have created new instance of list
list = new ArrayList<Bean>();
then in fact, the new created list and DataList in your adapter are pointing to 2 difference objects => calling notifyDataSetChanged here won't work. Try writing method updateData on MyAdapter and call it from your callback method
public void updateData(List<Bean> List){
DataList = List;
notifyDataSetChanged();
}
On your callback:
aQuery.ajax(URL, JSONObject.class,new AjaxCallback<JSONObject>(){
#Override
public void callback(String url, JSONObject object, AjaxStatus status) {
try {
bean = new Bean();
list = new ArrayList<Bean>();
String string = object.getString("data");
JSONArray jsonArray = new JSONArray(string);
for (int i=0;i<jsonArray.length();i++) {
jsonObject = jsonArray.getJSONObject(i);
bean.setPropertyName("property_name");
bean.setPropertyDescription("property_desc");
list.add(bean);
}
// update data
mAdapter.updateData(list);
} catch (JSONException e) {
e.printStackTrace();
}
}
});

Related

unable to make proper ui like playstore

Hi i am developing an app where i have to make a ui like playtstore for that i saw this tutorial but now my problem is that i'm unable to add different
text for different rows and second is that even after setting height and width it doesnt seem to be get affected.
Please if someone can help me out here i'm stuck at this point
my code is as follows
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int[] images = {R.drawable.vancouver,R.drawable.party,R.drawable.hands_ip,R.drawable.dj};
// Inflate the layout for this fragment
allSampleData = new ArrayList<SectionDataModel>();
createDummyData();
HorizontalAdapter firstAdapter = new HorizontalAdapter(images);
View view = inflater.inflate(R.layout.fragment_home, container, false);
NestedScrollView nestedScrollView = view.findViewById(R.id.scrollView);
llm = new LadderLayoutManager(1.5f, 0.85f, LadderLayoutManager.HORIZONTAL).
setChildDecorateHelper(new LadderLayoutManager
.DefaultChildDecorateHelper(getResources().getDimension(R.dimen.item_max_elevation)));
llm.setChildPeekSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
30, getResources().getDisplayMetrics()));
//llm.setReverse(true);
/*llm.setMaxItemLayoutCount(5);
rcv = (RecyclerView) view.findViewById(R.id.rcv);
rcv.setLayoutManager(llm);
new LadderSimpleSnapHelper().attachToRecyclerView(rcv);
adapter = new HSAdapter();
rcv.setAdapter(adapter);
multi_scroll_recyclerview = (RecyclerView)view.findViewById(R.id.multi_scroll_recyclerview);
multi_scroll_recyclerview.setNestedScrollingEnabled(false);
multi_scroll_layout_manager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
multi_scroll_recyclerview.setLayoutManager(multi_scroll_layout_manager);
multi_scroll_adapter = new RecyclerViewDataAdapter(getActivity(),allSampleData);
multi_scroll_recyclerview.setAdapter(multi_scroll_adapter);
multi_scroll_recyclerview.setHasFixedSize(true);*/
/* MultiSnapRecyclerView firstRecyclerView = (MultiSnapRecyclerView)view.findViewById(R.id.first_recycler_view);
LinearLayoutManager firstManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
firstRecyclerView.setLayoutManager(firstManager);
firstRecyclerView.setAdapter(firstAdapter);
HorizontalAdapter secondAdapter = new HorizontalAdapter(images);
MultiSnapRecyclerView secondRecyclerView =(MultiSnapRecyclerView) view.findViewById(R.id.second_recycler_view);
LinearLayoutManager secondManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
secondRecyclerView.setLayoutManager(secondManager);
secondRecyclerView.setAdapter(secondAdapter);
HorizontalAdapter thirdAdapter = new HorizontalAdapter(images);
MultiSnapRecyclerView thirdRecyclerView = (MultiSnapRecyclerView)view.findViewById(R.id.third_recycler_view);
LinearLayoutManager thirdManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
thirdRecyclerView.setLayoutManager(thirdManager);
thirdRecyclerView.setAdapter(thirdAdapter);*/
return view;
}
public void createDummyData() {
for (int i = 1; i <= 5; i++) {
SectionDataModel dm = new SectionDataModel();
dm.setHeaderTitle("Clubs");
dm.setHeadertitle2("Lounge");
dm.setHeadertitle3("Cafe");
dm.setHeadertitle4("Rooftop");
ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
for (int j = 0; j <= 5; j++) {
singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
}
dm.setAllItemsInSection(singleItem);
allSampleData.add(dm);//line which is causing issue
}
}
code for adapter
public class SectionListDataAdapter extends RecyclerView.Adapter<SectionListDataAdapter.SingleItemRowHolder> {
private ArrayList<SingleItemModel> itemsList;
private Context mContext;
public SectionListDataAdapter(Context context, ArrayList<SingleItemModel> itemsList) {
this.itemsList = itemsList;
this.mContext = context;
}
#Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, null);
SingleItemRowHolder mh = new SingleItemRowHolder(v);
return mh;
}
#Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {
SingleItemModel singleItem = itemsList.get(i);
holder.tvTitle.setText(singleItem.getName());
/* Glide.with(mContext)
.load(feedItem.getImageURL())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.error(R.drawable.bg)
.into(feedListRowHolder.thumbView);*/
}
#Override
public int getItemCount() {
return (null != itemsList ? itemsList.size() : 0);
}
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
protected TextView tvTitle;
protected ImageView itemImage;
public SingleItemRowHolder(View view) {
super(view);
this.tvTitle = (TextView) view.findViewById(R.id.tvTitle);
this.itemImage = (ImageView) view.findViewById(R.id.itemImage);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(v.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
My code for model class
public class SectionDataModel {
private String headerTitle,headertitle2,headertitle3,headertitle4;
private ArrayList<SingleItemModel> allItemsInSection;
public SectionDataModel() {
}
public SectionDataModel(String headerTitle, ArrayList<SingleItemModel> allItemsInSection) {
this.headerTitle = headerTitle;
this.allItemsInSection = allItemsInSection;
}
public String getHeaderTitle() {
return headerTitle;
}
public String getHeadertitle2() {
return headertitle2;
}
public String getHeadertitle3() {
return headertitle3;
}
public String getHeadertitle4() {
return headertitle4;
}
public void setHeaderTitle(String headerTitle) {
this.headerTitle = headerTitle;
}
public void setHeadertitle2(String headertitle2) {
this.headertitle2 = headertitle2;
}
public void setHeadertitle3(String headertitle3) {
this.headertitle3 = headertitle3;
}
public void setHeadertitle4(String headertitle4) {
this.headertitle4 = headertitle4;
}
public ArrayList<SingleItemModel> getAllItemsInSection() {
return allItemsInSection;
}
public void setAllItemsInSection(ArrayList<SingleItemModel> allItemsInSection) {
this.allItemsInSection = allItemsInSection;
}
}
listitem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.ct.listrtrial.Custom.CustomTextViewMedium
android:id="#+id/itemTitle"
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#+id/btnMore"
android:text="Sample title"
android:textColor="#android:color/white"
android:textSize="27sp" />
<ImageView
android:id="#+id/btnMore"
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_marginTop="13dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/ic_more_horiz_black_24dp"
android:textColor="#FFF" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="160dp"
android:layout_gravity="center_vertical"
android:orientation="horizontal" />
</LinearLayout>
main fragment.xml
<android.support.v4.widget.NestedScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/scrollView"
android:fillViewport="true"
android:background="#color/colorPrimary"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rcv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/multi_scroll_recyclerview"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
listsinglecard.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="220dp"
android:layout_marginLeft="18dp"
android:layout_height="200dp"
app:cardCornerRadius="65dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:orientation="vertical">
<ImageView
android:id="#+id/itemImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="#drawable/vancouver" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/itemImage"
android:gravity="center"
android:padding="5dp"
android:visibility="gone"
android:text="Sample title"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>

My database show nothing in my listview

In my project I have database and I want to fill data from database into my listview. But It shows nothing without any errors.
I open the database easily and get data from it and set to my arraylist and finally I put in into my listview.
where is the problem?
I am new with android
here is my code:
public class MainActivity extends Activity {
private ListView listView;
public static SqliteHelper database;
private ArrayList<ItemJomle> arrayDatas;
private ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayDatas = new ArrayList<>();
database = new SqliteHelper(this);
try {
database.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
listView = (ListView)findViewById(R.id.lstcontent);
updateListViewData();
}
public void updateListViewData() {
database.open();
arrayDatas = database.getDataListView();
adapter = new AdapterData(arrayDatas);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
database.close();
}
public static void updateFav(int id,String value){
database.open();
database.updateDatabase(id,value);
database.close();
}
}
And here is my code:
public ArrayList<ItemJomle> getDataListView() {
String selectQuery = "SELECT * FROM " + TBL_NAME ;
arrayListViwedatas.clear();
Cursor cu = Mydb.rawQuery(selectQuery, null);
if(cu!=null){
cu.moveToFirst();
if(cu.moveToFirst()){
while (cu.isAfterLast() == false) {
ItemJomle item = new ItemJomle();
item.id = cu.getInt(cu.getColumnIndex("id"));
item.subject = cu.getString(cu.getColumnIndex("subject"));
item.text = cu.getString(cu.getColumnIndex("text"));
item.nubmer = cu.getString(cu.getColumnIndex("number"));
item.fav = cu.getString(cu.getColumnIndex("fav"));
arrayListViwedatas.add(item);
cu.moveToNext();
}
cu.close();
}
}
return arrayListViwedatas;
}
and my layout:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ListView
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lstcontent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:divider="#null"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<com.vizitsaz.ClickBanner_CLickYab_Holder
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"/>
</LinearLayout>

Recycler View and Card View not displaying cards

This is my Activity:
public String id;
public String passPhrase;
public ArrayList<SongCell> songs;
private RecyclerView recyclerView;
private MyAdapter myAdapter;
private RecyclerView.LayoutManager layoutManager;
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_view_layout);
Context context = this.getApplicationContext();
Bundle stateData = getIntent().getExtras();
try
{
id = stateData.getString("id");
passPhrase = stateData.getString("passPhrase");
ArrayList data;
recyclerView = (RecyclerView) findViewById(R.id.song_list);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
data = new ArrayList<SongCell>();
for (int i=0; i<5;i++)
{
data.add(new SongCell("Song "+i,"Artist "+i, null));
}
myAdapter = new MyAdapter(data);
recyclerView.setAdapter(myAdapter);
}
catch(Exception e)
{
Log.d("Error: ", e.toString());
}
}
card.xml
<android.support.v7.widget.CardView android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dp"
>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/song_photo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:background="#drawable/error" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/song_name"
android:textSize="30sp"
android:text="Song Name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#000000" />
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/vote_button"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:background="#drawable/arrow" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Activity.xml
<?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:background="#2d2d2d">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="30dp"
android:layout_marginBottom="10dp"
android:background="#222222"></TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/featSongImage1"
android:contentDescription="#string/newsongimage"
android:background="#drawable/error" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/songname"
android:id="#+id/featSongName1" />
<ImageButton
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/featSongButt1"
android:background="#drawable/arrow"
android:contentDescription="#string/votearrow" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="fill_horizontal"
android:gravity="fill_horizontal|center">
<android.support.v7.widget.RecyclerView
android:id="#+id/song_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal" />
</TableRow>
</TableLayout>
</LinearLayout>
And this is my custom adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
private ArrayList<SongCell> data;
public MyAdapter(ArrayList<SongCell> dataI)
{
data = dataI;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.song_card,viewGroup,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewholder, int pos) {
viewholder.songName.setText(data.get(pos).getName());
viewholder.artist.setText(data.get(pos).getArtist());
viewholder.image.setImageBitmap(data.get(pos).getArtwork());
}
#Override
public int getItemCount() {
return 0;
}
#Override
public int getItemViewType(int position) {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView songName;
TextView artist;
ImageView image;
ImageButton voteButton;
public ViewHolder(View v)
{
super(v);
this.songName = (TextView) v.findViewById(R.id.song_name);
this.image = (ImageView) v.findViewById(R.id.song_photo);
this.voteButton = (ImageButton) v.findViewById(R.id.vote_button);
}
}
}
I have looked at a ton of guides on how to get this working but it still doesn't work. Anyone have any clue what's going on? I'm on version 25.0.1, and have all the modules imported. But it's not adding a single card into the layout.
I thought it would be difficult to debug such a big code you uploaded. But luckily my eye went on this line
#Override
public int getItemCount() {
return 0;
}
return 0; in adapter.
Your list size is always 0.
instead write this
#Override
public int getItemCount() {
return data.size();
}
EDIT-1:
You will also get null pointer exception here
viewholder.artist.setText(data.get(pos).getArtist());
Because you are not initializing the artist variable in ViewHolder class.
Edit-2
#Override
public int getItemCount() {
return 0;
}
you can remove this particular code from your adapter. Because overriding the methods of class that we don't use might sometimes result in errors that you can't even imagine.

RecyclerView + CardView: Inflating Wrong Data

Im using CardView to create a list of cards with newsletters. The basic elements are 3 TextViews + 1 ImageView. I cant inflate 2 elements properly: the title is fine, but the link text the gets the wrong string(it gets the string referring to the body text) and the body TextView dont change at all.
Aeres a picture with the general ideia:
preview on android studio (left) and app running on device(right)
This is my CardView XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".News">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card1_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/news_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:paddingLeft="8dp"
android:text="articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle articleTitle "
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:orientation="vertical">
<TextView
android:id="#+id/news_link"
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:text="http://www.articleLink.com/" />
<TextView
android:id="#+id/news_body"
android:layout_width="match_parent"
android:layout_height="120dp"
android:paddingLeft="8dp"
android:text="Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body Body ..."
android:textColor="#000000" />
</LinearLayout>
<ImageView
android:id="#+id/news_photoId"
android:layout_width="0dp"
android:layout_height="145dp"
android:layout_weight="5"
android:src="#drawable/enfnotum" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="end"
android:paddingEnd="6dp"
android:paddingRight="6dp"
android:text="Coments Share" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#bab7b7" />
</LinearLayout>
</android.support.v7.widget.CardView>
RecycleView list layout XML code:
<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"
tools:context=".News">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Now the java classes:
Here im populating the list, calling the adapter
public class News extends AppCompatActivity {
private List<Article> articles = new ArrayList<>();
private RecyclerView rv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
rv = (RecyclerView) findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
initializeData();
initializeAdapter();
}
private void initializeData() {
articles.add(new Article("0title", "0link", "0body", R.drawable.enfnotum));
articles.add(new Article("1title", "1link", "1body", R.drawable.enfnoticias));
articles.add(new Article("2title", "2link", "2body", R.drawable.image1));
articles.add(new Article("3title", "3link", "3body", R.drawable.image2));
articles.add(new Article("4title", "4link", "4body", R.drawable.image3));
articles.add(new Article("5title", "5link", "5body", R.drawable.enfnotum));
articles.add(new Article("6title", "6link", "6body", R.drawable.image3));
articles.add(new Article("7title", "7link", "7body", R.drawable.enfnoticias));
//TODO: database
}
private void initializeAdapter() {
NewsRVAdapter adapter = new NewsRVAdapter(articles);
rv.setAdapter(adapter);
}
}
My classe to create the list object, few getters
public class Article {
private String articleTitle;
private String articleLink;
private String articleBody;
private int articlePhotoId;
public Article(String articleTitle, String articleLink, String articleBody, int articlePhotoId) {
this.articleTitle = articleTitle;
this.articleLink = articleLink;
this.articleBody = articleBody;
this.articlePhotoId = articlePhotoId;
}
public String getArticleTitle() {
return articleTitle;
}
public String getArticleLink() {
return articleLink;
}
public String getArticleBody() {
return articleBody;
}
public int getArticlePhotoId() {
return articlePhotoId;
}
}
Inflating the layout
public class NewsRVAdapter extends RecyclerView.Adapter<NewsRVAdapter.ArticleViewHolder> {
List<Article> articles;
NewsRVAdapter(List<Article> articles) {
this.articles = articles;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public ArticleViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item_list, viewGroup, false);
return new ArticleViewHolder(v);
}
#Override
public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position) {
ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle());
ArticleViewHolder.articleLink.setText(articles.get(position).getArticleLink());
ArticleViewHolder.articleLink.setText(articles.get(position).getArticleBody());
ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId());
}
#Override
public int getItemCount() {
return articles.size();
}
public static class ArticleViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView articleTitle;
TextView articleLink;
TextView articleBody;
ImageView articlePhotoID;
ArticleViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.card1_view);
articleTitle = (TextView) itemView.findViewById(R.id.news_title);
articleLink = (TextView) itemView.findViewById(R.id.news_link);
articleBody = (TextView) itemView.findViewById(R.id.news_body);
articlePhotoID = (ImageView) itemView.findViewById(R.id.news_photoId);
}
}
}
Looks like a typo error. Try to replace your NewsRVAdapter's onBindViewHolder with:
#Override
public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position) {
ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle());
ArticleViewHolder.articleLink.setText(articles.get(position).getArticleLink());
ArticleViewHolder.articleBody.setText(articles.get(position).getArticleBody());
ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId());
}
The reason is your wrong code in onBindViewHolder ^^
#Override
public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position) {
ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle());
ArticleViewHolder.**articleLink**.setText(articles.get(position).getArticleLink());
ArticleViewHolder.**articleLink**.setText(articles.get(position).getArticleBody());
ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId());
}
ArticleViewHolder.articleLink was assigned two times so its value was overriden.
Correct it and your code work fine :)
You have a mistake on onBindViewHolder. You were referencing to articleLink twice.
it should have been
#Override
public void onBindViewHolder(ArticleViewHolder ArticleViewHolder, int position){
ArticleViewHolder.articleTitle.setText(articles.get(position).getArticleTitle());
ArticleViewHolder.articleLink.setText(articles.get(position).getArticleLink());
ArticleViewHolder.articleBody.setText(articles.get(position).getArticleBody());
ArticleViewHolder.articlePhotoID.setImageResource(articles.get(position).getArticlePhotoId());
}

Fragment and Adapter in Android App Not Displaying Data

I'm really new in Android. I have this problem, I have an Activity (MainActivity) and there is a NavigationDrawer, this switches two fragments - ActivitiesFragment and ReportFragments.
The problem is with ActivitiesFragments, data is not displayed.
I have my adapter ready and my layouts ready and the fragment. When I debugg my app, it actually brings data, but is not shown. This is my code:
ActivitiesAdapter
public class ActivitiesAdapter extends ArrayAdapter<Activities>
{
Context mContext;
int mLayoutResourceId;
public ActivitiesAdapter(Context context, int layoutResourceId)
{
super(context, layoutResourceId);
mContext = context;
mLayoutResourceId = layoutResourceId;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
final Activities currentItem = getItem(position);
if (row == null)
{
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(mLayoutResourceId, parent, false);
}
row.setTag(currentItem);
final TextView tituloview = (TextView) row.findViewById(R.id.tituloAct);
tituloview.setText(currentItem.getTitle());
final TextView descrpview = (TextView) row.findViewById(R.id.descrAct);
descrpview.setText(currentItem.getDescription());
return row;
}
}
This is my fragment ActivitiesFragment
public class ActivitiesFragment extends Fragment
{
protected static final String TAG = "ActivitiesFragmment";
private MobileServiceClient mClient;
private MobileServiceTable<Activities> mActivitiesTable;
private ActivitiesAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.act_listfragment, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
try
{
mClient = new MobileServiceClient("https://site.azure-mobile.net/",
"APPLICATIONKEYAPPLICATIONKEY",
getActivity().getApplicationContext()).withFilter(new ProgressFilter());
mActivitiesTable = mClient.getTable(Activities.class);
}
catch (MalformedURLException e)
{
createAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
}
mAdapter = new ActivitiesAdapter(getActivity(), R.layout.act_itemlist);
ListView listViewToDo = (ListView) getView().findViewById(R.id.activities_fragment_list);
listViewToDo.setAdapter(mAdapter);
refreshItemsFromTable();
}
private void createAndShowDialog(Exception exception, String title)
{
createAndShowDialog(exception.toString(), title);
}
private void createAndShowDialog(String message, String title)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
builder.setMessage(message);
builder.setTitle(title);
builder.create().show();
}
private void refreshItemsFromTable()
{
mActivitiesTable.execute(new TableQueryCallback<Activities>()
{
public void onCompleted(List<Activities> result, int count, Exception exception, ServiceFilterResponse response) {
if (exception == null) {
mAdapter.clear();
for (Activities item : result) {
mAdapter.add(item);
Log.i(TAG, "Titulo: " + item.getTitle());
}
} else {
createAndShowDialog(exception, "Error");
}
}
});
}
}
I don't know whats wrong, but I guess this block is not working on the OnActivityCreated method:
mAdapter = new ActivitiesAdapter(getActivity(), R.layout.act_itemlist);
ListView listViewToDo = (ListView) getView().findViewById(R.id.activities_fragment_list);
listViewToDo.setAdapter(mAdapter);
refreshItemsFromTable();
And here are my Layouts:
This is my frgament's layout:
<LinearLayout 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:background="#e1e1e1"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTituloActs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/all_activities"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:textColor="#009ad2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView android:id="#+id/activities_fragment_list"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:layout_weight="1"
tools:listitem="#layout/act_itemlist"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No hay datos"/>
</LinearLayout>
This is the layout act_itemlist. The row layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="8dp"
android:gravity="center_vertical"
android:src="#drawable/done"
android:layout_gravity="center_vertical"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTituloAct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:textSize="16sp"/>
<TextView
android:id="#+id/tvDescrAct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="Description de la actividad" />
</LinearLayout>
</LinearLayout>
I really need help with this! Please if someone can see something that I'm missing please tell me!
Thanks!!!!
Try to swap these lines:
mAdapter = new ActivitiesAdapter(getActivity(), R.layout.act_itemlist);
ListView listViewToDo = (ListView) getView().findViewById(R.id.activities_fragment_list);
refreshItemsFromTable(); // populate first before attaching your adapter
listViewToDo.setAdapter(mAdapter);
I finally solved my problem. Everything was Ok, the only detail was the layout. It was on the fragments layout.
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No hay datos"/>
This textview did not let to show the data so I deleted it and worked.
Thankk you for your help #LazyNinja

Categories

Resources