this app should show thumbnail , title, and the duration of each video in the playlist selected , but thumbnail remain white
the application works but It don't show the thumbnail of the video in the playlist
how can I do ? you have any solution?
THIS IS MY CODE
public class MainActivity extends Activity {
ImageView mainThumb;
TextView mainTitle;
TextView mainTime;
LinearLayout videos;
ArrayList<String> links;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.videos);
new ParseVideoDataTask().execute();
mainThumb = (ImageView) findViewById(R.id.mainThumb);
mainTitle = (TextView) findViewById(R.id.mainTitle);
mainTime = (TextView) findViewById(R.id.mainTime);
videos = (LinearLayout) findViewById(R.id.videos);
}
private class ParseVideoDataTask extends AsyncTask<String, String, String> {
int count = 0;
#Override
protected String doInBackground(String... params) {
URL jsonURL;
URLConnection jc;
links = new ArrayList<String>();
try {
jsonURL = new URL("http://gdata.youtube.com/feeds/api/playlists/" +
"PL-7t9DoIELCRF7F7bZvvBwO1yvHRFsJiu" +
"?v=2&alt=jsonc");
//PL_VGbflD64WSR1MBcpWlNwsY0lRNVuJ3r
jc = jsonURL.openConnection();
InputStream is = jc.getInputStream();
String jsonTxt = IOUtils.toString(is);
JSONObject jj = new JSONObject(jsonTxt);
JSONObject jdata = jj.getJSONObject("data");
JSONArray aitems = jdata.getJSONArray("items");
for (int i=0;i<aitems.length();i++) {
JSONObject item = aitems.getJSONObject(i);
JSONObject video = item.getJSONObject("video");
String title = video.getString("title");
JSONObject player = video.getJSONObject("player");
String link = player.getString("default");
String length = video.getString("duration");
JSONObject thumbnail = video.getJSONObject("thumbnail");
String thumbnailUrl = thumbnail.getString("hqDefault");
String[] deets = new String[4];
deets[0] = title;
deets[1] = thumbnailUrl;
deets[2] = length;
links.add(link);
publishProgress(deets);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(final String... deets) {
count++;
if (count == 1) {
MainActivity.setImageFromUrl(deets[1], mainThumb, MainActivity.this);
mainTitle.setText(deets[0]);
mainTime.setText(formatLength(deets[2]));
mainThumb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(links.get(1)));
startActivity(i);
}
});
} else {
LayoutInflater layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View video = layoutInflater.inflate(R.layout.video, null);
ImageView thumb = (ImageView) video.findViewById(R.id.thumb);
TextView title = (TextView) video.findViewById(R.id.title);
TextView time = (TextView) video.findViewById(R.id.time);
MainActivity.setImageFromUrl(deets[1], thumb, MainActivity.this);
title.setText(deets[0]);
time.setText(formatLength(deets[2]));
video.setPadding(20, 20, 20, 20);
videos.addView(video);
video.setId(count-1);
video.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(links.get(v.getId())));
startActivity(i);
}
});
}
}
}
private CharSequence formatLength(String secs) {
int secsIn = Integer.parseInt(secs);
int hours = secsIn / 3600,
remainder = secsIn % 3600,
minutes = remainder / 60,
seconds = remainder % 60;
return ((minutes < 10 ? "0" : "") + minutes
+ ":" + (seconds< 10 ? "0" : "") + seconds );
}
public static void setImageFromUrl(String string, ImageView mainThumb2,
MainActivity mainActivity) {
}
#Override
public void onDestroy() {
super.onDestroy();
for (int i=0;i<videos.getChildCount();i++) {
View v = videos.getChildAt(i);
if (v instanceof ImageView) {
ImageView iv = (ImageView) v;
((BitmapDrawable)iv.getDrawable()).getBitmap().recycle();
}
}
}
}
Use Picasso to set the image url like so:
Picasso.withContext(this).load(thumbnailUrl).into(myImageView);
To do this you'll need to return the JSON object in the doInBackground method, or use something like Retrofit (as I mentioned in the comment above) to get a nice POJO and return that. Then in the postExecute you can use the thumbnail url in the JSON (or POJO) and set it using Picasso above. Picasso will take care of downloading and caching the image for you and loading it into the image view.
Related
I created app that takes JSON with AsyncTask from server. When User click a button app starts new Activity and download data from server and show it as a items in ListView. The Problem is when I open new Activity it takes too long to load. When button is pressed app freeze on about one or two seconds and then show black screen for another 2/3 seconds. After that activity is displayed but it is very slow. It freeze every time user is scrolling or pressing button to display more options of custom adapter. Is there any way to make app more smooth? Json data that is downloaded is just simple JSONArray with JSONObjects that has 2 string values and one HTML format. This 3 values is display to user.
Part of Custom Adapter class
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
SuggestionList suggestionList = getItem(position);
int actualPosition = 0;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.sugestion_list, parent, false);
}
final Button suggestionsButton = (Button) convertView.findViewById(R.id.suggestionsMore);
final TextView suggestionNumber = (TextView) convertView.findViewById(R.id.sugestionNumber);
final TextView suggestionDescription = (TextView) convertView.findViewById(R.id.suggestionDescription);
final ImageView bio = convertView.findViewById(R.id.sugestionBio);
final ImageView block = convertView.findViewById(R.id.sugestionBlock);
final ImageView call = convertView.findViewById(R.id.sugestionCall);
...
final Animation slideUp = AnimationUtils.loadAnimation(getContext(), R.anim.slideup);
final Animation slideDown = AnimationUtils.loadAnimation(getContext(), R.anim.slidedown);
final Handler handler = new Handler();
suggestionsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (bioSuggestions.getVisibility() == View.GONE) {
bio.setVisibility(View.VISIBLE);
block.setVisibility(View.VISIBLE);
call.setVisibility(View.VISIBLE);
bioSuggestions.startAnimation(slideUp);
blockSuggestions.startAnimation(slideUp);
callSuggestions.startAnimation(slideUp);
} else if (bioSuggestions.getVisibility() == View.VISIBLE) {
bioSuggestions.startAnimation(slideDown);
blockSuggestions.startAnimation(slideDown);
callSuggestions.startAnimation(slideDown);
handler.postDelayed(new Runnable() {
#Override
public void run() {
bio.setVisibility(View.GONE);
block.setVisibility(View.GONE);
call.setVisibility(View.GONE);
}
}, 300);
}
}
});
if (actualPosition != position) {
if (bio.getVisibility() == View.VISIBLE) {
bio.setVisibility(View.GONE);
block.setVisibility(View.GONE);
call.setVisibility(View.GONE);
}
actualPosition = position;
}
JSONObject jsonValSuggestions = new getSugestions().sugestionsDetails(position, "suggestions");
try {
final String name = jsonValSuggestions.getString("client_name");
final String num = jsonValSuggestions.getString("client_number");
final String description = jsonValSuggestions.getString("client_description");
bio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent suggestionsDetails = new Intent(view.getContext(), SuggestionsDetails.class);
suggestionsDetails.putExtra("client_number", num);
suggestionsDetails.putExtra("client_name", name);
suggestionsDetails.putExtra("client_description", description);
activity.startActivityForResult(suggestionsDetails, position);
}
});
block.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent suggestionBlock = new Intent(view.getContext(), BlockSuggestionsActivity.class);
activity.startActivity(suggestionBlock);
}
});
call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent suggestionCall = new Intent(view.getContext(), CallSuggestionActivity.class);
suggestionCall.putExtra("client_number", num);
suggestionCall.putExtra("client_name", name);
activity.startActivity(suggestionCall);
}
});
} catch (Exception e) {
e.printStackTrace();
}
try {
if (suggestionList.suggestionName.equals("null") || suggestionList.suggestionName.equals("")) {
suggestionNumber.setText(suggestionList.suggestionNumber);
} else {
suggestionNumber.setText(suggestionList.suggestionName);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
suggestionDescription.setText(Html.fromHtml(suggestionList.suggestionDescription, Html.FROM_HTML_MODE_LEGACY));
} else {
suggestionDescription.setText(Html.fromHtml(suggestionList.suggestionDescription));
}
} catch (Exception e) {
Log.i("exception", e.getMessage());
}
return convertView;
}
Part of AsyncTask class
public static final String REQUEST_METHOD = "GET";
public static final int READ_TIMEOUT = 15000;
public static final int CONNECTION_TIMEOUT = 15000;
#Override
protected String doInBackground(String... params) {
String clientUrl = params[0];
String result;
String inputLine;
JSONObject obj;
String data;
String message;
try {
URL myUrl = new URL(clientUrl);
HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.connect();
InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
while ((inputLine = reader.readLine()) != null) {
stringBuilder.append(inputLine);
}
reader.close();
streamReader.close();
result = stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
result = null;
}
return result;
}
public String[] getSuggestionsList() {
String[] suggestionList = new String[5];
String result;
String status;
JSONObject listObj;
String suggestionsData;
JSONObject suggestionsDataObj;
JSONArray suggestionsDataArr;
String ClientsSugestionsUrl = "https://example.com/token=" + authToken;
getApiClientSugestions getSugestionsFromApi = new getApiClientSugestions();
try {
result = getSugestionsFromApi.execute(ClientsSugestionsUrl).get();
try {
listObj = new JSONObject(result);
status = listObj.getString("result");
suggestionsData = listObj.getString("suggestions");
suggestionsDataArr = new JSONArray(suggestionsData);
} catch (Exception e) {
e.printStackTrace();
suggestionsDataArr = null;
status = null;
}
suggestionList[3] = status;
suggestionList[4] = suggestionsDataArr.toString();
} catch (Exception e) {
e.printStackTrace();
}
return suggestionList;
}
Activity
public class CallsSuggestionsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calls_suggestions);
Slidr.attach(this);
getSupportActionBar().setTitle("Skontaktuj się");
}
#Override
protected void onResume() {
super.onResume();
CallsSuggestionList();
}
public void CallsSuggestionList() {
final ListView suggestionList = findViewById(R.id.sugestionList);
final ArrayList<SuggestionList> suggestionArray = new ArrayList<SuggestionList>();
SuggestionListAdapter suggestionListAdapter = new SuggestionListAdapter(getContext(), suggestionArray, this);
String[] suggestionListArray = new getSugestions().getSuggestionsList();
String suggStat = suggestionListArray[3];
String arrayList = suggestionListArray[4];
String clientName;
String clientNumber;
String clientDescription;
try {
JSONArray jsonArray = new JSONArray(arrayList);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
clientName = explrObject.getString("client_name");
clientNumber = explrObject.getString("client_number");
clientDescription = explrObject.getString("client_description");
if (suggStat.equals("true")) {
SuggestionList suggestionList1 = new SuggestionList(clientName, clientDescription, clientNumber);
suggestionListAdapter.addAll(suggestionList1);
suggestionListAdapter.notifyDataSetChanged();
suggestionList.setAdapter(suggestionListAdapter);
}
}
} catch (Exception e) {
Log.i("exception", e.getMessage());
e.printStackTrace();
clientName = null;
clientDescription = null;
clientNumber = null;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
SuggestionList
public class SuggestionList {
public String suggestionNumber;
public String suggestionDescription;
public String suggestionCallType;
public String suggestionName;
public SuggestionList(
// String suggestionCallType,
String suggestionName, String suggestionDescription, String suggestionNumber) {
this.suggestionNumber = suggestionNumber;
// this.suggestionCallType = suggestionCallType;
this.suggestionName = suggestionName;
this.suggestionDescription = suggestionDescription;
}
}
Adapter are custom with custom view displayed to user. I use similar custom adapter to show content from sqlite that is on phone and there app isn't so slow. But when I open this activity it slow down dramatically. Also I noticed when I press back button it take very long to back to previous screen.
The problem is in the getSuggestionsList function. in this function, you are calling getSugestionsFromApi.execute(ClientsSugestionsUrl).get(); which make your code sync again. I mean your code is waiting this code to be executed.
One way (not right way, but easy way): you can call new getSugestions().getSuggestionsList(); in a new thread.
Second way, call getSugestionsFromApi.execute(ClientsSugestionsUrl) without get() function. But to get result of the code, you need to give an interface.
To get right usage: https://xelsoft.wordpress.com/2014/11/28/asynctask-implementation-using-callback-interface/
i wish to display my data and image which download from an URL but after i download the image from URL, the order of the image go wrong. I displayed my data and images using card view and recycle view, but the first image in first card go to second, while the second image at third card and the third one will missing and the first one will empty. Anyone can help me on this?
Below is my code. Tq
SimpleCardViewAdapter.java
public class SimpleCardViewAdapter extends RecyclerView.Adapter<SimpleCardViewAdapter.ViewHolder> {
private List<CardViewData> mDataset;
Bitmap downloadedBitmap;
ImageView row_image;
public SimpleCardViewAdapter(List<CardViewData> dataset) {
mDataset = dataset;
}
#Override
public ViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(final ViewHolder viewHolder, int i) {
final CardViewData cardViewData = mDataset.get(i);
viewHolder.mTitle.setText(cardViewData.getTitle());
viewHolder.mDescription.setText(cardViewData.getDescription());
String var = cardViewData.getImage();
new Thread(new getImageTask(var)).start();
viewHolder.mImage.setImageBitmap(downloadedBitmap);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Title: " + cardViewData.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
class getImageTask implements Runnable {
String imageUrl;
getImageTask(String imgUrl){
imageUrl = imgUrl;
}
#Override
public void run() {
try {
URL url = new URL(imageUrl);
downloadedBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Message msg = new Message();
Bundle b = new Bundle();
b.putString("mfeApi", "1");
msg.setData(b);
handlerGroupedProductsImage.sendMessage(msg);
}
}
Handler handlerGroupedProductsImage = new Handler() {
public void handleMessage(Message msg) {
Bundle b = msg.getData();
String done = b.getString("mfeApi");
};
};
#Override
public int getItemCount() {
return mDataset == null ? 0 : mDataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTitle;
public TextView mDescription;
public ImageView mImage;
public ViewHolder(View itemView) {
super(itemView);
mTitle = (TextView) itemView.findViewById(R.id.row_title);
mDescription = (TextView) itemView.findViewById(R.id.row_description);
mImage = (ImageView) itemView.findViewById(R.id.row_image);
}
}
Activity.java
private void getData() {
try {
final String toSend = "http://www.test.com.my/test/api/kipcard_helpAPI.php";
new Thread(new Runnable() {
#Override
public void run() {
URL url = null;
HttpURLConnection conn = null;
String receivedString = null;
try {
url = new URL(toSend);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
InputStream in = new BufferedInputStream(conn.getInputStream());
receivedString = IOUtils.toString(in, "UTF-8");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Message msg = new Message();
Bundle b = new Bundle();
b.putString("mfeApi", receivedString);
msg.setData(b);
hGet.sendMessage(msg);
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
Handler hGet = new Handler() {
public void handleMessage(Message msg) {
Bundle b = msg.getData();
String s = b.getString("mfeApi");
items = s.split("\\|\\|");
size = items.length;
List <CardViewData> list = new ArrayList<CardViewData>();
for(int i = 0 ; i <= size-3 ; i+=3){
Log.d("here",items[i] +"" );
//new Thread(new getImageTask(imahr)).start();
list.add(new CardViewData(items[i+2], items[i+3], items[i+1]));
}
mAdapter = new SimpleCardViewAdapter(list);
mRecyclerView.setAdapter(mAdapter);
};
};
The problem is here:
new Thread(new getImageTask(var)).start();
viewHolder.mImage.setImageBitmap(downloadedBitmap);
As the thread do it's job parallel not synchronously downloadedBitmap can hold another's thread "old" result.
Instead of
new Thread(new getImageTask(var)).start();
viewHolder.mImage.setImageBitmap(downloadedBitmap);
Try using
final int position = i;
viewHolder.position = i;
new AsyncTask<Object, Void, Bitmap>() {
private AlbumViewHolder v;
#Override
protected Bitmap doInBackground( Object[] params ) {
v = (AlbumViewHolder) params[ 0 ];
// download here your image
return b;
}
#Override
protected void onPostExecute( Bitmap result ) {
super.onPostExecute( result );
if ( v.position == position && result != null ) {
// If this item hasn't been recycled already, hide the
// progress and set and show the image
v.mImageView.setImageBitmap( result );
v.mImageView.setVisibility( View.VISIBLE );
v.mProgressBar.setVisibility( View.GONE );
}
}
}.execute( viewHolder );
and in ViewHolder :
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTitle;
public TextView mDescription;
public ImageView mImage;
public int position;
public ViewHolder(View itemView) {
super(itemView);
mTitle = (TextView) itemView.findViewById(R.id.row_title);
mDescription = (TextView) itemView.findViewById(R.id.row_description);
mImage = (ImageView) itemView.findViewById(R.id.row_image);
}
}
And one more thing - your ViewHolder should have position parameter. And think about caching.
Try this:
add Context to your constructor
public SimpleCardViewAdapter(List<CardViewData> dataset, Activity activity){
this.mActivity = activity;
}
change in onBindViewHolder method:
new Thread(new getImageTask(var, viewHolder.mImage)).start();
and change your task like this:
class getImageTask implements Runnable {
String imageUrl;
ImageView imageView
getImageTask(String imgUrl, ImageView imageView){
imageUrl = imgUrl;
this.imageView = imageView;
}
#Override
public void run() {
try {
URL url = new URL(imageUrl);
//change this line also
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
//set you downloaded image now...
//new change ....
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
//Your code to run in GUI thread here
if(bitmap != null)
imageView.setImageBitmap(bitmap);
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Message msg = new Message();
Bundle b = new Bundle();
b.putString("mfeApi", "1");
msg.setData(b);
handlerGroupedProductsImage.sendMessage(msg);
}
}
***but it will not cache bitmap, every time you scroll your recycler inside app it will download it again and again.
I have a ListView and a list view adapter. The adapter populates the ListView from a List. The list view has a onScrollListener. The problem I have is when on scroll new data are loaded to the view but the scroll bar jumps to the top of the view.
What I want is to keep the scroll position where it was!
Any help?
Thanks
List View class:
private class GetItems extends AsyncTask<Void, Void, Void>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(AppList.this);
// Set progressdialog title
mProgressDialog.setTitle("Loading more");
mProgressDialog.setMessage("loading);
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params)
{
applicationList = new ArrayList();
try
{
GetDataAppList getDataAppList = new GetDataAppList();
JSONArray jsonData = getDataAppList.getJSONData(webfileName, limit, offset);
for (int i = 0; i <= jsonData.length() - 2; i++)
{
JSONObject c = jsonData.getJSONObject(i);
id = c.getString("id");
name = c.getString("name");
logo = c.getString("logo");
developer = c.getString("developer");
rate = c.getInt("rates");
category = c.getInt("category");
fileName = c.getString("filename");
path = c.getString("path");
appSize = c.getDouble("size");
if(category == 1001)
{
String gCat = c.getString("game_category");
applicationList.add(new ApplicationPojo(id,name,logo,developer,appSize,category,fileName,path,gCat));
}
else
{
applicationList.add(new ApplicationPojo(id,name,logo,developer,appSize,category,fileName,path));
}
}
JSONObject sizeObj = jsonData.getJSONObject(jsonData.length() - 1);
size = sizeObj.getInt("size");
}
catch (Exception ex)
{
Log.d("Thread:", ex.toString());
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
// Locate the ListView in listview.xml
listview = (ListView) findViewById(R.id.listView);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(AppList.this, applicationList,listview);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// Create an OnScrollListener
listview.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int threshold = 1;
int count = listview.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (listview.getLastVisiblePosition() >= count - threshold) {
if (size >= offset)
{
new LoadMoreDataTask().execute();
offset = offset + 15;
}
else
{
Toast.makeText(getApplicationContext(), "ختم لست!", Toast.LENGTH_LONG).show();
}
}
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
RatingBar bar = (RatingBar) findViewById(R.id.ratingBarShow);
}
}
private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(AppList.this);
mProgressDialog.setTitle("Loading more");
mProgressDialog.setMessage("loading);
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params)
{
try
{
GetDataAppList getDataAppList = new GetDataAppList();
JSONArray jsonData = getDataAppList.getJSONData(webfileName, limit, offset);
for (int i = 0; i <= jsonData.length(); i++) {
JSONObject c = jsonData.getJSONObject(i);
id = c.getString("id");
name = c.getString("name");
logo = c.getString("logo");
developer = c.getString("developer");
rate = c.getInt("rates");
category = c.getInt("category");
fileName = c.getString("filename");
path = c.getString("path");
appSize = c.getDouble("size");
if(category == 1001)
{
String gCat = c.getString("game_category");
applicationList.add(new ApplicationPojo(id,name,logo,developer,appSize,category,fileName,path,gCat));
}
else
{
applicationList.add(new ApplicationPojo(id,name,logo,developer,appSize,category,fileName,path));
}
}
}
catch (Exception ex)
{
}
return null;
}
#Override
protected void onPostExecute(Void result)
{
int position = listview.getLastVisiblePosition();
adapter = new ListViewAdapter(AppList.this, applicationList,listview);
listview.setAdapter(adapter);
listview.setSelectionFromTop(position, 0);
mProgressDialog.dismiss();
}
}
The Adpater class:
public ListViewAdapter(Activity activity, ArrayList<ApplicationPojo> applicationList, ListView listView)
{
this.activity = activity;
this.applicationList = applicationList;
this.inflater = LayoutInflater.from(activity);
downloader = new ApkFileDownloader(activity);
this.listView = listView;
}
#Override
public int getCount() {
return applicationList.size();
}
#Override
public ApplicationPojo getItem(int position) {
return applicationList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent)
{
if (view == null)
{
holder = new ViewHolder();
view = inflater.inflate(R.layout.singleapp, null);
holder.openInstalledAppBtn = (ImageView) view.findViewById(R.id.openInstalledApp);
holder.downloadBtn = (ImageView) view.findViewById(R.id.updateApp);
holder.progressBar = (ProgressBar)view.findViewById(R.id.updateProgress);
holder.cancelBtn = (ImageView) view.findViewById(R.id.cancel);
holder.appName = (TextView) view.findViewById(R.id.appName);
holder.developer = (TextView) view.findViewById(R.id.developer);
holder.size = (TextView) view.findViewById(R.id.size);
holder.appCat = (TextView) view.findViewById((R.id.appCat));
holder.installBtn = (ImageView) view.findViewById(R.id.install);
holder.catlogo = (ImageView) view.findViewById(R.id.catlogo);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
try
{
final View finalView = view;
holder.logo = (ImageView) finalView.findViewById(R.id.appLogo);
logoName = applicationList.get(position).getLogo();
Picasso.with(activity)
.load(IPClass.SERVERIP + logoName)
.into(holder.logo);
// holder.logo.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View arg0) {
//
// }
// });
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String appid = applicationList.get(position).getId();
int category = applicationList.get(position).getCategory();
Intent rec1Intent = new Intent(activity, AppView.class);
activity.startActivity(rec1Intent);
AppView appView = new AppView();
appView.setParameters(appid, category);
AppList.adapter.notifyDataSetChanged();
}
});
final String id = applicationList.get(position).getId();
final String path = applicationList.get(position).getPath();
final String fileName = applicationList.get(position).getFileName();
final String name = applicationList.get(position).getName();
final String developer = applicationList.get(position).getDeveloper();
final double size = applicationList.get(position).getSize();
final String logo = applicationList.get(position).getLogo();
final int category = applicationList.get(position).getCategory();
final String appName = applicationList.get(position).getFileName();
String checkAppInstalled = appName.substring(0,appName.length() - 4);
//------------CHECK IF APPLICATION IS INSTALLED ----------------------------------------
if(appInstalled(checkAppInstalled))
{
holder.downloadBtn.setVisibility(View.GONE);
holder.cancelBtn.setVisibility(View.GONE);
holder.progressBar.setVisibility(View.GONE);
holder.installBtn.setVisibility(View.GONE);
holder.openInstalledAppBtn.setVisibility(View.VISIBLE);
holder.openInstalledAppBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
String fileName = (applicationList.get(position).getFileName());
String appToOpen = fileName.substring(0,fileName.length() - 4);
Context ctx = activity.getApplicationContext();
Intent mIntent = ctx.getPackageManager().getLaunchIntentForPackage(appToOpen);
String mainActivity = mIntent.getComponent().getClassName();
Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName(appToOpen, mainActivity);
activity.startActivity(intent);
}
});
}
//------------- IF APPLICATION IS NOT ALREADY INSTALLED --------------------------------
else
{
//------------------------ CHECK IF APK EXISTS -------------------------------------
String filePath = Environment.getExternalStorageDirectory().toString();
File file = new File(filePath + "/appsaraai/" + fileName);
if(file.exists())
{
holder.downloadBtn.setVisibility(View.GONE);
holder.cancelBtn.setVisibility(View.GONE);
holder.openInstalledAppBtn.setVisibility(View.GONE);
holder.progressBar.setVisibility(View.GONE);
holder.installBtn.setVisibility(View.VISIBLE);
holder.installBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/appsaraai/" + fileName)), "application/vnd.android.package-archive");
activity.startActivity(intent);
for (int i = 0; i < DownloadLists.list.size(); i++) {
if (DownloadLists.list.get(i).getName().equals(name)) {
DownloadLists.list.remove(i);
}
}
}
});
AppList.adapter.notifyDataSetChanged();
}
//------------------ IF APK DOES NOT EXIST -----------------------------------------
else
{
//-----CHECK IF DOWNLOAD IS IN PROGRESS ----------------------------------------
if (ApkFileDownloader.applicationList.containsKey(name))
{
holder.downloadBtn.setVisibility(View.GONE);
holder.installBtn.setVisibility(View.GONE);
holder.openInstalledAppBtn.setVisibility(View.GONE);
new ApkFileDownloader(activity).getDownloadStatus(holder.progressBar, name, holder.installBtn, holder.cancelBtn);
holder.progressBar.setVisibility(View.VISIBLE);
holder.cancelBtn.setVisibility(View.VISIBLE);
holder.cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
downloader.cancelDownload(name);
holder.cancelBtn.setVisibility(View.GONE);
holder.downloadBtn.setVisibility(View.VISIBLE);
DownloadLists dlist = new DownloadLists(activity);
dlist.deleteData(name);
try {
AppList.adapter.notifyDataSetChanged();
} catch (Exception ex) {
System.out.println(ex);
}
try
{
SearchResult.adapter.notifyDataSetChanged();
}
catch (Exception ex)
{
System.out.println(ex);
}
}
});
}
//-------------- IF DOWNLOAD IS NOT IN PROGRESS START NEW DOWNLOAD -------------
else
{
holder.progressBar.setVisibility(View.GONE);
holder.cancelBtn.setVisibility(View.GONE);
holder.installBtn.setVisibility(View.GONE);
holder.openInstalledAppBtn.setVisibility(View.GONE);
holder.downloadBtn.setVisibility(view.VISIBLE);
holder.downloadBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
holder.downloadBtn.setVisibility(View.GONE);
holder.cancelBtn.setVisibility(View.VISIBLE);
holder.cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
downloader.cancelDownload(name);
holder.cancelBtn.setVisibility(View.GONE);
holder.downloadBtn.setVisibility(View.VISIBLE);
try {
AppList.adapter.notifyDataSetChanged();
} catch (Exception ex) {
System.out.println(ex);
}
try {
SearchResult.adapter.notifyDataSetChanged();
} catch (Exception ex) {
System.out.println(ex);
}
}
});
new Thread(new Runnable() {
#Override
public void run() {
try {
Bitmap logoImg = Picasso.with(activity).load(IPClass.SERVERIP + logo).get();
DownloadLists.list.add(new ApplicationPojo(id, name, developer, size, logoImg, holder.progressBar));
DownloadLists dlist = new DownloadLists(activity);
dlist.insertData(id, name, developer, size, fileName, logoImg);
UpdateServerDownload d = new UpdateServerDownload();
d.updateDownloadNo(id, category);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
new ApkFileDownloader(activity).setParameters(path, fileName, name);
try {
AppList.adapter.notifyDataSetChanged();
} catch (Exception ex) {
System.out.println(ex);
}
try {
SearchResult.adapter.notifyDataSetChanged();
} catch (Exception ex) {
System.out.println(ex);
}
}
});
}
}
}
holder.appName.setText(applicationList.get(position).getName());
holder.developer.setText(applicationList.get(position).getDeveloper());
String sizeText = " میگابایت ";
String appSize =String.valueOf(applicationList.get(position).getSize()) + sizeText;
holder.size.setText(appSize);
if(category == 1001)
{
String cat = applicationList.get(position).getgCat();
holder.appCat.setText(" " + returnGameCat(cat));
holder.catlogo.setImageResource(R.drawable.gamecatlogo);
}
}
catch (Exception ex)
{
Log.d("Adapter Exception", ex.toString());
}
return view;
}
//--------------- A METHOD TO CHECK IF APPLICATION IS ALREADY INSTALLED ------------------------
public boolean appInstalled(String checkApp)
{
pm = activity.getPackageManager();
try
{
pm.getPackageInfo(checkApp, PackageManager.GET_ACTIVITIES);
isAppInstalled = true;
}
catch (PackageManager.NameNotFoundException e)
{
isAppInstalled = false;
}
return isAppInstalled;
}
You are doing wrong in your GetItems and LoadMoreDataTask AsyncTask. you are setting new adapter each time when you scroll down so when new data are loaded to the view the scroll bar jumps to the top of the view.
You need to call
adapter = new ListViewAdapter(AppList.this, applicationList,listview);
listview.setAdapter(adapter);
only first time then you have to only call
adapter.notifyDataSetChanged()
to update your ListView no need to set adapter each time when making new request and also you have to set OnScrollListener to ListView only one time currently new OnScrollListener is set each time when making new request.
You need to setAdapter first time when adapter is null or you are fetching data first time after it just call notifyDataSetChanged()
Save state of listview before updating and then restore:
// save index and top position
int index = mListView.getFirstVisiblePosition();
View v = mListView.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// notify dataset changed or re-assign adapter here
// restore the position of listview
mListView.setSelectionFromTop(index, top);
The most Optimal Solution will be
// Save the ListView state (= includes scroll position) as a Parceble
Parcelable state = listView.onSaveInstanceState();
// e.g. set new items
listView.setAdapter(adapter);
// Restore previous state (including selected item index and scroll position)
listView.onRestoreInstanceState(state);
Reference : Retain Scroll Position Android ListView
I have gridview which load few images thumbnails. When I click on some image is opened in new activity full image. Now I trying to add some description about the image. When activity is loaded the info for this image is loaded also. So far so good..
The problem comes when I click on button next which load next image(full image without to back on the gridview with thumbnails) the description doesn't update and change for the next image. Is just same description for every image.
Here I download thumb, full_image and description,
String url = "http://myhost/get.php?id=" + my_id;
JSONArray data;
try {
resultServer = getJSONUrl(url);
data = new JSONArray(resultServer);
MyArrList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, Object>();
map.put("id", (String)c.getString("id"));
map.put("description", (String)c.getString("description"));
// Thumbnail Get ImageBitmap To Bitmap
map.put("ImagePathThum", (String)c.getString("image"));
map.put("ImageThumBitmap", (Bitmap)loadBitmap(c.getString("image")));
// Full (for View Full)
map.put("ImagePathFull", (String)c.getString("image_big"));
MyArrList.add(map);
}
Then this is the gridView() which display thumbs
public void ShowAllContent()
{
// gridView1
final GridView gridV = (GridView)findViewById(R.id.gridView1);
gridV.setAdapter(new ImageAdapter(act1.this,MyArrList));
// OnClick
gridV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String Position = String.valueOf(position);
Intent newActivity = new Intent(act1.this,act2.class);
newActivity.putExtra("Position", Position);
newActivity.putExtra("resultServer", resultServer);
newActivity.putExtra("description", MyArrList.get(position).get("description").toString());
startActivity(newActivity);
finish();
}
});
}
Then In act2
TextView txtView;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
txtView = (TextView) findViewById(R.id.text);
Intent intent= getIntent();
curPosition = Integer.parseInt(intent.getStringExtra("Position"));
resultServer = String.valueOf(intent.getStringExtra("resultServer"));
textDescription = getIntent().getStringExtra("description");
((TextView)findViewById(R.id.text)).setText(textDescription+"");
try {
MyArrList = ConvertJSONtoArrayList(resultServer);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("ArrayList Size",String.valueOf(MyArrList.size()));
// Show Image Full
new DownloadFullPhotoFileAsync().execute();
// Button Back
back = (Button) findViewById(R.id.btnBack);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
curPosition = curPosition - 1;
new DownloadFullPhotoFileAsync().execute();
}
});
// Button Next
next = (Button) findViewById(R.id.btnNext);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
curPosition = curPosition + 1;
new DownloadFullPhotoFileAsync().execute();
}
});
}
// Show Image Full
public void ShowImageFull(String imageName, Bitmap imgFull)
{
// Prepare Button (Back)
if(curPosition <= 0)
{
back.setEnabled(false);
}
else
{
back.setEnabled(true);
}
// Prepare Button (Next)
Log.d("curPosition",String.valueOf(curPosition));
Log.d("MyArrList.size",String.valueOf(MyArrList.size()));
if(curPosition >= MyArrList.size() - 1)
{
next.setEnabled(false);
}
else
{
next.setEnabled(true);
}
ImageView image = (ImageView) findViewById(R.id.fullimage);
try
{
image.setImageBitmap(imgFull);
} catch (Exception e) {
// When Error
image.setImageResource(android.R.drawable.ic_menu_report_image);
}
// Show Toast
Toast.makeText(act2.this,imageName,Toast.LENGTH_LONG).show();
}
public ArrayList<HashMap<String, Object>> ConvertJSONtoArrayList(String json) throws JSONException
{
JSONArray data = new JSONArray(resultServer);
ArrayList<HashMap<String, Object>> arr = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, Object>();
map.put("ImageName", (String)c.getString("name"));
map.put("ImagePathThum", (String)c.getString("image"));
map.put("ImagePathFull", (String)c.getString("image_big"));
map.put("description", (String)c.getString("description"));
arr.add(map);
}
return arr;
}
}
public class DownloadFullPhotoFileAsync extends AsyncTask<String, Void, Void> {
String strImageName = "";
String ImageFullPhoto = "";
Bitmap ImageFullBitmap = null;
#Override
protected Void doInBackground(String... params) {
strImageName = MyArrList.get(curPosition).get("ImageName").toString();
ImageFullPhoto = MyArrList.get(curPosition).get("ImagePathFull").toString();
ImageFullBitmap = (Bitmap)loadBitmap(ImageFullPhoto);
return null;
}
}
I'm sorry for long source code but I don't know which part exactly is needed.
I've tried to add in DownloadFullPhotoFileAsync
textDescription = MyArrList.get(curPosition).get("description").toString();
so when is loaded new DownloadFullPhotoFileAsync().execute(); in button next to load also this but I guess this is wrong since it doesn't work.
You can't setText in DoInBackground() function in Async Task, but you can do a setText of your description on PostExecute() which is a function of AsyncTask.
So i think you should try to set your description in PostExecute function in your Asynctask.
take a look at : http://developer.android.com/reference/android/os/AsyncTask.html
I hope this help,
Cheers !
I've built Twitter into my android application using Twitter4j api to view the tweets of one user. However it is only bringing up 7 tweets. I want at least 20...This is strange because I have set the count to 50..Can anyone help? Thanks
public class Twitter extends Activity{
LinearLayout tweets;
Context cont;
List<Status> statuses;
String separatorText = "test";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twitter);
cont = this;
statuses = new ArrayList<Status>();
tweets = (LinearLayout) findViewById(R.id.twitter_main_content);
if(Network.CheckInternet(this)){
new tweets().execute();
} else {
Toast toast = Toast.makeText(this, getResources().getString(R.string.nointernet), Toast.LENGTH_SHORT);
toast.show();
}
}
public void QueryTweets(){
ScrollView sView = new ScrollView(cont);
sView.setVerticalScrollBarEnabled(false);
sView.setHorizontalScrollBarEnabled(false);
LinearLayout layout = new LinearLayout(cont);
layout.setOrientation(1);
if(statuses.size()>0){
for(int i=0; i<statuses.size(); i++){
boolean addSeparator = false;
if(i==0){
addSeparator = true;
}
View TweetItem = LayoutInflater.from(getBaseContext()).inflate(R.layout.xmllisting_tweet, null);
TweetItem.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView tweet = (TextView) TweetItem.findViewById(R.id.xmllisting_tweet_title);
tweet.setText(statuses.get(i).getText());
TextView tweet_posted = (TextView) TweetItem.findViewById(R.id.xmllisting_tweet_post_details);
tweet_posted.setText(statuses.get(i).getCreatedAt().toLocaleString());
final int IntToUse = i;
TweetItem.setOnClickListener(new OnClickListener(){
public void onClick(View v){
String url = "https://twitter.com/" + statuses.get(IntToUse).getUser().getScreenName() + "/status/" + statuses.get(IntToUse).getId();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
//load image asynchronously - background task
ImageView imgThmb = (ImageView) TweetItem.findViewById(R.id.xmllisting_image);
loadImage downloader = new loadImage(imgThmb, false);
downloader.execute(statuses.get(i).getUser().getBiggerProfileImageURL());
//compare dates of listings to check if separator needed
String dateCompare = statuses.get(i).getCreatedAt().toGMTString().substring(0, 11);
if(!dateCompare.equals(separatorText)){
separatorText = dateCompare;
addSeparator = true;
}
if(addSeparator){
View feedSep = LayoutInflater.from(getBaseContext()).inflate(R.layout.tweet_separator, null);
feedSep.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView sepText = (TextView) feedSep.findViewById(R.id.tweet_separator);
sepText.setText(separatorText);
layout.addView(feedSep);
}
layout.addView(TweetItem);
}
}
sView.addView(layout);
tweets.addView(sView);
}
private class tweets extends AsyncTask<String, Void, Long>{
protected void onPostExecute(Long result) {
QueryTweets();
}
#Override
protected Long doInBackground(String... args) {
long totalSize = 0;
try {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("xxx")
.setOAuthConsumerSecret("xxx")
.setOAuthAccessToken("xxx")
.setOAuthAccessTokenSecret("xxx");
TwitterFactory tf = new TwitterFactory(cb.build());
//AccessToken accessToken = new AccessToken(SessionVariables.twitter_access_key, SessionVariables.twitter_access_secret);
//twitter4j.Twitter twitter = tf.getInstance(accessToken);
twitter4j.Twitter twitter = tf.getInstance();
String searchText = "CFABUK";
Query qry = new Query(searchText);
qry.setCount(50);
QueryResult qr;
try{
qr = twitter.search(qry);
for(int i=0; i<qr.getTweets().size(); i++){
twitter4j.Status status = qr.getTweets().get(i);
if(searchText.equals(status.getUser().getScreenName())){
statuses.add(status);
}
}
} catch(TwitterException e){
e.printStackTrace();
}
} catch (Exception e) {
Log.e("TwitterFeed", "Error: " + e.toString());
}
return totalSize;
}
}
}
You should use Paging,
take a look at:
Working with timelines, pages
or
Twitter4j Code-Examples - Paging
or
Twitter4j Paging javadoc
EDIT in your case I would use this:
and here's how Ive managed to get 100 tweets (Im writing tweets to SQLite DB, but Pagination would be the same in your case)
void fetch_timeline(){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
List<Status> statuses;
int count= 100;
try {
statuses = Constants.TwConst.getHomeTimeline(new Paging().count(count)); // HERE'S WHAT WILL PROBABLY HELP YOU
for(Status status: statuses){
// HERE I PARSE TWEETS AND WRITE THEM TO DATABASE
}
} catch (TwitterException e) {
e.printStackTrace();
}
}