putting a variable inside a button or image declaration - android

I am trying to do a data analysis And set image view visibility based on those data. Is that possible to put a variable inside a image view declaration so that I can use an array to analyse data and call the respective image view based on the variable for example
For(inti =0,i<20;i++)
Imageview(i).setvibility(gone)
This will prevent me from calling the image view over and over again .

You can solve this by creating a list of ImageViews.
List<ImageView> list = new ArrayList<>();
list.add((ImageView) findViewById(R.id.yourImageViewID);
list.add((ImageView) findViewById(R.id.anotherID);
for (int i=0;i<list.size();i++) {
ImageView view = list.get(i);
view.visible(false);
}
Now you can iterate through your views.

Related

How to set number of textview in horizontal scroll dynamically when data is coming from JSON parsing?

I am fetching the JSON data via URL and displaying it in text view inside the horizontal scroll view.
JSONArray ja = response.getJSONArray("results");
ArrayList<Details> myModelList = new ArrayList<Details>();
for (int i = 0; i < ja.length(); i++) {
JSONObject jsonObject = ja.getJSONObject(i);
mymodel = new Details();
mymodel.id = Integer.parseInt(jsonObject.optString("id").toString());
mymodel.url = jsonObject.getString("resLink");
mymodel.resType = jsonObject.getString("resType");
mymodel.name = jsonObject.getString("resName");
myModelList.add(mymodel);
setData(myModelList);
I am showing my data in text view but it shows the first data only
private void setData(List<Details> mList) {
for (int i =0; i <=mymodel.getResType().length();i++)
{
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(mymodel.getResType());
}
You are initializing the mymodel in the for loop again and again so I think it contains only one value that too the last one(not the first one).
If that is the problem, try declaring the following outside the for loop.
mymodel = new Details();
So there's quite a few problems in your code:
1) You are always replacing the text of the same TextView. Normally with scrolling views you back them up with an adapter, place your data into the list that is then added to the adapter, then rely on the scrolling list to dynamically create the TextView which you can populate. If you have a known number of views in your scroll view and you want to avoid adapters, make sure they all have unique IDs so that you could find them.
2) Second problem is you're calling your setData inside the loop. Finish the loop first, then call you method (although you won't really have to do this if you do the adapter thing)
3) Your setData method is all sorts of wrong. If you want to pass a list to a method then iterate through it, you do it like this:
for (Details d : list) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(d.getResType());
}
But like I said, that probably won't help you on its own. Look into documentation for the scroll lists or recycler view and adapters. There are many examples available around.

Displaying the same images in different lists with a view flipper

I have viewflipper which contains 3 childs, one GridView and two custom ListView, every view has different adapter , and every adapter has a image loader with Universal Image Loader library.
The items are the same for all adapters, my goal is to show content in different way (grid , list , and big list), but in this way every image loads 3 times. Is there any way to load images once and show them to their childs?
So you need an object common to the three views, which manages image loading and holds memory. You have it: it's the Adapter. Use a single one and just switch layouts.
For instance, you could define this method inside the adapter:
int layoutResId;
public void changeLayout(int layoutResId) {
this.layoutResId = layoutResId;
notifyDataSetChanged(); //force the adapter to call getView() again
}
Then in your getView() method you just inflate the layout defined by layoutResId.
Well the thing that you seem to be asking is if you can pass the images along to the other adapters. It would be something like if image is not empty them use it in the other views, if not then use the image loader.
public class Constants {
public static Constants INSTANCE = new Constants();
public Constants() {
}
public Uri IMAGE_PATH = Uri.EMPTY;
public File IMAGE_FILE = null;
public Bitmap IMAGE = null;
}
Presumably you would choose only one of these kinds to save from your view. And then check them to see if constants possesses an image for you to display.

How to divide gridview in sections in android?

My question is basic but I couldn't find any answer for my question. I have a layout which contains 2 textviews and a gridview. I added this layout into a listview.I want to generate multiple layouts. For example;
ListView------------------1stElement
----------textview1-----------------
----------gridview1-----------------
----------textview1-----------------
ListView------------------2ndElement
----------textview2-----------------
----------gridview2-----------------
----------textview2-----------------
ListView------------------3rdElement
----------textview3-----------------
----------gridview3-----------------
----------textview3-----------------
I tried it like that;
for(int i = 0; i<category.getChildren().size(); i++){
listView.setAdapter(new CategoryListAdapter(this, category.getChildren().get(i)));
}
But of course, it showed my last view only. setAdapter() doesn't suit to my code. I need something like adding views over and over.
Thanks for your helps.
You don't have to do this in for loop, Adapter Takes whole list Once Only, If u do it in a loop it will set the last list visible, So make list in For loop first and then add i to adapter only once.
for(int i=0;i<size;i++)
{
ArrayList<Object> myChldrenList = new ArrayList<Object>();
Object mychild = mycategory.getChildren().get(i)
myChildrenList.add(myChild);
}
listView.setAdapter(new CategoryListAdapter(this, myChildrenList));
u can use Ur Class type Instead of object class or simply Type Cast Object Class.

how to display an image from the Internet in SimpleAdapter

I have SimpleAdapter in which I want to display an image from the Internet.
final String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE,ATTRIBUTE_NAME_DESCRIPTION };
final int[] to = { R.id.tvText, R.id.ivImg,R.id.textDescr };
sAdapter = new SimpleAdapter(getActivity(), data, R.layout.itemvideo, from, to);
sAdapter.notifyDataSetChanged();
setListAdapter(sAdapter);
I fill the data in a separate thread
for (Element titles : title) {
m = new HashMap<String, Object>();
m.put(VideoList.ATTRIBUTE_NAME_TEXT, titles.select("a[href]").text());
m.put(VideoList.ATTRIBUTE_NAME_IMAGE, Uri.parse(titles.select("img").attr("abs:src")));
m.put(VideoList.ATTRIBUTE_NAME_DESCRIPTION,titles.select("div[style]").first().text());
m.put("link",titles.select("a[href]").attr("abs:href"));
data.add(m);
}
Displayed normal data adapter, but the picture is not displayed. if doing so
m.put(VideoList.ATTRIBUTE_NAME_IMAGE, R.drawable.ic_launcher);
then the standard output adapter icon android, but I need to display an image from the Internet
Hi you can use an Universal Image loader for this purpose, what you can do is make a custom list , before show a progress icon or dialog and later replace that with an image loaaded from the internet please visit
https://github.com/nostra13/Android-Universal-Image-Loader
It is much better to use a custom adapter as #Raghunandan suggested. You have much more control and it's not that hard.
First, create a class that extends ArrayAdapter and pass the type of object you want to handle:
public class MyCustomAdapter extends ArrayAdapter<MyObjectModel> {
Your adapter must contain a Collection (Lists are fine) of your MyModelObject objects. You can pass this list in the constructor of the adapter and keep a reference in a variable. Each object should have a property with the URL of an image.
Second, create a simple layout for each item in your list. Your layout must contain an ImageView somewhere:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ... >
<ImageView
android:id="#+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
Third, back to your adapter, you must implement the getView() method. This method returns a view, based on your custom layout, that contains the info from your MyObjectModel object at the given position in the list.
Inflate your layout : view = inflater.inflate(R.layout.custom_list_item, null);
Get your ImageView component : imageView = (ImageView)view.findViewById(R.id.myImage);
Get the object from the list : item = list.get(position);
Download the image in an asynctask or use a 3rd party tool such as Universal Image Loader, Picasso etc. Personally I use UrlImageViewHelper : UrlImageViewHelper.setUrlDrawable(imageView, item.imageURL, ...);
IMO it's the only way to have a nice and custom list because default Android options don't allow much customization.
Have a look at http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ for some tips to improve your custom adapter.

ListView getChildAt(i) gives null

I have a listview with with custom Layout. ListView items come from a separate xml document. The listview renders properly, now I want to access the textview and the editview which is in that listview, and I want to change the value of that dynamically. I tried it using list.getChildAt(i) but it gives me a null view. So how can I access the textviews?
ListView list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this,5,R.layout.layout_id);
list.setAdapter(adapter);
for (int j = 0; j < list.getCount(); j++) {
View v = list.getChildAt(j);
EditText text = (EditText) v.findViewById(R.id.textview1);
text.setText("Hello");
}
Ok I tried a lot and found that list.getChildAt(i) gives me value on onClickListener(). I want to get that value after the list render in onCreate() method. So how can i get the list value immediately of list render?
As you said you used a custom Layout, did you write your own Adapter? If so, you properly have to override the getChildAt-method. Hence you have to manage the model on your own.

Categories

Resources