Row selection ListView Android - java

my question is how can i do something when the user touches a row on the ListView
My app loads a json file and parses it using the Volley Library then everything is loaded nicely on a custom list row
But when I hit a row it does nothing
Really annoying thing ...
Im using a custom view and it has been impossible to assign the OnListItemClick
Here is my code
//all necessary libraries here
public class InicioPasajero extends Activity {
private static final String TAG = InicioPasajero.class.getSimpleName();
// Movies json url
private static final String url = "URL_RETURNING_JSON";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
ImageButton b_ajustes;
ImageButton b_filtros;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicio_pasajero);
b_ajustes= (ImageButton) findViewById(R.id.Bajustes);
b_filtros= (ImageButton) findViewById(R.id.Bfiltros);
b_ajustes.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent a=new Intent(InicioPasajero.this, MiPerfil.class);
startActivity(a);
}
});
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Cargando...");
pDialog.show();
// changing action bar color
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("n"));
movie.setThumbnailUrl(obj.getString("i"));
movie.setRating(obj.getString("r"));
movie.setYear(obj.getString("h"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("g");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
protected void onListItemClick(ListView movieList, View view, int posicion, long id) {
Log.i("Sel:","si");
// Hacer algo cuando un elemento de la lista es seleccionado
TextView textoTitulo = (TextView) view.findViewById(R.id.title);
CharSequence texto = "Seleccionado: " + textoTitulo.getText();
Toast.makeText(getApplicationContext(), texto, Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
/*
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}*/
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// no hacemos nada.
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Thanks in advance.

I don't see you calling setOnItemClickListener on your ListView anywhere? It also looks like you meant to have the Activity implement AdapterView.OnItemClickListener - then the overridden method named onItemClick would get called when a list item is clicked.

Related

How Can I Update data in Recyclerview?

I Build an app that load data with Retrofit, into a recyclerview, my recyclerview load perfectly all data from JSON file, but when I try of an update with swipeRefreshLayout, and I intent load again the method loadFirstPage();, but simply add again the same data.
I search in google for any solution, but all my intents do not work in my code.
I intent used adapter.clear and load again, but not work fine.
The idea is if 1 item change from JSON file, update again the data in Recyclerview.
public class historial extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
User user = SharedPrefManager.getInstance(this).getUser();
private static final String TAG = "MainActivity";
PaginationAdapter adapter;
LinearLayoutManager linearLayoutManager;
private SwipeRefreshLayout swipeRefreshLayout;
RecyclerView recyclerView;
ProgressBar progressBar;
private static final int PAGE_START = 1;
private boolean isLoading = false;
private boolean isLastPage = false;
// limiting to 5 for this tutorial, since total pages in actual API is very large. Feel free to modify.
private int TOTAL_PAGES = 5;
private int currentPage = PAGE_START;
private NetworkInterface geosInterface;
int position;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.historial);
Toolbar toolbar = findViewById(R.id.toolbar);
//setting the title
toolbar.setTitle("elGEos School - Historial");
//placing toolbar in place of actionbar
setSupportActionBar(toolbar);
progressBar = findViewById(R.id.progressBar);
recyclerView = findViewById(R.id.recycler_view);
adapter = new PaginationAdapter(this);
swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(historial.this);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new PaginationScrollListener(linearLayoutManager) {
#Override
protected void loadMoreItems() {
isLoading = true;
currentPage += 1;
// mocking network delay for API call
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
loadNextPage();
}
}, 1000);
}
#Override
public int getTotalPageCount() {
return TOTAL_PAGES;
}
#Override
public boolean isLastPage() {
return isLastPage;
}
#Override
public boolean isLoading() {
return isLoading;
}
});
//init serecyclerViewice and load data
geosInterface = NetworkApi.getClient().create(NetworkInterface.class);
loadFirstPage();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
//CODE FOR UPDATE HERE ???
if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
swipeRefreshLayout.setRefreshing(false); // This hides the spinner
}
}
});
}
//TOOL BAR Y MENU
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu3, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.aboutback:
onBackPressed();
//Toast.makeText(this, "You clicked about", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
private void loadFirstPage() {
swipeRefreshLayout.setRefreshing(true);
Log.d(TAG, "loadFirstPage: ");
callTopMessage().enqueue(new Callback<TopMessage>() {
#Override
public void onResponse(Call<TopMessage> call, Response<TopMessage> response) {
// Got data. Send it to adapter
List<Result> results = fetchResults(response);
progressBar.setVisibility(View.GONE);
adapter.addAll(results);
if (currentPage <= TOTAL_PAGES) adapter.addLoadingFooter();
else isLastPage = true;
swipeRefreshLayout.setRefreshing(false);
}
#Override
public void onFailure(Call<TopMessage> call, Throwable t) {
t.printStackTrace();
// TODO: 08/11/16 handle failure
swipeRefreshLayout.setRefreshing(false);
}
});
}
/**
* #param response extracts List<{#link Result>} from response
* #return
*/
private List<Result> fetchResults(Response<TopMessage> response) {
TopMessage topMessage = response.body();
return topMessage.getResults();
}
private void loadNextPage() {
swipeRefreshLayout.setRefreshing(true);
Log.d(TAG, "loadNextPage: " + currentPage);
callTopMessage().enqueue(new Callback<TopMessage>() {
#Override
public void onResponse(Call<TopMessage> call, Response<TopMessage> response) {
adapter.removeLoadingFooter();
isLoading = false;
List<Result> results = fetchResults(response);
adapter.addAll(results);
if (currentPage != TOTAL_PAGES) adapter.addLoadingFooter();
else isLastPage = true;
swipeRefreshLayout.setRefreshing(false);
}
#Override
public void onFailure(Call<TopMessage> call, Throwable t) {
t.printStackTrace();
// TODO: 08/11/16 handle failure
swipeRefreshLayout.setRefreshing(false);
}
});
}
private Call<TopMessage> callTopMessage() {
return geosInterface.getTopMessage(
"Comfama",
"B",
"TTY651",
"1",
currentPage
);
}
#Override
public void onRefresh() {
}
}
Try this:
it is very simple to update and remove data in recycler view
REMOVE Data : There are 4 steps to remove an item from a RecyclerView adapter class like this:
list.remove(position);
recycler.removeViewAt(position);
adapter.notifyItemRemoved(position);
mAdapter.notifyItemRangeChanged(position, list.size());
make sure your data is set in adapter class like this after that you notifyData
private void setAdapter() {
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recycler_view.setLayoutManager(mLayoutManager);
mAdapter = new BankDetailsAdapter(getBankList,this);
recycler_view.setAdapter(mAdapter);
}
UPDATE data: There are 1 steps to update data like this:
adapter.notifyDataSetChanged();
it helps you

Mimic the back button on the actionbar

So I have the action bar back button which returns me from an activity to my main activity. The problem I have is that it calls loadData() (which loads data from an API) when you press the action bar back button.
If I press the back button on the device (the button beside the home button) then I will be brought back to the previous view (the mainactivity) and won't have to call the API again.
So I'm trying to find a way to mimic the physical back button as an action bar widget.
I don't want MainActivity's code to be called again as it will execute another API call (I can only have 5 per minute) and it is also slower. I just want it to go back to the view I was just at.
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private ArrayList<ListItem> listItems;
private String defaultQuery = "ham";
private String builtURL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<>();
loadData(defaultQuery);
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
listItems.clear();
loadData(query);
(menu.findItem(R.id.action_search)).collapseActionView();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
searchView.setIconified(false);
return true;
}
public void loadData(String query) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading recipes...");
progressDialog.show();
Log.d("q", "loadData: " + query);
builtURL = buildURL(query);
StringRequest request = new StringRequest(Request.Method.GET,
builtURL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
if(response == null) {
response = "THERE WAS AN ERROR";
}
try {
JSONObject obj = new JSONObject(response);
JSONArray hits = obj.getJSONArray("hits");
for (int i = 0; i < hits.length(); i++) {
JSONObject a = hits.getJSONObject(i);
JSONObject recipe = a.getJSONObject("recipe");
String ingredients = recipe.getString("ingredientLines");
ingredients = ingredients.replace("[", "");
ingredients = ingredients.replace("]", "");
ingredients = ingredients.replace("\"", "");
ingredients = ingredients.replace("\\", "");
ingredients = ingredients.replace(",", "\n");
ListItem item = new ListItem(
recipe.getString("label"),
recipe.getString("source"),
recipe.getString("image"),
ingredients,
recipe.getString("url")
);
listItems.add(item);
}
adapter = new Adapter(listItems, getApplicationContext(), builtURL);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue rq = Volley.newRequestQueue(this);
rq.add(request);
}
private String buildURL(String query) {
Log.d("q", "buildURL: " + query);
Uri.Builder builder = new Uri.Builder();
//url built here but I removed it because it shows API key etc.
String urlToSend = builder.build().toString();
//debugging purposes to show the url created
Log.d("url", "doInBackground: " + urlToSend);
return urlToSend;
}
#Override
public boolean onNavigateUp(){
finish();
return true;
}
This is the activity that I'm coming from.
public class recipe_view extends AppCompatActivity {
ImageView ivRecipeImage;
TextView tvRecipeName;
TextView tvRecipeCreator;
TextView tvRecipeIngredients;
String url;
Integer pos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_view);
Bundle data = getIntent().getExtras();
ArrayList<ListItem> list = data.getParcelableArrayList("list");
pos = data.getInt("pos");
for (int i = 0; i < list.size() ; i++) {
System.out.println(list.get(i));
System.out.println(pos);
}
ivRecipeImage = findViewById(R.id.recipeImage);
tvRecipeName = findViewById(R.id.recipeName);
tvRecipeCreator = findViewById(R.id.recipeCreator);
tvRecipeIngredients = findViewById(R.id.ingredients);
tvRecipeName.setText(list.get(pos).getTitle());
tvRecipeCreator.setText(list.get(pos).getAuthor());
Picasso.with(getApplicationContext())
.load(list.get(pos).getImageUrl())
.centerCrop()
.fit()
.into(ivRecipeImage);
tvRecipeIngredients.setText(list.get(pos).getListOfIngredients());
url = list.get(pos).getRecipeUrl();
final Button button = findViewById(R.id.bViewInstructions);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent goToBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(goToBrowser);
}
});
}
}
In your manifest xml
set
<activity
android:name=".your_activity"
android:label="#string/title_activity_sign_up"
android:parentActivityName=".whateveractivity"
android:screenOrientation="portrait" >
</activity>
then in code
#Override
public void onBackPressed() {
NavUtils.navigateUpFromSameTask(this);
// Otherwise defer to system default behavior.
super.onBackPressed();
}
To elaborate what you are doing is overriding the onbackpressed hardware button.
Also make your parent activity's launchmode singleInstance.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent goToBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(goToBrowser);
finish();
}
});

Multiple Service calling by using Volley

I have a MainActivity where the Swipeable Tabs are created and from there two fragments are called. I have 2 webservice for Fragment A and Fragment B where I have to parse data coming from server. I am using volley. When I am in the first fragment; both the webservice are being called and the data are not loaded for the first time.
But in the Second time it is showing correctly but it should be like this that when I am in Fragment A the service of the Fragment A should be called and when I am in Fragment B the service of the Fragment B should be called. I am attaching the code snippets.
MainActivity.java
public class MainActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
int status = 0 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*Setup the DrawerLayout and NavigationView
*/
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
// Show menu icon
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
/**
* Lets inflate the very first fragment
* Here , we are inflating the NewsFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView, new NewsFragment()).commit();
// mNavigationView.setBackgroundColor(Color.parseColor("#CFCFCF"));
/**
* Setup click events on the Navigation View Items.
*/
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
if (menuItem.getItemId() == R.id.nav_item_sent) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new SportsFragment()).commit();
status = 1;
if (status == 1){
}
}
if (menuItem.getItemId() == R.id.nav_item_inbox) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new NewsFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_sent){
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new VideosFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_draft) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new OpinionFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_sports) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new SportsFragment()).commit();
}
if (menuItem.getItemId() == R.id.nav_item_weather) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView, new NewsFragment()).commit();
}
return false;
}
});
/**
* Setup Drawer Toggle of the Toolbar
*/
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
#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) {
switch (item.getItemId()) {
case R.id.action_about_us:
Intent intent = new Intent(MainActivity.this, AboutUs.class);
startActivity(intent);
return true;
case R.id.action_terms_of_use:
Intent intent_two = new Intent(MainActivity.this, TermsUse.class);
startActivity(intent_two);
return true;
case R.id.action_privacy_policy:
Intent intent_three = new Intent(MainActivity.this, PrivacyPolicy.class);
startActivity(intent_three);
return true;
case R.id.action_contact_us:
Intent intent_four = new Intent(MainActivity.this, ContactUs.class);
startActivity(intent_four);
case R.id.search:
// hidetext();
Toast.makeText(MainActivity.this, "In the development Phase... Thank You...", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// private void hidetext() {
//
// Intent i = new Intent(MainActivity.this, SearchResultActivity.class);
// startActivity(i);
// }
}
TopNewsFragment
public class TopNewsFragment extends Fragment {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
private static final String url = "http://sikkimexpress.itstunner.com/api/homenewslist/topnews";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
Movie movie;
private ListView listView;
private CustomListAdapter adapter;
String imageURL = "", title = "", description = "";
public static final String KEY_ID = "news_id";
public static final String KEY_HEADURL = "news_url";
public static final String KEY_DETAILS = "news_details";
public static final String KEY_TITLE = "news_title";
RequestQueue requestQueue;
public TopNewsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_news, container, false);
listView = (ListView) rootView.findViewById(R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int Position,
long offset) {
// TODO Auto-generated method stub
Movie item = (Movie) adapter.getItem(Position);
Intent intent = new Intent(rootView.getContext(), DetailsPage.class);
intent.putExtra(KEY_ID, item.getNewsId());
intent.putExtra(KEY_HEADURL, item.getThumbnailUrl());
intent.putExtra(KEY_TITLE, item.getTitle());
intent.putExtra(KEY_DETAILS, item.getDescription());
startActivity(intent);
}
});
// requestQueue = Volley.newRequestQueue(getActivity());
adapter = new CustomListAdapter(getActivity(), movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...Please Wait...");
pDialog.setCancelable(false);
pDialog.show();
Volley.newRequestQueue(getActivity()).add(new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
hidePDialog();
try {
JSONArray jsonArray = response.getJSONArray("HomeNews");
// if (jsonArray.length() == 0) {
// new AlertDialog.Builder(getActivity())
// .setTitle("Alert")
// .setMessage("No Items found...")
// .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// // continue with delete
// }
// })
// .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// // do nothing
// }
// })
// .setIcon(android.R.drawable.ic_dialog_alert)
// .show();
// }
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject homenews = jsonArray.getJSONObject(i);
Movie movie = new Movie();
movie.setNewsId(homenews.getString("NewsId"));
movie.setDateTime(homenews.getString("DateTime"));
movie.setNewsType(homenews.getString("NewsType"));
movie.setTitle(homenews.getString("Title"));
title = movie.setTitle(homenews.getString("Title"));
description = movie.setDescription(homenews.getString("Description"));
movie.setDescription(homenews.getString("Description"));
imageURL = movie.setThumbnailUrl(homenews.getString("MainImageThumbnail"));
movie.setThumbnailUrl(homenews.getString("MainImageThumbnail"));
movieList.add(movie);
System.out.println("Setting up in ListView");
// System.out.println("Result:- " + newsId + " " + dateTime + " " + newsType + " " + title + " " + description + " " + mainImageURL);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// new AlertDialog.Builder(getActivity())
// .setTitle("No Connectivity ")
// .setMessage("Please check your internet connectivity!")
// .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// // continue with delete
// }
// })
// //.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
// //public void onClick(DialogInterface dialog, int which) {
// // do nothing
// //}
// //})
// .setIcon(android.R.drawable.ic_dialog_alert)
// .show();
hidePDialog();
}
}));
// AppController.getInstance().addToRequestQueue(jsonObjectRequest);
// requestQueue.add(jsonObjectRequest);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
LatestNewsFragment.java
public class LatestNewsFragment extends Fragment {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
private static final String url = "http://sikkimexpress.itstunner.com/api/homenewslist/latest";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
// contacts JSONArray
private JSONArray users = null;
RequestQueue requestQueue;
public static final String KEY_HEADURL="news_url";
public static final String KEY_DETAILS="news_details";
public static final String KEY_TITLE = "news_title";
public LatestNewsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_news, container, false);
listView = (ListView) rootView.findViewById(R.id.list);
// requestQueue = Volley.newRequestQueue(getActivity());
adapter = new CustomListAdapter(getActivity(), movieList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int Position,
long offset) {
// TODO Auto-generated method stub
Movie item = (Movie) adapter.getItem(Position);
Intent intent = new Intent(rootView.getContext(), DetailsPage.class);
// intent.putExtra("URL", movie.getThumbnailUrl());
// intent.putExtra("title", movie.getTitle());
// intent.putExtra("description", movie.getDescription());
intent.putExtra(KEY_HEADURL, item.getThumbnailUrl());
intent.putExtra(KEY_TITLE, item.getTitle());
intent.putExtra(KEY_DETAILS, item.getDescription());
startActivity(intent);
}
});
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...Please Wait...");
pDialog.setCancelable(false);
pDialog.show();
Volley.newRequestQueue(getActivity()).add(new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
// JsonObjectRequest jsonObjectRequest =
#Override
public void onResponse(JSONObject response) {
try {
hidePDialog();
JSONArray jsonArray = response.getJSONArray("HomeNews");
// if (jsonArray.length() == 0){
// new AlertDialog.Builder(getActivity())
// .setTitle("Alert")
// .setMessage("No Items found...")
// .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// // continue with delete
// }
// })
// .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// // do nothing
// }
// })
// .setIcon(android.R.drawable.ic_dialog_alert)
// .show();
// }
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject homenews = jsonArray.getJSONObject(i);
Movie movie = new Movie();
movie.setNewsId(homenews.getString("NewsId"));
movie.setDateTime(homenews.getString("DateTime"));
movie.setNewsType(homenews.getString("NewsType"));
movie.setTitle(homenews.getString("Title"));
movie.setDescription(homenews.getString("Description"));
movie.setThumbnailUrl(homenews.getString("MainImageThumbnail"));
movieList.add(movie);
System.out.println("Setting up in ListView");
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// new AlertDialog.Builder(getActivity())
// .setTitle("No Connectivity ")
// .setMessage("Please check your internet connectivity!")
// .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// // continue with delete
// }
// })
// //.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
// //public void onClick(DialogInterface dialog, int which) {
// // do nothing
// //}
// //})
// .setIcon(android.R.drawable.ic_dialog_alert)
// .show();
hidePDialog();
}
}));
// AppController.getInstance().addToRequestQueue(jsonObjectRequest);
// requestQueue.add(jsonObjectRequest);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
What probably happens is that the two fragments are both loaded even though you are only looking at one fragment at a time. Instead of creating a new RequestQueue on every request you should only have one. For example create an Application class like so:
public class MyApp extends Application {
private RequestQueue mRequestQueue;
private static MyApp mInstance;
#Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized MyApp getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
}
Don't forget to add your application class to your manifest inside the <activity> tag:
<application
android:name=".MyApp"
Now you can put requests on that queue from your fragments:
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,
URL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//Do something with response
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Do something with error
}
});
//Put the actual request on the queue
MyApp.getInstance().addToRequestQueue(req);

Populating JSON listview in a fragment with volley

I am a green horn in java/android programming but I was trying to add different tutorials to create a customized application that would be a cool experiment, this could be something very easy to most of the people here but am stuck in this one and I am trying to use this Navigation Drawer View Pager trying to populate the tab one fragment with this Custom ListView with Volley.from android hive "great tutorials btw".
I want to transfer the code in the MainActivity.java of custom listview with volley to a HomeFragment.java in navigation drawer but I get errors.
Main Activity
public class MainActivity extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://api.androidhive.info/json/movies.json";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String> ();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
#Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
HomeFragment
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
Combined Code
public class HomeFragment extends Fragment{
private static final String TAG = HomeFragment.class.getSimpleName();
// Movies json url
private static final String url = "http://api.androidhive.info/json/movies.json";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
//private TextView txtFragmentone;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public static HomeFragment newInstance() {
HomeFragment fragment = new HomeFragment();
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
//txtFragmentone = (TextView) rootView.findViewById(R.id.txtFragmentOne);
//txtFragmentone.setText(R.string.fragment_tab_one);
rootView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT ));
listView = (ListView) getActivity().findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getActivity().getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#4cbaff")));
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
return rootView;
}
#Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
However I an error at
rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT ));
AppController.java
package androidhive.info.materialdesign.app;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import androidhive.info.materialdesign.util.LruBitmapCache;
import android.app.Application;
import android.text.TextUtils;
/**
* #author fanjavaid
*
*/
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
#Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
fragment_home.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:orientation="vertical"
tools:context="androidhive.info.materialdesign.activity.HomeFragment">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_row_selector" />
</RelativeLayout>
However if i change LayoutParams to Linear.LayoutParams it crashes
If you could help it would be awesome !!!
I am also trying to achieve the same.
I combined the login and the navigation drawer.
I also tried to display in home fragment a static data and it worked .
For debugging purpose i changed the default fragment to the 2nd fragment.
So now when i open the drawer, and click on the home tab, I again go back to the login screen.

Open New Activity by id on Listview Item Click

My ListView is opening and everything is ok. I don´t know how to pass params from onPostExecute() to onItemClick() to open a new activity (SingleItem.java) by id.
Nothing that I´ve tried has worked.
ListItems.java
public class ListItems extends Activity {
private ListView listV;
TextView estado, cidade, noItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_items);
listV = (ListView) findViewById(R.id.listV);
estado = (TextView) findViewById(R.id.Estado);
cidade = (TextView) findViewById(R.id.Cidade);
noItem = (TextView) findViewById(R.id.noItem);
estado.setText(getIntent().getExtras().getString("state"));
cidade.setText(getIntent().getExtras().getString("city"));
Task task = new Task();
task.execute();
listV.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleItem.class);
startActivity(intent);
}
});
}
public class Task extends AsyncTask<String, String, Void>{
private ProgressDialog progressDialog = new ProgressDialog(ListItems.this);
InputStream is = null;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Listing Items...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
Task.this.cancel(true);
}
});
};
#Override
protected Void doInBackground(String... params) {
String url = "http://myip/webviews/jsonlistItems.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error connecting to database " + e.toString());
Toast.makeText(ListItems.this, "Try again.", Toast.LENGTH_LONG).show();
}
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line = br.readLine()) != null){
sb.append(line+"\n");
}
is.close();
result = sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}
protected void onPostExecute(Void v){
try {
JSONArray Jarray = new JSONArray(result);
for (int i = 0; i < Jarray.length(); i++) {
JSONObject jsonObject = null;
jsonObject = Jarray.getJSONObject(i);
// output
String item_id = jsonObject.getString("item_id");
String item_name = jsonObject.getString("item_name");
String item_color = jsonObject.getString("item_color");
String city = jsonObject.getString("city");
String statee = jsonObject.getString("state");
if(estado.getText().toString().equalsIgnoreCase(statee) &&
cidade.getText().toString().equalsIgnoreCase(city)){
String[] values = new String[] {item_name, item_color};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListItems.this, android.R.layout.simple_list_item_1, values);
listV.setAdapter(adapter);
break;
}
else{
noItem.setText("No Item to show");
}
}
this.progressDialog.dismiss();
} catch (Exception e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
public class ItemById{
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_events, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
SingleItem.java
public class SingleItem extends Activity {
TextView item_name, item_color;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singleitem);
item_name = (TextView) findViewById(R.id.item_name);
item_color = (TextView) findViewById(R.id.item_color);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.event, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
How to pass params from onPostExecute() to onItemClick() to open a new activity (SingleItem.java) by id?
It all depends what your SingleItem Activity is supposed to show when it's opened.
If you only need the name of the selected item then you simply retrieve the item name in the onItemClick method and pass it as parameter to SingleItem:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleItem.class);
String name = adapter.getItem(position);
intent.putExtra("yourItem", name);
startActivity(intent);
}
In order for this to work the adapter needs to be a variable in the Activity:
public class ListItems extends Activity {
private ArrayAdapter<String> adapter;
and this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListItems.this, android.R.layout.simple_list_item_1, values);
becomes this:
adapter = new ArrayAdapter<String>(ListItems.this, android.R.layout.simple_list_item_1, values);
If you need more information in the SingleItem Activity than just the name of the item you'd have to create an Item class to hold that information:
public static class Item implements Serializable {
String mName;
String mColor;
// more data
#Override
public String toString() {
return mName;
}
}
your onItemClick becomes:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleItem.class);
Item item = adapter.getItem(position);
intent.putExtra("yourItem", name);
startActivity(intent);
}
Your adapter would be:
ArrayAdapter<Item> adapter;
adapter = new ArrayAdapter<Item>(ListItems.this, android.R.layout.simple_list_item_1, itemArray);
Of course you'd need to create the itemArray when parsing the json stream.
The activity could easily read the selected item from the intent like so:
getIntent().getSerializableExtra("yourItem");
The adapter looks like it's populated only by each attribute of your JSON object (one row for item_name, one row for item_color). If this is what you want the naturally you won't get item_id because it's not there.
If you want each row to correspond to each of your JSON objects then you should modify your adapter.
First make your own class like so
class Wrap{
String itemId, itemName, city, statee;
}
And then create your own Adapter class that extends ArrayAdapter<Wrap>.
This way every time a row is clicked, you can get the Wrap object which contains everything, including id. Then you can pass these values to your next Activity.

Categories

Resources