Can I add ImageView to this list from External URL?
item.put will just add the URL link but it won't display the image at all
It's not duplicate. I just want to know what additional code I have to add to load the image on this list.
try {
JSONObject object = new JSONObject(string);
JSONArray offers = object.optJSONArray("data");
ArrayList<HashMap<String, String>> list = new ArrayList<>();
HashMap<String, String> item;
for(int i = 0; i < offers.length(); i++) {
JSONObject jsonChildNode = offers.getJSONObject(i);
item = new HashMap<>();
item.put("L1", jsonChildNode.optString("gate"));
item.put("L2", jsonChildNode.optString("stack"));
item.put("L3", jsonChildNode.optString("image"));
list.add(item);
}
sa = new SimpleAdapter(getActivity(), list,
R.layout.installs_item,
new String[]{"L1", "L2", "L3"},
new int[]{R.id.gate, R.id.stack, R.id.image});
installs.setAdapter(sa);
} catch (JSONException e) {
e.printStackTrace();
}
installs_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/L1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Head"
android:textSize="22sp"
android:textStyle="italic" />
<TextView
android:id="#+id/L2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sub"
android:textSize="14sp" />
<TextView
android:id="#+id/L3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="desc"
android:textSize="14sp" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="" />
</LinearLayout>
Create your own adapter that contains imageview in its xml and use glide library(https://github.com/bumptech/glide) to display images in it
It's recommended using an external lib will reduce the load and image handling process much easier.
Recommended lib: http://square.github.io/picasso
This reuses the image that already has been used and reduces data usage.
Related
I want to list the information I got from json in my project in the recyclerview, but I could not succeed. Can you help me?
My Codes :
RecyclerView countries = (RecyclerView) findViewById(R.id.countries);
countries.setLayoutManager(new LinearLayoutManager(this));
CityList cityList = new CityList();
try {
BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.jsondata)));
StringBuilder jsonBuilder = new StringBuilder();
for (String line = null; (line = jsonReader.readLine()) != null; ) {
jsonBuilder.append(line).append("\n");
}
Gson gson = new Gson();
cityList = gson.fromJson(jsonBuilder.toString(), CityList.class);
}
catch (FileNotFoundException e){
Log.e("jsonFile", "file not found.");
}
catch (IOException e){
Log.e("jsonFile","ioerror");
}
List <String> data = new ArrayList<>();
for(int i=0; i<cityList.getList().size(); i++){
data.add(cityList.getList().get(i).getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.country_item_layout, data);
adapter.setDropDownViewResource(R.layout.country_item_layout);
countries.setAdapter(adapter);
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/countries"
tools:listitem="#layout/country_item_layout"
app:layoutManager="LinearLayoutManager"
/>
</FrameLayout>
</LinearLayout>
COUNTRİES XML
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="3dp"
android:layout_marginHorizontal="8dp"
android:layout_marginTop="8dp"
>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.ExampleFragment" >
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="7dp"
android:layout_marginHorizontal="15dp">
<TextView
android:id="#+id/countryName"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_marginEnd="30dp"
android:textColor="#color/black"
android:text="Türkiye"
android:layout_centerVertical="true"
android:gravity="center"
android:textSize="15dp" ></TextView>
<TextView
android:id="#+id/temperature"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp"
android:textColor="#color/black"
android:text="Temperature"
android:layout_centerVertical="true"
android:gravity="center"
android:textSize="15dp" ></TextView>
</RelativeLayout>
</androidx.fragment.app.FragmentContainerView>
</com.google.android.material.card.MaterialCardView>
PROBLEMS:
'setAdapter(androidx.recyclerview.widget.RecyclerView.Adapter)' in 'androidx.recyclerview.widget.RecyclerView' cannot be applied to '(android.widget.ArrayAdapter<java.lang.String>)'
ArrayAdapter is not gonna work with RecyclerView. You have to create your own custom adapter. Follow this and build your own custom adapter.
ArrayAdapter is not applicable for RecyclerView. You need to implement your own Adapter. See this answer for possible solution: https://stackoverflow.com/a/38931340/7703505
I'm kinda new to android studio and most of my work was done referring to Stack Overflow answered questions and topics cutting short to the question.
my JSON is as such:
[
{ "name":"station1",
"url":"http://example1.com",
"image":"R.drawable.radio1"
},
{ "name":"station2",
"url":"example2.com",
"image":"R.drawable.radio2"
}
]
and so on,
and my XML is
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="100dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/relative1"
android:layout_width="100dp"
android:layout_height="100dp"></RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
I need to load the image and name in a scroll view horizontally created dynamically every time I add another "name and image" to the JSON file please can someone help me with a code (the text below every image that loads).
First i suggest to create a class BEAN like this:
public class MyBean{
String name;
String url;
String image;
}
Now, as GrIsHu describes in their answer here:
you need to read the Json File from your assests file using below code:
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("yourfilename.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
After, transform the Json String into JsonArray like this:
JSONArray ja = new JSONArray(loadJSONFromAsset());
Now you can popolate a
List<MyBeen.class> lst
like this:
for (int i = 0; i < ja.length(); i++) {
lst.add(gSon.fromJson(ja.get(i).toString(), MyBeen.class));
}
first to all do you need to change HorizontalScrollView in ListView
https://developer.android.com/guide/topics/ui/layout/listview.html
, as:
<LinearLayout
android:id="#+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="UselessParent">
<ListView
android:id="#+id/lstView"
android:layout_margin="5dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp">
</ListView>
</LinearLayout>
When you add a in MainClass this code for work with ListView:
ListView listView = findViewById(R.id.lstView);
listView.setAdapter(new CustomAdapter(this, lst));
After, in CustomAdapter.java, need:
public class CustomAdapter extends BaseAdapter {
public CustomAdapter(MainClass mainActivity, List<?> iLst) {
lst = iLst;
context=mainActivity;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView;
rowView = inflater.inflate(R.layout.listview_custom, null); <-- RAPPRESENTE A SINGLE ROW LAYOUT
ImageView img = (ImageView) rowView.findViewById(R.id.customImg);
TextView text =(TextView) rowView.findViewById(R.id.customText);
return rowView;
}
}
At end, you add the XML layout for a single row, it will duplicate automatically for each row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/border_radius"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0sp"
android:layout_weight=".2"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:id="#+id/customImg"
android:layout_width="50sp"
android:layout_height="45sp"
android:drawableTint="#color/White"
android:background="#drawable/upload"
android:tint="#color/White"/>
</LinearLayout>
<LinearLayout
android:layout_width="0sp"
android:layout_weight=".6"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/customText"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:ellipsize="none"
android:scrollHorizontally="false"
android:maxLines="100"
android:textColor="#color/White"
android:text="Title" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
I have my information posted already on parse database but i want to retrieve it using recircularView.
can anyone please guide me or direct me on how should i do it?
here is my code of the xml that holds the layout of the recircularView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/size_byte"
android:layout_marginLeft="#dimen/size_word"
android:layout_marginRight="#dimen/size_word"
android:layout_marginTop="#dimen/size_byte">
<ImageView
android:id="#+id/adPic"
android:layout_width="#dimen/movie_thumbnail_width"
android:layout_height="#dimen/movie_thumbnail_height"
android:layout_centerVertical="true"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/adTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/adPic"
android:layout_marginLeft="56dp"
android:alpha="0.87"
android:gravity="right"
android:padding="#dimen/size_half_byte"
android:text="Hitman Agent 47"
android:textSize="#dimen/size_text_primary" />
<TextView
android:id="#+id/adPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/adTitle"
android:alpha="0.87"
android:padding="#dimen/size_half_byte"
android:text="5.00$"
android:textSize="#dimen/size_text_secondary" />
<TextView
android:id="#+id/adDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/adPrice"
android:alpha="0.87"
android:padding="#dimen/size_half_byte"
android:text="31-31-31"
android:textSize="#dimen/size_text_secondary" />
</RelativeLayout>
You can get the image url from ParseObject using getUrl() method of ParseFile
Suppose you get a list of ParseObjects like this
List<MyObject> myObjects = nee ArrayList();
List<ParseObject> objects
for (ParseObject objectItem: objects) {
String fileName = "";
String fileURL = "";
if (objectItem.getParseFile("imageColumn") != null) {
try {
ParseFile parseFile = (ParseFile) objectItem
.getParseFile("imageColumn");
fileURL = parseFile.getUrl();
fileName = parseFile.getName();
myObjects.add(new MyObject(fileName,fileURL));
} catch (Exception e) {
}
}
}
Later in your getView use the file URL to get the image only for the visible items in the listview. Such that your listview wont crash.
Whatever I set ListView attribute,it has a empty line at the top,I can't get rid of,I find many methods,but can't figure it out.My environment is in Android 4.1,so please help,thanks in advance.
My xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f0f0"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17px" />
<TextView
android:id="#+id/tvSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10px" />
<com.ibelieve.news.View.XListView
android:id="#+id/lvArray"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000" >
</com.ibelieve.news.View.XListView>
</LinearLayout>
Code file:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_list);
listview = (XListView) findViewById(R.id.lvArray);
ArrayList<News> newslist = getIntent().getParcelableArrayListExtra("newslist");
ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
String[] result = new String[newslist.size()];
for (int i = 0; i < newslist.size(); i++) {
for (int j = 0; j < 4; j++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", newslist.get(i).Title);
map.put("source", newslist.get(i).Source + " " + newslist.get(i).Time);
list.add(map);
}
}
simpleAdapter = new SimpleAdapter(NewsListActivity.this, list, R.layout.news_list, new String[] { "title", "source" }, new int[] { R.id.tvTitle, R.id.tvSource });
listview.setAdapter(simpleAdapter);
}
You are using your activity's resource as an item for list view. Move
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17px" />
<TextView
android:id="#+id/tvSource"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10px" />
To a new resource files and use that resource file in new SimpleAdapter(...) parameters
edit: oh and those "empty lines" are those textviews that you added in your activity's resources. And each your listview item contains an empty listview...ception
I have JSON data which contents Strings and Image URLs, and I already parse it in the first Activity. How can I display it like the address book in my customized Listview in second Activity Should I customize an Adapter?
If that's the solution, what should I do?
I'm not familiar with Adapter.
Here's the code from the first Activity:
public class BBtest05Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
executeHttpGet();
} catch (Exception e) {
Log.i("bird","parser failure");
e.printStackTrace();
}
}
String aStr = new String();
String aStrArray[] = new String[2048];
public void executeHttpGet() throws Exception {
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://test.mobibon.com.tw/MovieGoTest/GetMovies.js");
HttpResponse response = client.execute(request);
String retSrc = EntityUtils.toString(response.getEntity(),"utf-8");
//there's a space in the head...
retSrc = retSrc.substring(1);
JSONObject obj = new JSONObject(retSrc);
JSONArray movieArray = obj.getJSONArray("movies");
for(int i = 0;i < movieArray.length(); i++)
{
JSONObject movie_data = movieArray.getJSONObject(i);
aStr = aStr+","+movie_data.getString("cTitle");
}
} finally {
aStrArray = aStr.split(",");
ListView list = (ListView) findViewById(R.id.linearLayout1);
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, aStrArray));
list.setTextFilterEnabled(true);
}
}
public void aMethod(View theButton) {
Intent i = new Intent(this,nowamovie.class);
startActivity(i);
}
}
the aMethod is changing Activity method...
and the following code is my XML file in second Activity...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:orientation="vertical" android:background="#drawable/pic_background">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/linearLayout1" >
<ImageView android:layout_height="wrap_content"
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:src="#drawable/ic_launcher"></ImageView>
<TextView android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginTop="6dip"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/linearLayout1"
android:orientation="horizontal">
<TextView android:id="#+id/textView2"
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>
<TextView android:text="TextView" android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="6dip">
</TextView>
</LinearLayout>
What should I do to put my data from JSON parser into this Activity?
Yes you need to customize your adapter and loading of images should be lazy, search on google for Lazy Implementation of images in list, and you would get enough material on this. In 3.0 there is a new feature introduced which is Loader, which do this lazy loading work for you.