eddydn / AndroidRSSFeed

AndroidRSSFeed
8 stars 8 forks source link

What things change in order to parse JSON Array #1

Open mandy8055 opened 7 years ago

mandy8055 commented 7 years ago

Sir I am learning Json parsing through Android. I just want to know what needs to be changed in the code in order to parse JSON array:The link is http://starlord.hackerearth.com/newsjson

eddydn commented 7 years ago

My dear ! This is simple Json

You can delcare class

public class News{ public int ID { get; set; } public string TITLE { get; set; } public string URL { get; set; } public string PUBLISHER { get; set; } public string CATEGORY { get; set; } public string HOSTNAME { get; set; } public object TIMESTAMP { get; set; }}

And your Json just return array of News item :D ... you can convert it to News[]

2017-09-16 17:46 GMT+07:00 Manuj Sankrit notifications@github.com:

Sir I am learning Json parsing through Android. I just want to know what needs to be changed in the code in order to parse JSON array:The link is http://starlord.hackerearth.com/newsjson

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/eddydn/AndroidRSSFeed/issues/1, or mute the thread https://github.com/notifications/unsubscribe-auth/ALERyGtSNewMB4_rWdNI0SCvRHlos7mmks5si6bsgaJpZM4PZyFh .

mandy8055 commented 7 years ago

Sorry Sir But I am a tyro in json parsing So I am not able to make changes accordingly in FeedAdapter and MainActivity class. Although I have created this class...(I apologize Sir for this silly issue but I am Still learning).I have changed accordingly Sir but here this error occurs:

holder.txtTitle.setText(news.get(position).getTITLE());
 return news.size();

It is indicating that size() and get() method cannot be used.I have changed the variable as News[] news. So Sir how do I set the news position to The recyclerView?

eddydn commented 7 years ago

Show me your FeedAdapter

2017-09-16 19:49 GMT+07:00 Manuj Sankrit notifications@github.com:

Sorry Sir But I am a tyro in json parsing So I am not able to make changes accordingly in FeedAdapter and MainActivity class. Although I have created this class...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eddydn/AndroidRSSFeed/issues/1#issuecomment-329966522, or mute the thread https://github.com/notifications/unsubscribe-auth/ALERyNWSqVeak31UvE_9LgvKKkjfLfVhks5si8O-gaJpZM4PZyFh .

mandy8055 commented 7 years ago

Sir here is the FeedViewHolder and FeedAdapter class.

class FeedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener
{

public TextView txtTitle,txtPubDate,txtContent;
private ItemClickListener itemClickListener;

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

    txtTitle = (TextView)itemView.findViewById(R.id.txtTitle);
    txtPubDate = (TextView)itemView.findViewById(R.id.txtPubDate);
    txtContent = (TextView)itemView.findViewById(R.id.txtContent);

    //Set Event
    itemView.setOnClickListener(this);
    itemView.setOnLongClickListener(this);
}

public void setItemClickListener(ItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
}

@Override
public void onClick(View v) {

    itemClickListener.onClick(v,getAdapterPosition(),false);

}

@Override
public boolean onLongClick(View v) {
    itemClickListener.onClick(v,getAdapterPosition(),true);
    return true;
}
}

Here is the FeedAdapter class:

public class FeedAdapter extends RecyclerView.Adapter<FeedViewHolder> {

private Items[] rssObject;
//private List<Items> data = Collections.emptyList();
private Context mContext;
private LayoutInflater inflater;

public FeedAdapter(Items[] rssObject, Context mContext) {
    this.rssObject = rssObject;
    this.mContext = mContext;
    inflater = LayoutInflater.from(mContext);
}

@Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   View itemView = inflater.inflate(R.layout.row,parent,false);
    return new FeedViewHolder(itemView);
}

@Override
public void onBindViewHolder(FeedViewHolder holder, int position) {

    holder.txtTitle.setText(rssObject.get(position).getTITLE()); // ERROR HERE
    holder.txtPubDate.setText(rssObject.get(position).getPUBLISHER());//ERROR HERE
    holder.txtContent.setText(rssObject.get(position).getTIMESTAMP());//ERROR HERE

    holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {
            if(!isLongClick)
            {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(rssObject.get(position).getURL()));
                browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(browserIntent);
            }
        }
    });
}

@Override
public int getItemCount() {
    return rssObject.size();//ERROR HERE
}
}
eddydn commented 6 years ago

public class Items{ public int ID { get; set; } public string TITLE { get; set; } public string URL { get; set; } public string PUBLISHER { get; set; } public string CATEGORY { get; set; } public string HOSTNAME { get; set; } public object TIMESTAMP { get; set; }}

Declare your Item class like here

Show me code to create Adapter (at MainActivity)

2017-09-16 21:39 GMT+07:00 Manuj Sankrit notifications@github.com:

Sir here is the FeedViewHolder and FeedAdapter class.

class FeedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {

public TextView txtTitle,txtPubDate,txtContent; private ItemClickListener itemClickListener;

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

txtTitle = (TextView)itemView.findViewById(R.id.txtTitle);
txtPubDate = (TextView)itemView.findViewById(R.id.txtPubDate);
txtContent = (TextView)itemView.findViewById(R.id.txtContent);

//Set Event
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);

}

public void setItemClickListener(ItemClickListener itemClickListener) { this.itemClickListener = itemClickListener; }

@Override public void onClick(View v) {

itemClickListener.onClick(v,getAdapterPosition(),false);

}

@Override public boolean onLongClick(View v) { itemClickListener.onClick(v,getAdapterPosition(),true); return true; }

}

public class FeedAdapter extends RecyclerView.Adapter {

private Items[] rssObject; private List data = Collections.emptyList(); private Context mContext; private LayoutInflater inflater;

public FeedAdapter(Items[] rssObject, Context mContext) { this.rssObject = rssObject; this.mContext = mContext; inflater = LayoutInflater.from(mContext); }

@Override public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = inflater.inflate(R.layout.row,parent,false); return new FeedViewHolder(itemView); }

@Override public void onBindViewHolder(FeedViewHolder holder, int position) {

holder.txtTitle.setText(rssObject.get(position).getTITLE());
holder.txtPubDate.setText(rssObject.get(position).getPUBLISHER());
holder.txtContent.setText(rssObject.get(position).getTIMESTAMP());

holder.setItemClickListener(new ItemClickListener() {
    @Override
    public void onClick(View view, int position, boolean isLongClick) {
        if(!isLongClick)
        {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(rssObject.get(position).getURL()));
            browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(browserIntent);
        }
    }
});

}

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

}

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eddydn/AndroidRSSFeed/issues/1#issuecomment-329972394, or mute the thread https://github.com/notifications/unsubscribe-auth/ALERyMToGsi-FPYao8W5DJO1qsG2i4NFks5si92HgaJpZM4PZyFh .