Hi, I am unable to click on Feed Item. Please help me
Refer the code:
============
MainActivity
============
===========
FeedAdapter
===========
===========
DetailsPage:
===========
Refer the code:
============
MainActivity
============
Code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ListView listApps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listApps = (ListView)findViewById(R.id.xmlListView);
Log.d(TAG, "onCreate: starting Asynctask");
DownloadData downloadData = new DownloadData();
downloadData.execute("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml");
Log.d(TAG, "onCreate: Done");
}
private class DownloadData extends AsyncTask<String, Void, String>
{
private static final String TAG = "DownloadData";
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d(TAG, "onPostExecute: parameter is " +s);
ParseApplications parseApplications = new ParseApplications();
parseApplications.parse(s);
FeedAdapter feedAdapter = new FeedAdapter(MainActivity.this, R.layout.list_record,parseApplications.getApplications());
listApps.setAdapter(feedAdapter);
}
@Override
protected String doInBackground(String... strings) {
Log.d(TAG, "doInBackground: starts with " + strings[0]);
String rssFeed = downloadXML(strings[0]);
if(rssFeed == null)
{
Log.e(TAG, "doInBackground: Error downloading" );
}
return rssFeed;
}
private String downloadXML(String urlPath)
{
StringBuilder xmlResult = new StringBuilder();
try
{
URL url = new URL(urlPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int response = connection.getResponseCode();
Log.d(TAG, "downloadXML: The response code was " + response);
//InputStream inoutStream = connection.getInputStream();
//InputStreamReader inputStreamReader = new InputStreamReader(inoutStream);
//BufferedReader reader = new BufferedReader(inputStreamReader);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
int charsRead;
char[] inputBuffer = new char[500];
while(true)
{
charsRead = reader.read(inputBuffer);
if(charsRead < 0)
{
break;
}
if(charsRead > 0)
{
xmlResult.append(String.copyValueOf(inputBuffer,0,charsRead));
}
}
reader.close();
return xmlResult.toString();
}catch (MalformedURLException e)
{
Log.e(TAG, "downloadXML: Invalid URL" +e.getMessage());
e.printStackTrace();
}catch (IOException e)
{
Log.e(TAG, "downloadXML: IO Exception reading data" + e.getMessage());
e.printStackTrace();
}catch (SecurityException e)
{
Log.e(TAG, "downloadXML: Security Exception: Need Permissions ?" + e.getMessage());
e.printStackTrace();
}
return null;
}
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Are you sure you want to exit ?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
===========
FeedAdapter
===========
Code:
public class FeedAdapter extends ArrayAdapter {
private static final String TAG = "FeedAdapter";
private final int layoutResource;
private final LayoutInflater layoutInflater;
private List<FeedEntry> applications;
public FeedAdapter(@NonNull Context context, @LayoutRes int resource, List<FeedEntry> applications) {
super(context,resource);
this.layoutResource = resource;
this.layoutInflater = LayoutInflater.from(context);
this.applications = applications;
}
@Override
public int getCount() {
return applications.size();
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null)
{
convertView = layoutInflater.inflate(layoutResource,parent,false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else
{
viewHolder = (ViewHolder) convertView.getTag();
}
//View view = layoutInflater.inflate(layoutResource,parent,false);
TextView idTitle = (TextView) convertView.findViewById(R.id.idTitle);
TextView idStartdate = (TextView) convertView.findViewById(R.id.idStartdate);
TextView idDescription = (TextView) convertView.findViewById(R.id.idDescription);
FeedEntry currentApp = applications.get(position);
viewHolder.idTitle.setText(currentApp.getTitle());
viewHolder.idStartdate.setText(currentApp.getStartdate());
viewHolder.idDescription.setText(currentApp.getDescription());
viewHolder.idDescription.setText(Html.fromHtml(currentApp.getDescription()));
return convertView;
}
private class ViewHolder
{
final TextView idTitle;
final TextView idStartdate;
final TextView idDescription;
ViewHolder (View v)
{
this.idTitle = (TextView) v.findViewById(R.id.idTitle);
this.idStartdate = (TextView) v.findViewById(R.id.idStartdate);
this.idDescription = (TextView) v.findViewById(R.id.idDescription);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicking on Event");
Intent intent = new Intent(this,DetailsPage.class);
intent.putExtra("webURL",FeedEntry.getLink());
intent.startActivity(intent);
}
});
}
}
}
DetailsPage:
===========
Code:
public class DetailsPage extends AppCompatActivity {
private android.webkit.WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_page);
webView = (android.webkit.WebView) findViewById(R.id.webview);
//webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(getIntent().getStringExtra("webURL"));
}
private class MyWebViewClient extends WebViewClient {
[USER=1021285]@override[/USER]
public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}