retrieving an image from parse database - android

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.

Related

How to Display Images from Assets - Image Name from CursorAdapter?

have db with _id,hName,lName,detail,images
images stored in assets
images name in db (SQLite)
list layout without images working fine, but with images layout display's on names not images (no error in logcat)
wanted to bind CursorAdpater with `ImageView'
public void bindView(View view, Context context, Cursor cursor) {
TextView hName = view.findViewById(R.id.hName);
hName.setText(cursor.getString(cursor.getColumnIndex("hName")));
TextView lName = view.findViewById(R.id.lName);
lName.setText(cursor.getString(cursor.getColumnIndex("lName")));
ImageView image = view.findViewById(R.id.image);
InputStream is = null;
try {
is = context.getAssets().open("birds/" + cursor.getColumnIndex("images"));
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(is);
image.setImageBitmap(bitmap);
}
XML file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:scaleType="fitCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:padding="24dp"
/>
<TextView
android:id="#+id/hName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/image"
android:layout_marginLeft="21dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/image"
android:layout_toRightOf="#+id/image"
android:text="Name"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/lName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image"
android:layout_alignLeft="#+id/hName"
android:layout_alignStart="#+id/hName"
android:textSize="16sp"
android:text="About" />
check the image extension is used in database
then try this
try {
is = context.getAssets().open("birds/" + cursor.getColumnIndex("images"));
Drawable d = Drawable.createFromStream(is, null);
image.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}

How to add Image URL in ListView?

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.

How to load json file that refers to drawable images into a horizontal scroll view?

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>

Android: Picasso doesn't work

Case :
I work on app which retrieve info about local venues based on coordinates from Foursquare API. All information is stored in the DB, including venue featured photo stored as prefix and suffix of the link. For the list representation I use recyclerview. I logged url links and they seem work fine. Here is my onBindViewHolder method:
public void onBindViewHolder(ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
Venue venue = venueList.get(position);
String url = venue.getFeaturedPhotos().getItems().get(0).getPrefix() + "120x120" + venue.getFeaturedPhotos().getItems().get(0).getSuffix();
((ItemViewHolder) holder).venueName.setText(venue.getName());
((ItemViewHolder) holder).streetAdr.setText(venue.getLocation().getAddress());
((ItemViewHolder) holder).rating.setText(venue.getRating().toString());
Context context = ((ItemViewHolder) holder).venuePhoto.getContext();
Picasso.with(context).load(url).into(((ItemViewHolder) holder).venuePhoto);;
((ItemViewHolder) holder).venue = venue;
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
This is how my item looks like:
<?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:orientation="horizontal">
<ImageView
android:id="#+id/venuePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="#string/imgDescr"
/>
<TextView
android:id="#+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/ratingSample"
android:textSize="45sp" />
<TextView
android:id="#+id/venueName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#id/rating"
android:layout_toRightOf="#id/venuePhoto"
android:text="#string/VenueNameSample"
android:textSize="20sp" />
<TextView
android:id="#+id/streetAdr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/venueName"
android:layout_toLeftOf="#id/rating"
android:layout_toRightOf="#id/venuePhoto"
android:text="#string/addressSample"
android:textSize="20sp" />
</RelativeLayout>
I found a solution by just adding uri parsing
String url = venue.getFeaturedPhotos().getItems().get(0).getPrefix() + "120x120" + venue.getFeaturedPhotos().getItems().get(0).getSuffix();
Uri uri = Uri.parse(url);

Android - How to "match" the data in mysql after pressed a button

In my project, im going to make a multiple choice type question. I can select the questions and answer from mysql, however, i cant do the "matching" of the answer between input answer and the data from mysql. I tried some solutions but they doesnt work as well. I need a big help on it. Below are my codes.
the java class that make multiple choice
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.normalmode);
new DataAsyncTask().execute();
}
public void onClick(View v){
inputtext = ((Button) v).getText().toString();
tv3 = (TextView)findViewById(R.id.textView3);
tv3.setText(inputtext);
if(inputtext == tv2.getText().toString()){
playerscore +=5;
} else {
playerscore +=1;
}
score.setText("Scores : " + playerscore);
new DataAsyncTask().execute();
}
class DataAsyncTask extends AsyncTask<String, String, String>{
#Override
protected String doInBackground(String... param) {
String result = DBConnector.executeQuery("SELECT * FROM questions ORDER BY RAND( ) LIMIT 1");
return result;
}
#Override
protected void onPostExecute(String result) {
question = (TextView)findViewById(R.id.textView_question);
fchoice = (Button)findViewById(R.id.Btn_choice1);
schoice = (Button)findViewById(R.id.Btn_choice2);
tchoice = (Button)findViewById(R.id.Btn_choice3);
tv2 = (TextView)findViewById(R.id.textView2);
score = (TextView)findViewById(R.id.textView_score);
try {
JSONArray jsonArray = new JSONArray(result);
JSONObject jsonData = jsonArray.getJSONObject(0);
question.setText(jsonData.getString("q_desc"));
fchoice.setText(jsonData.getString("fchoice"));
schoice.setText(jsonData.getString("schoice"));
tchoice.setText(jsonData.getString("tchoice"));
ans = jsonData.getString("q_ans");
tv2.setText(ans);
} catch(Exception e) {
Log.e("log_tag", e.toString());
}
}
}
}
three buttons are sharing same onClick in xml file by using
android:onClick="onClick"
the current problem i faced is that it always return "false" no matter i pressed which button. also, is there any way to store/pass "previous" async task data?
I have tried using internal storage but it doesnt work as well
EDIT:
my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.95" >
<TextView
android:id="#+id/textView_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/player_score" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.95" >
<TextView
android:id="#+id/textView_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/string_question"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/Btn_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/third_choice"
android:onClick="onClick" />
<Button
android:id="#+id/Btn_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Btn_submit"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/second_choice"
android:onClick="onClick" />
<Button
android:id="#+id/Btn_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Btn_choice2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/first_choice"
android:onClick="onClick" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView1"
android:text="TextView" />
</RelativeLayout>
</LinearLayout
textview2 and textview3 are used to test my output and display them as text
try this if(inputtext.equals(tv2.getText().toString())) instead of if(inputtext == tv2.getText().toString())
becasue the == operator checks to see if the two strings are exactly the same object.
The .equals() method will check if the two strings have the same value.

Categories

Resources