Here is my code displaying a list that is being parsed from an html file.
private class getItemDesc extends AsyncTask<Void, Void, Void> {
private ArrayList<String> descArray;
#Override
protected Void doInBackground(Void... arg0) {
try {
File file = new File(dir, getString(R.string.html_file));
descArray = new ArrayList<String>();
FileInputStream in = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(
in, "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
Document doc = Jsoup.parse(line);
Elements descs = doc.select("p");
for (Element desc : descs) {
descArray.add(desc.text());
}
}
in.close();
br.close();
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void v) {
MyAdapter mAdapter = new MyAdapter(getApplicationContext(),
R.layout.list_layout, descArray);
listView.setAdapter(mAdapter);
}
There is a lot of data coming from this (over 100 items in the array) and it's taking a while to load. I was wondering if there is a way to load 10 items at a time and use an OnScrollListener to continue loading the data from the file? Any suggestions will be appreciated.
Unfortunetly I haven't used that approach before but how about loading for instance 10 elements when list is scrolled to the very bottom . It can be done by simply reseting the adapter , yet there's a question of efficienty . But in my opinion 10 elements would do the thing.
lv = (ListView)findViewById(R.id.list_view);
adapter = new CustomAdaper(this ,/* List of elements*/);
lv.setAdapter(adapter);
lv.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//Check if the last view is visible
if (++firstVisibleItem + visibleItemCount > totalItemCount) {
adapter = new CustomAdapter(getApplicationContext() , /*UpdateYourListData*/)
lv.setAdapter(adapter);
}
}
});
So basicly whenever ListView reach bottom you need to create new Adapter if with List of current + 10 elements.
I would use a Streaming Parser either from GSON or JacksonJSON.
Example from GSON docs:
JsonStreamParser parser = new JsonStreamParser(br); // br is your BufferedReader()
JsonElement element;
synchronized (parser) { // synchronize on an object shared by threads
if (parser.hasNext()) {
element = parser.next();
}
}
I would add extra to do call on listAdapter.notifyDataSetChanged() // ON MAIN THREAD whenever you hit 10. There's obviously other ways you can organize the code.
I have a full blown pagination example here.
There appear to be many ways to accomplish running background code when you reach the end of a listview, the following snippet is what I chose to implement:
lv.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
int threshold = 1;
int count = lv.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (lv.getLastVisiblePosition() >= count - threshold) {
items = (AddItems) new AddItems().execute(count);
}
}
}
});
As for the class AddItem, what I wanted required creating a custom array object, Gift()
public class Gift {
private String descs;
private String itemName;
private String price;
private String imageUrl;
public Gift() {
}
public Gift(String p, String i, String d, String u) {
this.descs = d;
this.itemName = i;
this.price = p;
this.imageUrl = u;
}
public String getDetails() {
return descs;
}
public void setDetails(String details) {
this.descs = details;
}
} // You get the idea
Then I would have to iterate through the file, adding desired strings to their respect ArrayList, and combine all three lists into an ArrayList<Gift> which is shown below.
private class AddItems extends AsyncTask<Integer, Void, ArrayList<Gift>> {
#Override
protected ArrayList<Gift> doInBackground(Integer... integer) {
try {
amountArray = new ArrayList<String>();
itemArray = new ArrayList<String>();
descArray = new ArrayList<String>();
imageUrls = new ArrayList<String>();
finalArray = new ArrayList<Gift>();
File file = new File(Ids.dir, getString(R.string.html_file));
FileInputStream in = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(
in, "UTF-8"));
String line;
int itemNumber = 0;
while ((line = br.readLine()) != null) {
Document doc = Jsoup.parse(line);
Elements titles = doc.select("h4[class=title]");
Elements amounts = doc.select("div[class=price]");
Elements descs = doc.select("p");
Elements urls = doc.select("img[class=gallery-image]");
for (Element price : amounts) {
itemNumber++;
amountArray.add(price.text());
}
for (Element title : titles) {
itemArray.add(title.text());
}
for (Element desc : descs) {
descArray.add(desc.text());
}
for (Element url : urls) {
imageUrls.add(url.attr("src"));
}
// totalShowing is a final integer with a value of 10
// which only iterates through 10 objects plus
// integer[0], which is the total number of items
// shown in the listview.
if (itemNumber == totalShowing + integer[0] + 1) {
break;
}
}
in.close();
br.close();
finalArray = new ArrayList<Gift>();
// Only add to finalArray the items after position integer[0]
// so we don't add items we've already added
for (int i = integer[0]; i < amountArray.size(); i++) {
finalArray.add(new Gift(amountArray.get(i), itemArray
.get(i), descArray.get(i), imageUrls.get(i)));
}
} catch (Exception e) {
e.printStackTrace();
}
return finalArray;
}
#Override
protected void onPostExecute(ArrayList<Gift> result) {
super.onPostExecute(result);
da.addItems(result);
}
}
After that, all I had to do was change my adapter to extend ArrayList<Gift> and add the following code:
public void addItems(ArrayList<Gift> newItems) {
if (null == newItems || newItems.size() <= 0) {
return;
}
if (null == finalArray) {
finalArray = new ArrayList<Gift>();
}
finalArray.addAll(newItems);
notifyDataSetChanged();
}
Related
I'm trying to compare two lists. First list has values from switches of class1 and the other list I'm trying to retreive it from a site (class2). I'm trying to compare those two lists but if statement returns no. For example
I'm checking the soy switch and when I scan a barcode the fetched list has soy in it but text returns no. Help me please!
Here is the code that I wrote.
Class1
public ArrayList<String> soy2= new ArrayList<String>();
Switch soy;
.....................
if (soy.isChecked()) {
checked();
}
else {
textView.setText("blahblah");
}
public void checked() {
soy2.add("Soy");
soy2.add("Σόγια");
soy2.add("soja");
soy2.add("Soybeans");
soy2.add("soybeans");
soy2.add("en:soybeans");
//checkedAllergens.add(soy2);
}
public ArrayList<String> getList() {
return soy2;
}
Class2
public Manage checkd;
String fetchedAllergens=new String();
List<String> fetchedAllergensList = new ArrayList<String>();
.......................
public void test() {
checkd = new Manage();
ArrayList<String> list = checkd.getList();
System.out.println(list);
System.out.println(fetchedAllergensList);
if(fetchedAllergensList.contains(list))
{
testtxt.setText("yes");
}
else
{
testtxt.setText("no");
}
//Test method is calling by a click listener and i get the list from class 2 with the code below
protected String doInBackground(String... strings) {
final String barcode = strings[0];
#Nullable
String allergens = null;
try {
final String jsonStr = Jsoup.connect(
"https://world.openfoodfacts.org/api/v0/product/" + barcode + ".json")
.ignoreContentType(true)
.execute()
.body();
final JSONObject jsonObj = new JSONObject(jsonStr);
if (jsonObj.has("product")) {
JSONObject productNode = jsonObj.getJSONObject("product");
allergens = productNode.getString("allergens");
}
} catch (IOException | JSONException e) {
e.printStackTrace();
}
fetchedAllergens=allergens;
fetchedAllergensList = Arrays.asList(fetchedAllergens.split(","));
System.out.print(fetchedAllergensList);
return allergens;
}
Here if(fetchedAllergensList.contains(list))
.contains() is expecting an element of your list not an arraylist
ex.
fetchedAllergensList.contains("Soy")
If you want to compare two list, try this
listA.containsAll(listB) && listB.containsAll(listA)
compare lists if equal
To check if list2 contains element that is also present in list1
public boolean elementExist(ArrayList<String> list1, ArrayList<String> list2) {
for (int i= 0; i < list2.length() i++) {
if (list1.contains(list2[i])) {
// element list2[i] exist in list1
return true;
}
}
return false;
}
This method will return true if list2 has element that is also in list1
Now in detail. I am writing an App for a dictionary. For that I have created a custom Object called Word. This Word contains the term and its' corresponding translation:
public class Word implements Serializable {
private int _id;
private int wordType;
private String engWordSi;
private String okyWordPl;
private String engWordPl;
private String okyWordSi;
private String engEx;
private String okyEx;
/** Creates a custom class, which allows save a term, it's translation, as well as the plural and example in both
* languages
* #param _id saves the Id of the Word in the Database
* #param engWordSi saves the English translation of the word
* #param okyWordSi saves the Oshikwanyama translation of the word
* #param wordType saves the type of word in form of a number:
* 0 = phrase; 1 = noun; 2 = verb; 3 = adjective; 4 = pronoun; 5 = other
*/
public Word(int _id, int wordType, String engWordSi, String okyWordSi){
this._id = _id;
this.wordType = wordType;
this.engWordSi = engWordSi;
this.okyWordSi = okyWordSi;
}public int get_id(){
return _id;
}
public void set_id(int i){
_id = i;
}
public int getWordType(){
return wordType;
}
public void setWordType(int i){
if(i < 0 || i > 6){
return;
}
wordType = i;
}
public String getEngWordSi(){
return engWordSi;
}
public void setEngWordSi(String word){
engWordSi = word;
}
public String getOkyWordSi(){
return okyWordSi;
}
public void setOkyWordSi(String word){
okyWordSi = word;
}
public String getEngWordPl(){
return engWordPl;
}
public void setEngWordPl(String word){
engWordPl = word;
}
public String getOkyWordPl(){
return okyWordPl;
}
public void setOkyWordPl(String word){
okyWordPl = word;
}
public String getEngEx(){
return engEx;
}
public void setEngEx(String word){
engEx = word;
}
public String getOkyEx(){
return okyEx;
}
public void setOkyEx(String word){
okyEx = word;
}
The data is loaded via a php file connected to the database. The ORDER BY statement only allows me to choose one language to order the results by. But what I need is, that the search term, which might be found in words of both languages, defines if the English word or the Oshikwanyama word is the word which is being arranged in the order. I have check which translation to use in each case. My guess would be to do it in onPostExecute(). The data is returned by the PHP file in the JSON format. From there I put them into an ArrayList.
private class Query extends AsyncTask<Void, Void, Void>{
#Override
protected void onPostExecute(Void aVoid) {
try{
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
Word word = new Word(jsonObject.getInt("_id"), jsonObject.getInt("type"), jsonObject.getString("eng"), jsonObject.getString("oky") );
arrayList.add(word);
Log.d("JSON", word.toString());
}
}catch (JSONException e){
e.printStackTrace();
}
ProgressBar bar = (ProgressBar) findViewById(R.id.bar);
bar.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.SearchButton);
button.setVisibility(View.VISIBLE);
super.onPostExecute(aVoid);
if (arrayList.isEmpty()){
Toast.makeText(SearchActivity.this, "Unfortunately there were no results, the missing word was sent to our developers.", Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(SearchActivity.this, ResultActivity.class);
ItemDetailsWrapper wrapper = new ItemDetailsWrapper(arrayList);
intent.putExtra("results", wrapper);
intent.putExtra("term", searchTerm);
startActivity(intent);
}
InputStream inputStream = null;
String result = "";
#Override
protected void onPreExecute() {
if (arrayList != null){
arrayList.clear();}
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... voids) {
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri.toString());
//httpPost.setEntity(new UrlEncodedFormEntity(null));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
}
catch (UnsupportedEncodingException e1){
e1.printStackTrace();
} catch (ClientProtocolException e2){
e2.printStackTrace();
}catch (IllegalStateException e3){
e3.printStackTrace();
}catch (IOException e4){
e4.printStackTrace();
}try{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null){
stringBuilder.append(line + "\n");
}
inputStream.close();
result = stringBuilder.toString();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}}
I already have a place to check whether the term searched for is contained in the English or Oshikwanyama word. There I use it to define the flag used in the layout.
public class WordAdapter extends ArrayAdapter<Word> {
private Context mContext;
private List<Word> wordList = new ArrayList<>();
private int srcCode;
private String term;
/** uses words and puts them into a list
*
* #param context stores the context of the calling activity
* #param list stores the ArrayList that was passed into the constructor, and which contains the
* content
* #param searchTerm stores the term that was searched for in SearchActivity to later compare it
* to the contents of the Word and to arrange the correct flag for the source
* language
*/
public WordAdapter(#NonNull Context context, ArrayList<Word> list, String searchTerm) {
super(context, 0 , list);
mContext = context;
wordList = list;
term = searchTerm;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null){
listItem = LayoutInflater.from(mContext).inflate(R.layout.list_item,parent,false);}
final Word currentWord = wordList.get(position);
//Add Images for the flags of the countries and the flag
final TextView sourceTerm = (TextView) listItem.findViewById(R.id.searchTerm);
final TextView translationTerm = (TextView) listItem.findViewById(R.id.translationTerm);
ImageView flag = (ImageView) listItem.findViewById(R.id.src_flag);
ImageButton button = (ImageButton) listItem.findViewById(R.id.flag);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), EditorActivity.class);
intent.putExtra("mode", "report");
intent.putExtra("id", currentWord.get_id());
intent.putExtra("source", sourceTerm.getText());
intent.putExtra("trans", translationTerm.getText());
getContext().startActivity(intent);
}
});
if (currentWord.getOkyWordPl() == null){
currentWord.setOkyWordPl("");
}
if (currentWord.getEngWordPl() == null){
currentWord.setEngWordPl("");
}
if (currentWord.getEngWordSi().contains(term) || currentWord.getEngWordPl().contains(term)){
srcCode = 0;
}
if (currentWord.getOkyWordSi().contains(term) || currentWord.getOkyWordPl().contains(term)){
srcCode = 1;
}
if (srcCode == 0){
sourceTerm.setText(currentWord.getEngWordSi());
translationTerm.setText(currentWord.getOkyWordSi());
flag.setImageResource(R.drawable.britain);
}
if (srcCode == 1){
sourceTerm.setText(currentWord.getOkyWordSi());
translationTerm.setText(currentWord.getEngWordSi());
flag.setImageResource(R.drawable.namibia);
}
return listItem;
}}
Thank you in advance. :)
EDIT:
Adding example Words:
1.
EngWordSi: good
OkyWordSi: nawa
wordType: 2
2.
EngWordSi: good morning
OkyWordSi: wa lele po?
wordType: 0
3.
EngWordSi: morning
OkyWordSi: ongula
WordType: 1
what you can do is, sort your object property like below in your adapter,
public WordAdapter(#NonNull Context context, ArrayList<Word> list, String searchTerm) {
super(context, 0 , list);
mContext = context;
wordList = list;
term = searchTerm;
Collections.sort(list, new Comparator<Word>() {
#Override
public int compare(Word word1, word2) {
return word1.getEngWordSi().compareToIgnoreCase(word2.getEngWordSi());
}
});
}
this is helpful to you.
Here is my demo code
public class Main {
static class Word{
String prority1;
#Override
public String toString() {
return "Word{" +
"prority1='" + prority1 + '\'' +
'}';
}
}
public static void main(String[] args) {
ArrayList<Word> words = new ArrayList<>();
Word word;
for (int i = 0; i < 5; i++) {
word =new Word();
word.prority1 = "aaaa"+(5-i);
words.add(word);
System.out.println("wrod "+i+" is "+word);
}
Collections.sort(words, new Comparator<Word>() {
#Override
public int compare(Word o1, Word o2) {
int ret = o1.prority1.compareTo(o2.prority1);
if (ret > 0) {
return 1;
} else if (ret < 0) {
return -1;
}
return 0;
}
});
System.out.println("after sort check words");
for (Word w : words) {
System.out.println("check word:"+w);
}
}
}
and following is console's output:
wrod 0 is Word{prority1='aaaa5'}
wrod 1 is Word{prority1='aaaa4'}
wrod 2 is Word{prority1='aaaa3'}
wrod 3 is Word{prority1='aaaa2'}
wrod 4 is Word{prority1='aaaa1'}
after sort check words
check word:Word{prority1='aaaa1'}
check word:Word{prority1='aaaa2'}
check word:Word{prority1='aaaa3'}
check word:Word{prority1='aaaa4'}
check word:Word{prority1='aaaa5'}
Hope that can help you.
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 am using recyclerview in which I want to fetch more data from server using json.
scenario is something like that :- On first hit I want to display 5 page in list and a show more button below recyclerview is there when user click show more button on second hit display 5 more pages.how can I do that
here is my init():
public void init() {
mProgressBar = (ProgressBar) m_Main.findViewById(R.id.progressBar1);
mProgressBar.setVisibility(View.GONE);
m_showMore = (AppCompatButton) m_Main.findViewById(R.id.show_more);
m_showMore.setBackgroundColor(Color.TRANSPARENT);
m_showMore.setVisibility(View.GONE);
// Getting the string array from strings.xml
m_n_FormImage = new int[]{
R.drawable.amazon,
R.drawable.whatsapp,
R.drawable.zorpia,
R.drawable.path,
R.drawable.app_me,
R.drawable.evernote,
R.drawable.app_me};
m_RecyclerView = (RecyclerView) m_Main.findViewById(R.id.my_recycler_view);//finding id of recyclerview
m_RecyclerView.setItemAnimator(new DefaultItemAnimator());//setting default animation to recyclerview
m_RecyclerView.setHasFixedSize(true);//fixing size of recyclerview
mLayoutManager = new LinearLayoutManager(getActivity());
m_RecyclerView.setLayoutManager(mLayoutManager);//showing odata vertically to user.
sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// increment of record count
sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// increment of last count...
m_Handler = new Handler();
}
public void implementScroll() {// on scroll load more data from server.............
m_RecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
}
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
m_showMore.setVisibility(View.VISIBLE);
m_showMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//change boolean value
m_showMore.setVisibility(View.GONE);
m_n_DefaultRecordCount = m_n_DefaultRecordCount + 5;// increment of record count by 5 on next load data
m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above
sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
new DealNext().execute(m_DealListingURL);// POST DATA TO SERVER TO LOAD MORE DATA......
}
});
} else {
m_showMore.setVisibility(View.GONE);
}
}
});
}
here is my first serevr hit:-
//sending deal data to retreive response from server
public String DealListing(String url, CRegistrationDataStorage login) {
InputStream inputStream = null;
m_oJsonsResponse = new CJsonsResponse();
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("agentCode", m_szMobileNumber);
jsonObject.put("pin", m_szEncryptedPassword);
jsonObject.put("recordcount", sz_RecordCount);
jsonObject.put("lastcountvalue", sz_LastCount);
//jsonObject.put("emailId", "nirajk1190#gmail.com");
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
// 9. receive response as inputStream
inputStream = entity.getContent();
System.out.println("InputStream....:" + inputStream.toString());
System.out.println("Response....:" + httpResponse.toString());
StatusLine statusLine = httpResponse.getStatusLine();
System.out.println("statusLine......:" + statusLine.toString());
////Log.d("resp_body", resp_body.toString());
int statusCode = statusLine.getStatusCode();
// 10. convert inputstream to string
if (statusCode == 200) {
// 10. convert inputstream to string
if (inputStream != null)
s_szresult = m_oJsonsResponse.convertInputStreamToString(inputStream);
//String resp_body =
EntityUtils.toString(httpResponse.getEntity());
} else
s_szresult = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
System.out.println("resul.....:" + s_szresult);
// 11. return s_szResult
return s_szresult;
}
public void getResponse() throws JSONException {// getting response from serevr ..................
if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Transaction Successful")) {// server based condition
m_oAdapter = new CDealAppListingAdapter(s_oDataset);// create adapter object and add arraylist to adapter
m_oAdapter.notifyDataSetChanged();
m_RecyclerView.setAdapter(m_oAdapter);//adding adapter to recyclerview
} else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions
CToastMessage.getInstance().showToast(getActivity(), "Connection not avaliable");
} else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions .....
CToastMessage.getInstance().showToast(getActivity(), "No More Deals Available");
}
}
// sending deal data to server and retreive response......
class CDealDataSent extends AsyncTask<String, Void, String> {
public CRegistrationDataStorage oRegisterStorage;
public CDealAppDatastorage item;
#Override
protected void onPreExecute() {
super.onPreExecute();
CProgressBar.getInstance().showProgressBar(getActivity(), "Please wait while Loading Deals...");
}
#Override
protected String doInBackground(String... urls) {
return DealListing(urls[0], oRegisterStorage);// sending data to server...
}
// onPostExecute displays the results of the AsyncTask.
#Override
protected void onPostExecute(final String result) {
new Thread(new Runnable() {
#Override
public void run() {
m_Handler.post(new Runnable() {
#Override
public void run() {
CProgressBar.getInstance().hideProgressBar();// hide progress bar after getting response from server.......
try {
m_oResponseobject = new JSONObject(result);// getting response from server
JSONArray posts = m_oResponseobject.optJSONArray("dealList");
s_oDataset = new ArrayList<CDealAppDatastorage>();
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.getJSONObject(i);
item = new CDealAppDatastorage();
item.setM_szHeaderText(post.getString("dealname"));
item.setM_szsubHeaderText(post.getString("dealcode"));
item.setM_n_Image(m_n_FormImage[i]);
s_oDataset.add(item);
}
getResponse();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}).start();
}
}
here is my second serevr hit
// sending data and receive reponse on second hit T LOAD MORE DATA when show more Btn clicked..............
private class DealNext extends AsyncTask<String, Void, String> {
public CRegistrationDataStorage oRegisterStorage;
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);// SHOW PROGRESS BAR
}
#Override
protected String doInBackground(String... urls) {
//My Background tasks are written here
synchronized (this) {
return DealListing(urls[0], oRegisterStorage);// POST DATA TO SERVER
}
}
#Override
protected void onPostExecute(final String result) {
super.onPostExecute(result);
new Thread(new Runnable() {
#Override
public void run() {
m_Handler.post(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.INVISIBLE);// DISMISS PROGRESS BAR..........
try {
m_oResponseobject = new JSONObject(result);// getting response from server
final JSONArray posts = m_oResponseobject.optJSONArray("dealList");// GETTING DEAL LIST
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
item = new CDealAppDatastorage();// object create of DealAppdatastorage
item.setM_szHeaderText(post.getString("dealname"));//getting deal name
item.setM_szsubHeaderText(post.getString("dealcode"));// getting deal code
item.setM_n_Image(m_n_FormImage[i]);// static image for testing purpose not original....
s_oDataset.add(item);// add items to arraylist....
m_oAdapter.notifyItemInserted(s_oDataset.size());// notify adapter when deal added to recylerview
}
getResponse();// getting response from server.....and also here response based logics...
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}).start();
}
}
here is my adapter class:
public class CDealAppListingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static CDealAppDatastorage list;
private static ArrayList<CDealAppDatastorage> s_oDataset;
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
public CDealAppListingAdapter(ArrayList<CDealAppDatastorage> mDataList) {
s_oDataset = mDataList;
}
#Override
public int getItemViewType(int position) {
return s_oDataset.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
if (i == VIEW_TYPE_ITEM) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.deallisting_card_view, viewGroup, false);
return new DealAppViewHolder(view);
} else if (i == VIEW_TYPE_LOADING) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_loading_item, viewGroup, false);
return new LoadingViewHolder(view);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof DealAppViewHolder) {
list = s_oDataset.get(position);// receiving item on position on index i
DealAppViewHolder dealAppViewHolder = (DealAppViewHolder) holder;
dealAppViewHolder.s_szAppImage.setImageResource(list.getM_n_Image());
dealAppViewHolder.s_szheadingText.setText(list.getM_szHeaderText());
dealAppViewHolder.s_szSubHeader.setText(list.getM_szsubHeaderText());
} else if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true);
}
}
#Override
public int getItemCount() {
return (s_oDataset == null ? 0 : s_oDataset.size());//counting size of odata in ArrayList
}
static class LoadingViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public LoadingViewHolder(View itemView) {
super(itemView);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar1);
}
}
public static class DealAppViewHolder extends RecyclerView.ViewHolder {
public static ImageView s_szAppImage;
public static TextView s_szheadingText, s_szSubHeader;
public static Button s_szGetDealBtn;
public DealAppViewHolder(View itemLayoutView) {
super(itemLayoutView);
s_szheadingText = (TextView) itemLayoutView.findViewById(R.id.headingText);// finding id of headerText...
s_szSubHeader = (TextView) itemLayoutView.findViewById(R.id.subHeaderText);// finding id of subHeader.....
s_szAppImage = (ImageView) itemLayoutView.findViewById(R.id.appImage);//finding Id of Imgae in CardView
s_szGetDealBtn = (Button) itemLayoutView.findViewById(R.id.getDealBtn);// finding id of getdeal Btn
Random rnd = new Random();//creating object of Random class
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));//genrating random color
s_szGetDealBtn.setBackgroundColor(color);//backgraound color of getDeal Btn
s_szGetDealBtn.setOnClickListener(new View.OnClickListener() {// onclick getDeal Btn
#Override
public void onClick(View v) {//send to deal detail page onclick getDeal Btn
Intent i = new Intent(v.getContext(), CDealAppListingDetails.class);
i.putExtra("DealCode", s_oDataset.get(getPosition()).getM_szsubHeaderText());
i.putExtra("headerText", s_oDataset.get(getPosition()).getM_szHeaderText());
v.getContext().startActivity(i);
}
});
itemLayoutView.setOnClickListener(new View.OnClickListener() {// onclick cardview
#Override
public void onClick(View v) {// onclick cardview send to deal app listing details page .....
Intent i = new Intent(v.getContext(), CDealAppListingDetails.class);
i.putExtra("DealCode", list.getM_szsubHeaderText());
i.putExtra("headerText", list.getM_szHeaderText());
v.getContext().startActivity(i);
}
});
}
}
}
Either store two lists in your adapter, the currently displayed list and the full list, or get only the content you need if your API allows it.
You can then set your adapter size in getItemCount to be your displayed list size + 1 (for the view more items). You will need to add a separate view type to return in getItemViewType for your view more cell (the logic to display it would be if the position is currently displayed list size). You will also need to add a new view and view holder for the load more cell.
After you've set up your new view more cell you can simply add a click listener to it, if clicked add 5 items from your full list into your currently displayed list (making sure to start at the currently displayed list - 1 index of the full list, and call notifyDataSetChanged, or notifyItemAdded x 5.
I'm sorry I currently don't have time to write an appropriate adapter for you but I hope the above explanation will suffice.
I have a problem. I have multiple records that after SAX parsing from a xml file that needs to be inserted into the Hashtable and displayed in a listview. I am able to display the data in the listview but all it gives is the same data for all rows in the listview. I think it has something to do with either my coding for the hashtable or my SAX parsing.
This is the xml file I'm trying to retrieve data from online: http://spark.opac.tp.edu.sg/X?op=present&set_no=007584&set_entry=000000001,000000002,000000003,000000004,000000005&format=marc
Below is my handler where i place my data into the hashtable:
ArrayList<Hashtable<String,String>> list = new ArrayList<Hashtable<String,String>>();
Hashtable<String,String> data = new Hashtable<String,String>();
private final String MY_DEBUG_TAG = "Debugging~";
private Boolean in_record = false;
private Boolean in_author = false;
private Boolean in_format = false;
private Boolean in_title = false;
private Boolean in_callNumber = false;
private String format = "";
private String title = "";
private String author = "";
private String callNumber = "";
//private SearchList sList;
//public SearchList getListData() {
// return this.sList;
//}
#Override
public void startDocument() throws SAXException {
// this.sList = new SearchList();
}
#Override
public void endDocument() throws SAXException {
// Nothing to do
}
/** Called when tag starts ( ex:- <name>AndroidPeople</name>
* -- <name> )*/
#Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(localName.equalsIgnoreCase("record")) {
this.in_record = true;
}
else
if (localName.equalsIgnoreCase("fixfield")) {
if (attributes.getValue("id").equalsIgnoreCase("FMT")) {
this.in_format = true;
}
}
else
if(localName.equalsIgnoreCase("varfield")) {
if(attributes.getValue("id").equalsIgnoreCase("100")) {
this.in_author = true;
}
else
if(attributes.getValue("id").equalsIgnoreCase("245")) {
this.in_title = true;
}
else
if(attributes.getValue("id").equalsIgnoreCase("099")) {
this.in_callNumber = true;
}
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
StringBuffer sb = new StringBuffer();
String finalString;
String originalString = "";
String chars = new String(ch, start, length);
chars = chars.trim();
if (this.in_format) {
sb.append(chars);
finalString = sb.toString();
this.format = finalString;
}
else
if (this.in_author) {
if(!(chars.equals(""))) {
sb.append(chars);
finalString = sb.toString();
originalString = this.author + finalString;
this.author = originalString;
}
}
else
if (this.in_title) {
if(!(chars.equals(""))) {
sb.append(chars);
finalString = sb.toString();
originalString = this.title + finalString;
this.title = originalString;
}
}
else
if (this.in_callNumber) {
if(!(chars.equals(""))) {
sb.append(chars);
finalString = sb.toString();
this.callNumber = finalString;
}
}
}
#Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("record")) {
this.in_record=false;
}
else
if (localName.equalsIgnoreCase("fixfield")) {
if (this.in_format) {
//set the foundCharacter to Hashtable
data.put("format", this.format);
//sList.setFormat(this.format);
Log.d(MY_DEBUG_TAG,"Format = " + this.format);
this.in_format = false;
}
}
else
if(localName.equalsIgnoreCase("varfield")) {
if (this.in_author) {
//set the foundCharacter to Hashtable
data.put("author", this.author);
//sList.setAuthor(this.author);
Log.d(MY_DEBUG_TAG,"Author = " + this.author);
this.in_author = false;
}
else
if (this.in_title) {
//set the foundCharacter to Hashtable
data.put("title", this.title);
//sList.setTitle(this.title);
Log.d(MY_DEBUG_TAG,"Title = " + this.title);
this.in_title = false;
}
else
if (this.in_callNumber) {
//set the foundCharacter to Hashtable
data.put("callNumber", this.callNumber);
//sList.setCallNumber(this.callNumber);
Log.d(MY_DEBUG_TAG,"Call Number = " + this.callNumber);
this.in_callNumber = false;
}
}
//add the hashtable into ArrayList
list.add(data);
}
Any suggestions guys?
I don't have time to read completly your code ...
But this is a simple example :
ListView myListView;
myListView = (ListView) findViewById(R.id.mylistview);
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
//load your data
String[][] items = database.getItems("Blog");
//check if the database was empty
if(items != null){
for(int i = 0; i < items[0].length; i++) {
map = new HashMap<String, String>();
map.put("pubdate", items[2][0]);
map.put("title", items[0][i]);
map.put("description", items[1][i]);
listItem.add(map);
}
//Creation of a SimpleAdapter to put items in your list (listitems) in your listview
// You need to have a xml file called list_full_news_item with elements
// called android:id="#+id/title" etc.
SimpleAdapter mSimpleAdaptor = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list_full_news_item,
new String[] {"pubdate", "title", "description"}, new int[] {R.id.itemPubdate, R.id.itemTitle, R.id.itemDescription});
//Assign to the listView the created adapter
myListView.setAdapter(mSimpleAdaptor);
}
Hope this will help you to understand.
You should not use the SAX Parser and use the Simple XML Library instead. It has the #ElementMap annotation for exactly this purpose. This turns your entire problem into one annotation and three lines of code to read the XML in. Look at my blog post on including the library in an android project and look at the Simple tutorial if you want to see how to use all of its cool features.
Edit: I just realised that I have answered this question before in one of your previous questions. I'm done trying to convince you to look into Simple XML; but please, stop posting essentially the exact same question in five different ways here; people have helped you already, give you answers and lead you in the right direction. It is now time to read more, experiment and learn.