I am displaying multiple rows in one header and there is 5 more categories like drama and I am doing the same job for them. My question is when I set the adapter categories displaying randomly.(assuming of request speed).For example I want the display Drama on the first line always. I gave them id of adapter's constructor but didn't work out. Code :
if (response.body().getItems().size() > 0) {
Log.d("Drama",""+response.body().getItems().size());
List<MovieItem> newMovieItems = response.body().getItems();
HeaderItem liveHeader = new HeaderItem("Drama");
LiveChannelPresenter liveChannelPresenter = new LiveChannelPresenter();
ArrayObjectAdapter liveRowAdapter = new ArrayObjectAdapter(liveChannelPresenter);
for (int i = 0; i < newMovieItems.size(); i++) {
movieItem2 = response.body().getItems().get(i);
liveRowAdapter.add(movieItem2);
}
mRowsAdapter.add(new ListRow(0,liveHeader,liveRowAdapter));
}
}
if (response.body().getItems().size() > 0) {
Log.d("Drama",""+response.body().getItems().size());
List<MovieItem> newMovieItems = response.body().getItems();
//sorted:
Arrays.sort(newMoviewItems, new Comparator() { some condition to sort });
HeaderItem liveHeader = new HeaderItem("Drama");
LiveChannelPresenter liveChannelPresenter = new LiveChannelPresenter();
ArrayObjectAdapter liveRowAdapter = new ArrayObjectAdapter(liveChannelPresenter);
for (int i = 0; i < newMovieItems.size(); i++) {
//note the change here \|/
movieItem2 = newMoviewItems.get(i);
liveRowAdapter.add(movieItem2);
}
mRowsAdapter.add(new ListRow(0,liveHeader,liveRowAdapter));
}
Related
I am trying to integrate highcharts in my android app (inside fragment) but I am getting this error every time:
java.util.NoSuchElementException: HIOptions not found in HIChartView
Here is my onCreateView Method:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
parentView = inflater.inflate(R.layout.bar_layout, null);
return parentView;
}
How I am getting chart view:
chartView = parentView.findViewById(R.id.hc);
I am calling chart API to get to data after getting a response, I am setting options but it is giving me the same error, below is the code snippets how am I setting chart:
HIOptions options = new HIOptions();
HIChart chart = new HIChart();
chart.setType("column");
options.setChart(chart);
HIExporting hiExporting = new HIExporting();
hiExporting.setEnabled(false);
options.setExporting(hiExporting);
List<BarChartSeries> seriesList = barChartModel.getSeries();
if (!seriesList.isEmpty()) {
final HIXAxis xAxis = new HIXAxis();
HITitle xAxisTitle = new HITitle();
xAxisTitle.setText(getString(R.string.year));
xAxis.setTitle(xAxisTitle);
XAxisDto xAxisDto = barChartModel.getxAxis();
List<String> stringList1 = xAxisDto.getCategories();
ArrayList<String> xAxisList = new ArrayList<>();
xAxisList.addAll(stringList1);
xAxis.setCategories(xAxisList);
xAxis.setCrosshair(new HICrosshair());
ArrayList<HIXAxis> axes = new ArrayList<>();
axes.add(xAxis);
options.setXAxis(axes);
HIYAxis yAxis = new HIYAxis();
yAxis.setMin(0);
HITitle yAxisTitle = new HITitle();
yAxisTitle.setText(getString(R.string.no_of_cases));
yAxis.setTitle(yAxisTitle);
ArrayList<HIYAxis> yaxes = new ArrayList<>();
yaxes.add(yAxis);
options.setYAxis(yaxes);
HITooltip tooltip = new HITooltip();
tooltip.setHeaderFormat("<span style=\"font-size:10px\">{point.key}</span><table>");
tooltip.setPointFormat("<span style=\"font-size:10px\">{point.key}</span><table>");
tooltip.setFooterFormat("</table>");
tooltip.setShared(true);
tooltip.setUseHTML(true);
options.setTooltip(tooltip);
HIPlotOptions plotOptions = new HIPlotOptions();
HIColumn hiColumn = new HIColumn();
hiColumn.setPointPadding(0.2);
hiColumn.setBorderWidth(0);
plotOptions.setColumn(hiColumn);
options.setPlotOptions(plotOptions);
ArrayList<HISeries> dataSetList = new ArrayList<>();
if (seriesList.size() > 0) {
for (int i = 0; i < seriesList.size(); i++) {
BarChartSeries barChartSeries = seriesList.get(i);
String name = barChartSeries.getName();
List<Data> dataList = barChartSeries.getData();
HIColumn series1 = new HIColumn();
series1.setName(name);
List<String> stringList = xAxisDto.getCategories();
ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
ArrayList<Long> doubleArrayList = new ArrayList<>();
if (stringList != null) {
for (int j = 0; j < stringList.size(); j++) {
String cat = stringList.get(j);
int count = 0;
for (int ij = 0; ij < dataList.size(); ij++) {
Data data = dataList.get(ij);
if (data.getYear().equalsIgnoreCase(cat)) {
count++;
yVals.add(new BarEntry(ij, data.getCount()));
doubleArrayList.add(data.getCount());
break;
}
}
if (count == 0) {
yVals.add(new BarEntry(yVals.size() + 1, 0));
doubleArrayList.add(0l);
}
}
}
series1.setData(doubleArrayList);
dataSetList.add(series1);
}
}
options.setSeries(dataSetList);
ArrayList<HIColor> hiColors = new ArrayList<>();
hiColors.add(HIColor.initWithHexValue("FF0000"));
hiColors.add(HIColor.initWithHexValue("FFA500"));
hiColors.add(HIColor.initWithHexValue("808080"));
options.setColors(hiColors);
HITitle title = new HITitle();
title.setText("");
options.setTitle(title);
HISubtitle subtitle = new HISubtitle();
subtitle.setText("");
options.setSubtitle(subtitle);
HICredits hiCredits = new HICredits();
hiCredits.setEnabled(false);
options.setCredits(hiCredits);
chartView.setOptions(options);
What am I missing here? Or How to resolve this issue?
I am able to solve the issue.
Follow below steps to resolve the issues:
When you fragment view is visible
Get highchart Id
chartView = parentView.findViewById(R.id.hc);
Set empty options to chartView:
HIOptions options = new HIOptions();
chartView.setOptions(options);
Once you get the data from your API set options to chartView again and call reload method:
chartView.setOptions(options);
chartView.reload();
Add the dependency again
implementation 'com.highsoft.highcharts:highcharts:7.0.3'
in the build.gradle file(app)
In my case It worked Well
I have been using realm about more than a years, in many cases realm is good choice to construct Android database instead using SQLite. I have been using realm for one-one, one-many relationship and good for it. But, a moment a good. I discover realm NOT update my existing data in ONE-MANY relationship. This what i have done.
This code for populate skeleton data or a unit test
private void initData() {
List<PackModel> packs = new ArrayList<>();
for (int i = 0; i < 1; i++) {
PackModel packModel = new PackModel();
packModel.id = i;
packModel.name = "Pack " + new Random().nextInt(100);
List<FoodModel> foods = new ArrayList<>();
for (int j = 0; j < 3; j++) {
FoodModel foodModel = new FoodModel();
foodModel.id = 0; // i set primary key 0, so in list should be 3 but when insert should be JUST one
foodModel.packId = i;
foodModel.name = "Food " + new Random().nextInt(100);
foodModel.stock = new Random().nextInt(100);
foodModel.price = new Random().nextInt(100);
foodModel.image = "hrrp";
foodModel.calorie = new Random().nextInt(100);
foodModel.createTime = System.nanoTime();
foods.add(foodModel);
}
packModel.foods.addAll(foods);
packs.add(packModel);
}
insertdb(packs);
}
As you see a List of PackModel, and each PackModel have their food items.
So, i write in Realm like below code.
private void insertdb(final List<PackModel> items) {
new AsyncTask<Void, Void, List<PackObject>>() {
#Override
protected List<PackObject> doInBackground(Void... params) {
final List<PackObject> packsObject = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
PackObject packObject = new PackObject();
packObject.id = items.get(i).id;
packObject.name = items.get(i).name;
List<FoodObject> foodsObject = new ArrayList<>();
for (int j = 0; j < items.get(i).foods.size(); j++) {
FoodObject foodObject = new FoodObject();
foodObject.id = items.get(i).foods.get(j).id;
foodObject.name = items.get(i).foods.get(j).name;
foodObject.createdTime = items.get(i).foods.get(j).createTime;
foodsObject.add(foodObject);
}
packObject.foods.addAll(foodsObject);
packsObject.add(packObject);
}
Realm realm = Realm.getInstance(RealmApplication.getRealmConfiguration());
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(packsObject);
}
});
return realm.copyFromRealm(realm.where(PackObject.class).findAll());
}
#Override
protected void onPostExecute(List<PackObject> items) {
super.onPostExecute(items);
Log.d("Test", "");
}
}.execute();
}
I got multiple FoodObject in each PackObject, what i am wrong here? i also use realm.beginTransaction. realm.beginTransaction is just a same like execute*, the difference just execute* are thread safe.
i also use realm.insertOrUpdate(obj), but just have same result.
NOTE: I have read realm documentation about it. So dont judge me not read their documentation.
this my repot for the code https://github.com/radityagumay/realm-bug
Thanks
I'm building an Android TV app for which I've rewritten the loadRows() method as such:
private void loadRows(String[] CATEGORY_LIST) {
mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
int i;
for (i = 0; i < NUM_ROWS; i++) {
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new ListRowPresenter());
HeaderItem header = new HeaderItem(i, CATEGORY_LIST[i]);
mRowsAdapter.add(new ListRow(header, listRowAdapter));
}
setAdapter(mRowsAdapter);
}
My problem is that the CATEGORY_LIST is being drawn twice: once when the app is launched, and once on a right arrow press.
When it is drawn on the right arrow press, the spaces between the items are a little bit bigger, so I assume that is a "feature" from ArrayObjectAdapter.
HeaderItem header1 = new HeaderItem(1, getString(R.string.pontos_title));
ArrayObjectAdapter listRowAdapter1 = new ArrayObjectAdapter(cardPresenter);
for (int z = 0; z < pontos.size() ; z++) {
listRowAdapter1.add(pontos.get(z));
}
mRowsAdapter.add(new ListRow(header1,listRowAdapter1));
ObjectAdapter mAdapter = getAdapter();
mAdapter.notifyItemRangeChanged(1, NewObjects.size()); //Line 1
If you want to update the item on the same line use, but remove first.
mRowsAdapter.removeItems(1, LastedObjects.size());// Line 1
Below is Simple declaration in this a child and parent is being build with dynamic list value.With relevant code as createGroupList() and createChildList(). I am able to manipulate with TextView but one of field is my dynamic image from getting to JSON not is being load in WebView.
expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), // Creating group List.
R.layout.group_row,
new String[] { "Group Item" },
new int[] { R.id.`enter code here`row_name },
createChildList(), // Creating Child List.
R.layout.child_row,
new String[] { "Sub Item0", "Sub Item1", "Sub Item2", "Sub Item3" },
new int[] { R.id.grpChildName, R.id.grpChildAddress, R.id.grpChildPhoneNo, R.id.childImage }
);
setListAdapter(expListAdapter);
private List createGroupList() {
String header = "";
int headerSize = 0;
ArrayList result = new ArrayList();
for (int i = 0; i < dynamicList.size(); i++) {
HashMap m = new HashMap();
String taxiCompanyData[] = dynamicList.get(i);
if (header != taxiCompanyData[3]) {
m.put("Group Item", taxiCompanyData[3]);
header = taxiCompanyData[3];
headerList[headerSize] = header;
headerSize++;
result.add(m);
counter++;
}
}
return (List) result;
}
Above is Group creating and
private List createChildList() {
ArrayList result = new ArrayList();
Log.i("Info ", "headerList.length" + headerList.length);
for (int i = 0; i < headerList.length; i++) {
final ArrayList secList = new ArrayList();
System.out.println("dynamic list size is in main"
+ dynamicList.size());
webView = (WebView) findViewById(R.id.childImage);
for (int j = 0; j < dynamicList.size(); j++) {
String taxiCompanyData[] = dynamicList.get(j);
if (taxiCompanyData[3].equalsIgnoreCase(headerList[i])) {
final HashMap child = new HashMap();
child.put("Sub Item0", taxiCompanyData[0]);
child.put("Sub Item1", taxiCompanyData[1]);
child.put("Sub Item2", taxiCompanyData[2]);
// child.put("Sub Item3", taxiCompanyData[4]);
Thread th = new Thread(){
#Override
public void run() {
webView.loadUrl("https://lh5.googleusercontent.com/-u8heQyD_4y4/T5VMu-Zc6wI/AAAAAAAi71s/EP32a3D-fc0/s90/Corsino");
secList.add(child);
};
};
th.start();
}
}
result.add(secList);
}
return (List)result;
}
I am unable to load WebUI. Please help anyone.
Hi can some one suggest me a sample example of how i can sort the textviews based on the numbers in textviews. I am able to get the text from the TextViews need to sort and place the lowest number first.
Thank you.
public void sortNumbers(View v) {
String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
for (int i = 0; i < intValues.length; i++) {
Integer intValue = intValues[i];
//here I want to assign sorted numberes to the TextViews
}
}
So I have followed Jeffrey's advice. Here is the code which still doesn't work properly. What could be wrong?
Created an array of TextViews:
TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
tvs[2] = textView43;
tvs[3] = textView53;
tvs[4] = textView63;
tvs[5] = textView73;
tvs[6] = textView83;
Sorted the array and assinged new values to the TextViews:
Arrays.sort(tvs, new TVTextComparator());
textView23.setText(tvs[0].getText().toString());
textView33.setText(tvs[1].getText().toString());
textView43.setText(tvs[2].getText().toString());
textView53.setText(tvs[3].getText().toString());
textView63.setText(tvs[4].getText().toString());
textView73.setText(tvs[5].getText().toString());
textView83.setText(tvs[6].getText().toString());
And here is the Comporator class:
public class TVTextComparator implements Comparator<TextView> {
public int compare(TextView lhs, TextView rhs) {
Integer oneInt = Integer.parseInt(lhs.getText().toString());
Integer twoInt = Integer.parseInt(rhs.getText().toString());
return oneInt.compareTo(twoInt);
}
}
to sort your textViews, first put them in an array,
TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
// and so on
note that if you have handle to the parent container, you could easily build the array by using ViewGroup.getChildCount() and getChildAt().
now write a comparator for a text view,
class TVTextComparator implements Comparator<TextView> {
#Override
public int compare(TextView lhs, TextView rhs) {
return lhs.getText().toString().compareTo(rhs.getText().toString());
// should check for nulls here, this is NOT a robust impl of compare()
}
}
now use the comparator to sort the array,
Arrays.sort(tvs, 0, tvs.length, new TVTextComparator());
public void sortNumbers(View v) {
String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
textView23.setText(intValues[0]);
textView33.setText(intValues[1]);
textView43.setText(intValues[2]);
textView53.setText(intValues[3]);
textView63.setText(intValues[4]);
textView73.setText(intValues[5]);
textView83.setText(intValues[6]);
}