I am new to android. I am trying to create a GridView
consisting of ImageView. The images are from flickr and I am using flickr API to search flickr and receive JSON response. I parse the JSON data to get image URL. The API call and parsing JSON data is done inside FlickrAdapter which extends BaseAdapter. But the problem is my getView() method is never called. I searched stackoverflow for this and I found that if there is no data to show then getView() will not be called. But I am setting the data in FlickrAdapter constructor. I've set some debug texts in the methods and I see that getCount() is called before the data is set which means getCount() is called before the FlickrAdapter constructor finishes executing. So getView() is never called.
I want getCount() method to be called after the constructor method of my FlickrAdapter finishes executing.
How can I solve this problem?
Here is my code for the BaseAdapter and other classes
FlickrAdapter.java
public class FlickrAdapter extends BaseAdapter {
public ArrayList<FlickrImage> flickrImagesList;
private Context context;
public static FlickrAdapter instance;
FlickrAdapter(final Context context, String searchText, Activity activity) {
this.flickrImagesList = new ArrayList<FlickrImage>();
this.context = context;
instance = this;
Flickr flickr = new Flickr(searchText, context, activity);
//row[0] = inflater.inflate(R.layout.template, null, false);
Log.d("URL", flickr.buildApiUrl());
DownloadManager.getJsonResponse(flickr.buildApiUrl(), context, new VolleyCallBack<JSONObject>() {
#Override
public void onSuccess(JSONObject success) {
//Log.d("JSON",success.toString());
try {
success = success.getJSONObject("photos");
JSONArray photo = success.getJSONArray("photo");
for (int i = 0; i < photo.length(); i++) {
JSONObject tempObject = photo.getJSONObject(i);
String result = new StringBuilder("https://farm" + tempObject.get("farm").toString() +
".staticflickr.com/" +
tempObject.get("server").toString() +
"/" +
tempObject.get("id").toString() +
"_" +
tempObject.get("secret").toString() +
"_n.jpg").toString();
FlickrImage image = new FlickrImage(i, result);
instance.flickrImagesList.add(image);
Log.d("SIZE", String.valueOf(instance.flickrImagesList.size()));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
#Override
public int getCount() {
Log.d("COUNT",String.valueOf(flickrImagesList.size()));
return flickrImagesList.size();
}
#Override
public Object getItem(int position) {
return flickrImagesList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
ImageView myImage;
ViewHolder(View v) {
myImage = (ImageView) v.findViewById(R.id.individulaImage);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.individual_image, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
FlickrImage tempImage = flickrImagesList.get(position);
Log.d("Address", tempImage.imageName);
//ImageView iv = (ImageView) convertView.findViewById(R.id.individulaImage);
//Picasso.with(context).load(tempImage.imageName).into(iv);
return row;
}}
Flickr.java
public class Flickr {
private StringBuilder urlBuilder = new StringBuilder("https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=e78808f6cf756218d6981fcdf8021f0c&tags=");
private String apiUrl = "";
private Context context = null;
private Activity activity;
private String[] individualImageUrl;
private String keyWord = "";
public Flickr(String keyWord, Context context, Activity activity) {
this.keyWord=keyWord;
this.apiUrl = buildApiUrl();
this.context = context;
this.activity = activity;
//Log.d("APIURL", this.apiUrl);
}
public String buildApiUrl() {
String[] temp;
StringBuilder sb = new StringBuilder();
temp = this.keyWord.split(" ");
for (int i = 0; i < temp.length; i++) {
sb.append(temp[i]);
if (i != (temp.length - 1)) {
sb.append("+");
}
}
urlBuilder.append(sb);
urlBuilder.append("&text=");
urlBuilder.append(sb);
urlBuilder.append("&format=json&nojsoncallback=1");
return urlBuilder.toString();
}}
FlickrImage.java
public class FlickrImage {
public int imageId;
public String imageName;
FlickrImage(int imageId, String imageURL) {
this.imageId = imageId;
this.imageName = imageURL;
}
}
VolleyCallBack.java
public interface VolleyCallBack<T> {
public void onSuccess(T success);
}
DownloadManager.java
public class DownloadManager {
public static void getJsonResponse(final String Url, Context context, final VolleyCallBack callBack) {
JsonObjectRequest flickrJsonRequest = new JsonObjectRequest(Request.Method.GET, Url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
callBack.onSuccess(response);
}
},
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
Volley.newRequestQueue(context).add(flickrJsonRequest);
}
}
It is not being called before your constructor completes. YOur web code runs in a seperate thread so your constructor code completes and then getCount is called. Once your web code is complete onSuccess is called.
It is never advisable to run network code in adapter. Run it in your activity or fragment and once it completes call notifyDatasetChanged() on your adapter.
Related
I have been following some tutorials to retrieve json data and add it to a listview using volley. I have been stuck in this error trying to solve it for some time, so i had to seek out some help. I tried many examples, this one seems the most intuitive to learn, but i just cant get passed this error, thank you
i cant solve this error
this is my model class
public class Trilhos {
private int id;
private String titulo;
private int id_user;
private String dificuldade;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public int getId_user() {
return id_user;
}
public void setId_user(int id_user) {
this.id_user = id_user;
}
public String getDificuldade() {
return dificuldade;
}
public void setDificuldade(String dificuldade) {
this.dificuldade = dificuldade;
}
}
my adapter
public class AdapterTrilhos extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Trilhos> triList;
public AdapterTrilhos(List<Trilhos> triList, Activity activity) {
this.activity = activity;
this.triList = triList;
}
#Override
public int getCount() {
return this.triList.size();
}
#Override
public Object getItem(int position) {
return this.triList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.dadoslista, null);
TextView idtxt = (TextView)
convertView.findViewById(R.id.txtid);
TextView tittxt = (TextView)
convertView.findViewById(R.id.txttit);
TextView usertxt = convertView.findViewById(R.id.txtuser);
TextView diftxt = (TextView)
convertView.findViewById(R.id.txtdif);
Trilhos t = triList.get(position);
idtxt.setText(t.getId());
tittxt.setText(t.getTitulo());
usertxt.setText(t.getId_user());
diftxt.setText(t.getDificuldade());
return convertView;
}
}
my main_activity (listarTrilhos)
public class ListarTrilhos extends Activity {
private static final String
urlget="http://192.168.1.68/loginsignup/getTrilhos.php";
private ListView listView;
private AdapterTrilhos adapter;
private List<Trilhos> TrilhosList = new ArrayList<Trilhos>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listatrilhos);
listView=findViewById(R.id.listv);
adapter=new AdapterTrilhos(this,TrilhosList);
listView.setAdapter(adapter);
criarLista();
}
public void criarLista(){
RequestQueue mRequestQueue =
Volley.newRequestQueue(getApplicationContext());
JsonArrayRequest request = new JsonArrayRequest(
urlget,
new Response.Listener<JSONArray>(){
#Override
public void onResponse(JSONArray response) {
int count = response.length();
for(int i = 0; i < count; i++){
try {
JSONObject jo = response.getJSONObject(i);
Trilhos tri=new Trilhos();
tri.setId(jo.getInt("id"));
tri.setTitulo(jo.getString("titulo"));
tri.setId_user(jo.getInt("id_user"));
tri.setDificuldade(jo.getString("dificuldade"));
TrilhosList.add(tri);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(getApplicationContext(), response.toString(),
Toast.LENGTH_LONG).show();
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "onErrorResponse",
Toast.LENGTH_LONG).show();
}
}
);
Object TAG_REQUEST_QUEUE = new Object();
request.setTag(TAG_REQUEST_QUEUE);
mRequestQueue.add(request);
mRequestQueue.start();
}
}
I didn't read all of your code but noticed that you have constructor
public AdapterTrilhos(List<Trilhos> triList, Activity activity) {
this.activity = activity;
this.triList = triList;}
change adapter=new AdapterTrilhos(this, TrilhosList); to
adapter=new AdapterTrilhos(TrilhosList, this);
I am using this LIB. It's working fine but I can not loading my data in adapter class. like.
If i add some static data that will be added but whenever i tries to load from server that will not be added. finally i am trying with this approach.
But when i calls the Brandinfo and try to add data that will be error saying colon missing etc. please tell me how i can i add data to adapter to complete this recyclerview.
Thanks.
My Fragment class is
List<BrandInfo> mContentItems = new ArrayList<BrandInfo>();
pbDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pbDialog.setMessage("Loading...");
pbDialog.show();
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d("BrandViewActivity", response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
brandInfo = new BrandInfo();
mContentItems.add(new BrandInfo().setBrandname(obj.getString("title")));
String title = obj.getString("title");
String location = obj.getString("location");
String image = obj.getString("image_path");
String category = obj.getString("category");
Log.d("fffffffffff", mContentItems.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error aa gai bhaya", "Error: " + error.getMessage());
hidePDialog();
}
});
This is my Brandinfo class:
public class BrandInfo {
private String brandname;
private String brandinfo;
private String address;
private String detail;
public String getBrandname() {
return brandname;
}
public void setBrandname(String brandname) {
this.brandname = brandname;
}
public String getBrandInfo() {
return brandinfo;
}
public void setBrandinfo(String brandinfo) {
this.brandname = brandinfo;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDetail() {
return detail;
}
public void setSex(String detail) {
this.detail = detail;
}
}
finally this is my adapter class:
public class TOAdapter extends RecyclerView.Adapter<TOAdapter.ViewHolder> {
private List<BrandInfo> brandLists = new ArrayList<BrandInfo>();
private ImageLoader imageLoader;
public TOAdapter(List<BrandInfo> brandLists) {
this.brandLists = brandLists;
}
// Create new views (invoked by the layout manager)
#Override
public TOAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_card_big, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
BrandInfo brandList = new BrandInfo();
imageLoader = AppController.getInstance().getImageLoader();
viewHolder.txtViewTitle.setText(brandList.getBrandname());
//viewHolder.thumbNail.setImageResource(itemsData[position].getImageUrl());
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
viewHolder.thumbNail.setImageUrl(brandList.getBrandname(), imageLoader);
}
#Override
public int getItemCount() {
String s = String.valueOf(brandLists.size());
Toast.makeText(AppController.getInstance(),s,Toast.LENGTH_LONG).show();
return brandLists.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public TextView txtViewTitle;
final NetworkImageView thumbNail;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.brandtitle);
thumbNail = (NetworkImageView) itemLayoutView
.findViewById(R.id.thumbnail);
}
}
}
So what I would recommend is the following (which I use in my own project):
Create the following method in you TOAdapter:
public void refreshRecyclerViewBrands(List<BrandInfo> brandLists) {
this.brandLists = brandLists;
notifyDataSetChanged();
}
Then call the following method from where you initialized the recyclerviewadapter and your list:
mAdapter.refreshRecyclerViewBrands(mContentItems);
This basically takes the list you just got from volley and tells the recyclerview to use this new list and refresh the recyclerview. This worked for me so hopefully will work for you.
Another thing I noticed is in your onBindViewHolder your are setting the following:
BrandInfo brandList = new BrandInfo();
Then you use:
viewHolder.txtViewTitle.setText(brandList.getBrandname());
But brandlist.getBrandname is null.
Never are you actually giving the brandlist object any values, rather use the following :
final BrandInfo brandList = brandLists.get(position);
Hope this helps.
you should do like this
JSONObject obj = response.getJSONObject(i);
brandInfo = new BrandInfo();
mContentItems.add(new
BrandInfo(obj.getString("title"),
obj.getString("title");
obj.getString("location");
obj.getString("image_path");
obj.getString("category");
then i add my adapter like this
CustomSourcingAdapter adapter=new
CustomSourcingAdapter(Sourcing_modes.this, publishPlacementList);
lists.setAdapter(adapter);
this is my custom Adapter class
public class CustomSourcingAdapter extends ArrayAdapter<SourcingModel> {
private final Activity context;
private final List<SourcingModel> publishPlacementList;
public CustomSourcingAdapter(Activity context,List<SourcingModel>
publishPlacementList) {
// TODO Auto-generated constructor stub
super(context, R.layout.sorsinglist,publishPlacementList);
// TODO Auto-generated constructor stub
this.context=context;
this.publishPlacementList=publishPlacementList;
}
static class ViewHolder {
protected TextView placementtitle;
protected TextView Noofposition;
protected ImageButton next;
protected TextView Department;
}
public View getView(int position,View converview,ViewGroup parent) {
ViewHolder viewHolder = null;
if (converview ==null ){
LayoutInflater inflater=context.getLayoutInflater();
converview=inflater.inflate(R.layout.sorsinglist, null);
viewHolder = new ViewHolder();
converview.setTag(viewHolder);
converview.setTag(R.id.idplacetitle,viewHolder.placementtitle);
converview.setTag(R.id.idnoofpos,viewHolder.Noofposition);
converview.setTag(R.id.imreqserch,viewHolder.next);
converview.setTag(R.id.iddepartment,viewHolder.Department);
}else{
viewHolder= (ViewHolder)converview.getTag();
}
viewHolder.placementtitle.setText(publishPlacementList.get(position).getPositionName());
viewHolder.Noofposition.setText(publishPlacementList.get(position).getNoOfPosition());
viewHolder.Department.setText(publishPlacementList.get(position).getName());
viewHolder.next.setTag(position);
viewHolder.next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int pos = (Integer) v.getTag();
long id = publishPlacementList.get(pos).getId();
Intent it = new Intent(context, SourcingSecond.class);
it.putExtra("id", id);
context.startActivity(it);
}
});
return converview;
};
May be this question many times.
i am getting some data from server and showing in listview . every thing working fine but i am getting problem to show image in list view.
Here is my example code
public class MainActivity extends ListActivity {
private static String url = null;
private static final String book_name = "b_name";
private static final String book_detail = "b_publisher";
private static final String book_image = "b_image";
ProgressDialog progressDialog;
ListView lv;
String cus_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
url = getResources().getString(R.string.url);
/*----Receiving data from Splash Activity-----*/
Bundle b = getIntent().getExtras();
cus_id = b.getString("custom_id");
new ProgressTask(MainActivity.this).execute();
}
class ProgressTask extends AsyncTask<String, Integer, Boolean> {
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
public ProgressTask(ListActivity activity) {
context = activity;
}
private Context context;
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Processing...");
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
#Override
protected void onProgressUpdate(Integer... values) {
// set the current progress of the progress dialog
progressDialog.setProgress(values[0]);
}
#Override
protected void onPostExecute(final Boolean success) {
progressDialog.dismiss();
}
protected Boolean doInBackground(final String... args) {
url = url + "?custom_iid=" + cus_id;
Log.d("Passing Url", url);
CustomListAdapter jParser = new CustomListAdapter();
JSONArray json = jParser.getJSONFromUrl(url);
if (json != null) {
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String b_image = c.getString("b_image");
String b_name = c.getString("b_name");
String b_detail = c.getString("b_publisher");
Log.d("detail", "" + b_image);
setBookImageUrl(b_image);
setBookName(b_name);
setBookDetail(b_detail);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return null;
}
public String getBookImageUrl() {
return book_image;
}
public CharSequence getBookName() {
return book_name;
}
public CharSequence getBookDetail() {
return book_detail;
}
public void setBookImageUrl(String imgeUrl) {
book_image = imgeUrl;
}
public void setBookName(String b_name) {
book_name = b_name;
}
public void setBookDetail(String b_detail) {
book_detail = b_detail;
}
}
BookListAdapter class:
public class BookListAdapter extends ArrayAdapter<MainActivity> {
private ArrayList<MainActivity> bookModels;
private Context context;
public BookListAdapter(Context context, int resource,
ArrayList<MainActivity> bookModels) {
super(context, resource, bookModels);
this.bookModels = bookModels;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row_list_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.bookIcon = (ImageView) rowView.findViewById(R.id.icon);
viewHolder.bookName = (TextView) rowView.findViewById(R.id.b_name);
viewHolder.bookDetail = (TextView) rowView
.findViewById(R.id.b_detail);
rowView.setTag(viewHolder);
}
final MainActivity bookModel = bookModels.get(position);
ViewHolder holder = (ViewHolder) rowView.getTag();
Picasso.with(context).load(bookModel.getBookImageUrl())
.into(holder.bookIcon);
holder.bookName.setText(bookModel.getBookName());
holder.bookDetail.setText(bookModel.getBookDetail());
return rowView;
}
#Override
public int getCount() {
return bookModels.size();
}
static class ViewHolder {
public ImageView bookIcon;
public TextView bookName;
public TextView bookDetail;
}
}
i can show book name and book detail in listview finely but image is not showing ..
i am getting value for book_image is http:\/\/X.X.X.X\/admin\/book_images\/232513pic9.png how to show in listview from that path..
I think you will need to implement your own adapter and use some library to display the image from URL.
My recommendation is Picasso
This is an example to implement your own adapter
BookListAdapter.java
public class BookListAdapter extends ArrayAdapter<BookModel> {
private ArrayList<BookModel> bookModels;
private Context context;
public BookListAdapter(Context context, int resource, ArrayList<BookModel> bookModels) {
super(context, resource, bookModels);
this.bookModels = bookModels;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.book_child_list, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.bookIcon = (ImageView) rowView
.findViewById(R.id.bookIcon);
viewHolder.bookName = (TextView) rowView
.findViewById(R.id.bookName);
viewHolder.bookDetail = (TextView) rowView
.findViewById(R.id.bookDetail);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
final BookModel bookModel = bookModels.get(position);
Picasso.with(context).load(bookModel.getBookImageUrl()).into(viewHolder.bookIcon);
viewHolder.bookName.setText(bookModel.getBookName());
viewHolder.bookDetail.setText(bookModel.getBookDetail());
return rowView;
}
#Override
public int getCount() {
return bookModels.size();
}
static class ViewHolder {
public ImageView bookIcon;
public TextView bookName;
public TextView bookDetail;
}
}
BookModel.java
public class BookModel {
private String bookName;
private String bookDetail;
private String bookImageUrl;
public BookModel() {
bookName = "";
bookDetail = "";
bookImageUrl = "";
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookDetail() {
return bookDetail;
}
public void setBookDetail(String bookDetail) {
this.bookDetail = bookDetail;
}
public String getBookImageUrl() {
return bookImageUrl;
}
public void setBookImageUrl(String bookImageUrl) {
this.icons = bookImageUrl;
}
}
Where BookModel class is a class where you can wrap your data (book name, book detail, book image) and pass it as a list to the adapter.
for example :
protected Boolean doInBackground(final String... args) {
url = url + "?custom_iid=" + cus_id;
Log.d("Passing Url", url);
CustomListAdapter jParser = new CustomListAdapter();
JSONArray json = jParser.getJSONFromUrl(url);
ArrayList<BookModel> bookModelList = new ArrayList<BookModel>();
if (json != null) {
for (int i = 0; i < json.length(); i++) {
try {
BookModel bookModel = new BookModel();
JSONObject c = json.getJSONObject(i);
String b_image = c.getString("b_image");
String b_name = c.getString("b_name");
String b_detail = c.getString("b_publisher");
Log.d("detail", "" + b_image);
bookModel.setBookName(b_name);
bookModel.setBookDetail(b_detail);
bookModel.setBookImageUrl(b_image);
bookModelList.add(bookModel);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
if(bookModelList.size()>0) {
BookListAdapter bookListAdapter = new BookListAdapter(MainActivity.this, R.id.yourlistview, bookModelList );
}
return null;
}
I hope my answer can help you!
lv.setAdapter(adapter);
use AQuery lib.
and just write this code in your custom adapter:
AQuery aQuery = new AQuery(context);
aQuery.id(your image id).image(your url,true,true);
I am new to Android Development.
I purely like to work with JSON Objects and Arrays for my simple application considering the lightness of the JSON Carrier compared to XMLs.
I had challenges with ArrayAdapter to populate the ListView.
This is how I overcome and need your suggestions on it.
Extend the Adaptor class.
Then pass the JSONArray to the constructor.
Here the constructor calls super with dummy String array setting the length of the JSONArray.
Store the constructor arguments in class for further use.
public myAdaptor(Context context, int resource, JSONArray array)
{
super(context, resource, new String[array.length()]);
// Store in the local varialbles to the adapter class.
this.context = context;
this.resource = resource;
this.profiles = objects;
}
The getView() will do the job of fetching JSONObjects from JSONArray to build view.
public View getView(int position, View convertView, ViewGroup parent)
{
View view;
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, parent, false);
}
else
{
view = convertView;
}
// Here
JSONObject item = (JSONObject) profiles.getJSONObject(position);
// READY WITH JSONObject from the JSONArray
// YOUR CODE TO BUILD VIEW OR ACCESS THE
}
Now Any improvements/suggestions/thoughful-question??
I would advise you to use google GSON instead JSON. It is a library that gives you a create objects from JSON-request, and you don't need to parse JSON everymore. Just create an object which contains all the fields from your JSON request and are named the same, and do with it whatever you want - for example:
Your JSON request
{
[
{
"id": "2663",
"title":"qwe"
},
{
"id": "1234",
"title":"asd"
},
{
"id": "5678",
"title":"zxc"
}
]
}
Your class - item of JSON-Array
public class MyArrayAdapterItem{
int id;
String title;
}
Somwhere in your code where you downloading data. I didn't know how are you doing it so i'll post my code for example:
mGparser = new JsonParser();
Gson mGson = new Gson();
Url url = "http://your_api.com"
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Connection", "close");
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
JsonArray request = (JsonArray) mGparser.parse(in.readLine());
in.close();
ArrayList<MyArrayAdapterItem> items = mGson.fromJson(request, new TypeToken<ArrayList<MyArrayAdapterItem>>() {}.getType());
So that's all, for now just put "items" instead JSON-array in your adapter's constructor
You can pass null to super instead of creating a string array and implement getCount method:
public myAdaptor(Context context, int resource, JSONArray array)
{
super(context, resource, null);
// Store in the local varialbles to the adapter class.
this.context = context;
this.resource = resource;
this.profiles = array;
}
public int getCount(){
return profiles.length();
}
create one textview and assign with item.getString("key") and add that string to the local string array and return that view
This is the listview adapter class.
public class Adapter extends BaseAdapter {
Context context = null;
ArrayList<OffersAvailable> offers = null;
public Adapter(Context context, ArrayList<OffersAvailable> offer) {
this.context = context;
this.offers = offer;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return offers.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return offers.get(position).getTitle();
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
final TextView tvpoints;
final TextView tv,tv_quantity;
if (convertView == null) {
LayoutInflater li = ((Activity) context).getLayoutInflater();
v = li.inflate(R.layout.design_userlist, null);
} else {
v = convertView;
}
tvpoints = (TextView) v.findViewById(R.id.tvpointlist);
tv_quantity= (TextView) v.findViewById(R.id.tv_quantity);
tv = (TextView) v.findViewById(R.id.tvdatalist);
((Activity) context).runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
tv.setText(offers.get(position).getTitle().toUpperCase());
tv_quantity.setText(offers.get(position).getQuatity().toUpperCase());
tvpoints.setText(offers.get(position).getPoint() + "");
}
});
return v;
}
}
Object class
public class OffersAvailable {
String title, point, quatity, description,nid;
public String getNid() {
return nid;
}
public void setNid(String nid) {
this.nid = nid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public String getQuatity() {
return quatity;
}
public void setQuatity(String quatity) {
this.quatity = quatity;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Use the json in the main class and store it inthe Arraylist of type OffersAvailable.
and pass it to the listviews adapter.
if you are getting the response from the internet use asynchttpclient method.And parse the json.
I am fairly new to Android development and I am trying to build a ListView which get data from web service using gson. I have a model class, a list class, an adapter class and the activity class.
The list works fine and it got the data, and now I want to integrate the OnItemClickListener to it and pass the data to the 2nd activity. And I'd like to get the item id (DistrictId) and pass it to the next Activity(listView) instead of the row id. It would be great if someone could show me the light... as the documentation is not as clear to understand and because I am new.
Below is my code.
The model class
package com.sample.myapp;
public class DistrictModel {
private String id;
private String districtName;
public String getDistrictId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDistrictName(){
return districtName;
}
public void setDistrictEN(String districtName){
this.districtName = districtName;
}
}
The List class
public class DistrictList {
private List<DistrictModel> districts;
public List<DistrictModel> getDistricts(){
return districts;
}
public void setDistrictList(List<DistrictModel> districts){
this.districts = districts;
}
}
The Adapter class
public class DistrictAdapter extends ArrayAdapter<DistrictModel>{
int resource;
String response;
Context context;
private LayoutInflater dInflater;
public DistrictAdapter(Context context, int resource, List<DistrictModel> objects) {
super(context, resource, objects);
this.resource = resource;
dInflater = LayoutInflater.from(context);
}
static class ViewHolder {
TextView title;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
//Get the current location object
DistrictModel lm = (DistrictModel) getItem(position);
//Inflate the view
if(convertView==null)
{
convertView = dInflater.inflate(R.layout.item_district, null);
holder = new ViewHolder();
holder.title = (TextView) convertView
.findViewById(R.id.district_name);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(lm.getDistrictName());
return convertView;
}
}
The activity class
public class DistrictListActivity extends Activity{
LocationManager lm;
ArrayList<DistrictModel> districtArray = null;
DistrictAdapter districtAdapter;
DistrictList list;
ListView lv;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.districtlist_layout);
lv = (ListView) findViewById(R.id.list_district);
districtArray = new ArrayList<DistrictModel>();
districtAdapter = new DistrictAdapter(DistrictListActivity.this, R.layout.item_district, districtArray);
lv.setTextFilterEnabled(true);
lv.setAdapter(districtAdapter);
try {
new DistrictSync().execute("http://aws.something.com/service");
} catch(Exception e) {}
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(DistrictListActivity.this);
adb.setTitle("LVSelectedItemExample");
adb.setMessage("Selected Item is = "+(lv.getItemIdAtPosition(position)));
adb.setPositiveButton("Ok", null);
adb.show();
}
}); **//i'd like to get the DistrictId from the json data.**
}
private class DistrictSync extends AsyncTask<String, Integer, DistrictList> {
protected DistrictList doInBackground(String... urls) {
DistrictList list = null;
int count = urls.length;
for (int i = 0; i < count; i++) {
try {
// ntar diganti service
RestClient client = new RestClient(urls[i]);
try {
client.Execute(RequestMethod.GET);
} catch (Exception e) {
e.printStackTrace();
}
String json = client.getResponse();
list = new Gson().fromJson(json, DistrictList.class);
//
} catch(Exception e) {}
}
return list;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(DistrictList dislist) {
for(DistrictModel lm : dislist.getDistricts())
{
districtArray.add(lm);
}
districtAdapter.notifyDataSetChanged();
}
}
}
For testing purpose, now I click the row it will show me the row id, so I know the onclick listener works, but I just want it to grab me the DistrictId so I can use it to pass to the next activity.
Thank you so much.
(out of my head) Try this:
((DistrictModel)lv.getAdapter().getItem(position)).getDistrictId();
Generally when you want to pass data from one Activity to another, you just place it into the Intent that you use to create the new Activity.
For example (and here are some additional examples):
Intent i = new Intent(context, MyNewActivity.class);
i.putExtra("MyCurrentHealth", mCurrentHealth);
context.startActivity(i);
To retrieve the data do this:
Bundle extras = getIntent().getExtras();
if (extra != null) {
... // Do stuff with extras
}