ArrayObjectAdapter drawing the items twice (Android TV) - android

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

Related

How to put ArrayObjectAdapter items in order

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));
}

HighChart Libraries Android: HIOptions not found in HIChartView

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

No data appears on BarChart for X entries when you use y-values as floats

I have tried to add a BarChart in my Code, nothing appear when I launch my app.
The purpose of my code is that: when the user enter the start date (StartDate), end date (EndDate) and the product name, the graph should show the show price evolution during that period.
I have tried the code below, but nothing appear on the graph.
Please help to find the issue, or propose to me another code.
public class ResultProductStatisticsActivity extends AppCompatActivity {
private String mCategory;
private String mName;
private long mStartDate;
private long mEndDate;
private int mNumber = 0;
private ProgressBar mProgressBar;
private ArrayList<ShopDetail> mDetail = new ArrayList<ShopDetail>();
private ArrayList<Float> mPriceList = new ArrayList<Float>();
protected Typeface mTfLight;
private Typeface mTypeFaceLight;
private TextView mMontextViewtest;
private BarChart mChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.result_product_statistics_activity);
mChart = (BarChart) findViewById(R.id.m_barchart);
// mChart.setOnChartValueSelectedListener(this);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.setDescription("");
// if more than 60 entries are displayed in the chart, no values will be
// drawn
mChart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
// mChart.setDrawYLabels(false);
AxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mChart);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTypeface(mTfLight);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f); // only intervals of 1 day
xAxis.setLabelCount(7);
xAxis.setValueFormatter(xAxisFormatter);
AxisValueFormatter custom = new MyAxisValueFormatter();
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTypeface(mTfLight);
leftAxis.setLabelCount(8, false);
leftAxis.setValueFormatter(custom);
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setSpaceTop(15f);
leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setTypeface(mTfLight);
rightAxis.setLabelCount(8, false);
rightAxis.setValueFormatter(custom);
rightAxis.setSpaceTop(15f);
rightAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
l.setForm(Legend.LegendForm.SQUARE);
l.setFormSize(9f);
l.setTextSize(11f);
l.setXEntrySpace(4f);
// l.setExtra(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
// "def", "ghj", "ikl", "mno" });
// l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
// "def", "ghj", "ikl", "mno" });
init();
mChart.setMarkerView(new XYMarkerView(this, xAxisFormatter));
}
private void init(){
mCategory = Myrecord.getSearchShopCategory(this);
mName = Myrecord.getSearchShopName(this);
mStartDate = Myrecord.getSearchStartTime(this);
mEndDate = Myrecord.getSearchEndTime(this);
if(mStartDate == -1){
mStartDate = Long.MIN_VALUE;
}
if(mEndDate == -1){
mEndDate = Long.MAX_VALUE;
}
Thread thread = new Thread(){
#Override
public void run() {
MyDB db = new MyDB(ResultProductStatisticsActivity.this);
ArrayList<MyDetail> detailList = db.getMyDetailByAll(mCategory, mName, -1, 0, -1);
for(int i = 0; i < detailList.size(); i ++){
MyDetail detail = detailList.get(i);
long date = detail.date;
if(date >= mStartDate && date <= mEndDate){
mPriceList.add(db.getPriceGoodsByOwnName("ALEVONS")+0f);
}
}
ResultProductStatisticsActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
setData(mPriceList);
}
});
}
};
thread.start();
}
private void setData(ArrayList<Float> mPriceList) {
mChart.getXAxis().setAxisMinValue(mStartDate);
mChart.getXAxis().setAxisMaxValue(mEndDate);
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < mPriceList.size(); i++) {
yVals1.add(new BarEntry(i , mPriceList.get(i)));
}
BarDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
set1.setValues(yVals1);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(yVals1, "Evolution du prix produit");
set1.setColors(ColorTemplate.MATERIAL_COLORS);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setValueTypeface(mTfLight);
data.setBarWidth(0.9f);
mChart.setData(data);
}
}
}
A wild guess, without any more details provided: your're setting the x-axis min and max values to the start and end date. I'm guessing this is encoded in unix time, so you have something like 1470614400 and 1471219200.
Now looking at your data, it will start with 0 and go up to the number of prices you have in your price list (mPriceList.size()). So with 10 prices we'd have values at 0 through 9. However, since your x-axis starts at 1470614400, no data will be shown (although the axis, gridlines etc. should be visible).
So in order to fix your code (depending on how your price list is structured, this example assumes one price every hour), you'd need to change your code in setData to this:
for (int i = 0; i < mPriceList.size(); i++) {
yVals1.add(new BarEntry(mStartDate + i * 3600, mPriceList.get(i)));
}
If you have a list of dates associated with the price values, you can also simply do this:
for (int i = 0; i < mPriceList.size(); i++) {
yVals1.add(new BarEntry(mDateList.get(i), mPriceList.get(i)));
}
We the reply of(#TR4Android),
I have update my code such below and I can get the graph:
float start = 0f;
mChart.getXAxis().setAxisMinValue(start);
mChart.getXAxis().setAxisMaxValue(mPriceList.size());
for (int i = 0; i < mPriceList.size(); i++) {
yVals1.add(new BarEntry(i , mPriceList.get(i)));
}
But I have a bit issue with start date end date where each item has been bought. I have put each date in array list but I don't see how I can put it on xAxis. The Array list is :
private ArrayList<Long> mDateList = new ArrayList<Long>();
One example of mDateList value is 2016/08/13

How do I refactor android code to choose layout?

I have 1 top layout and 3 bottom layouts, a user drags items from top layout to one of the 3 below.
The code below:
puts sequentially every image in every bottom layout into a list
removes items from each bottom layout
puts them back into top layout
public void onClick(View view) {
LinearLayout bottomLinearLayout1 = (LinearLayout) findViewById(R.id.bottom1);
LinearLayout bottomLinearLayout2 = (LinearLayout) findViewById(R.id.bottom2);
LinearLayout bottomLinearLayout3 = (LinearLayout) findViewById(R.id.bottom3);
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
int total_num1 = bottomLinearLayout1.getChildCount();
int total_num2 = bottomLinearLayout2.getChildCount();
int total_num3 = bottomLinearLayout3.getChildCount();
View current_image = null;
List<View> listOfkids = new ArrayList<>() ;
//************ REPEATS **************
for(int i = 0 ; i < total_num1 ; i++){
current_image = bottomLinearLayout1.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout1.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
listOfkids.clear();
//************ REPEATS **************
for(int i = 0 ; i < total_num2 ; i++){
current_image = bottomLinearLayout2.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout2.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
listOfkids.clear();
//************ REPEATS **************
for(int i = 0 ; i < total_num3 ; i++){
current_image = bottomLinearLayout3.getChildAt(i);
listOfkids.add(current_image);
}
bottomLinearLayout3.removeAllViews();
for(int i = 0 ; i < listOfkids.size();i++){
topLinearLayout.addView( listOfkids.get(i));
}
Basically, the only difference between these loops is the last digit in "bottomLinearLayout"; the other code just copies itself!
Can i do this:
if(id == R.id.bottom1){
String current_layout = "bottomLinearLayout" + 1 ;
}
else if( id == R.id.bottom2){
current_layout = "bottomLinearLayout" + 2 ;
}
else if( id == R.id.bottom3){
current_layout = "bottomLinearLayout" + 3 ;
}
and then add this string as a command right into the java source code?
is this possible?
Assuming that you want to refactor the onClick method, you could create an array of the layout ids and iterate through them as follows:
int bottomLayoutIds[] = {R.id.bottom1, R.id.bottom2, R.id.bottom3};
LinearLayout topLinearLayout = (LinearLayout) findViewById(R.id.topLinear);
for (int layoutId : bottomLayoutIds){
LinearLayout bottomLinearLayout = (LinearLayout) findViewById(layoutId);
int childCount = bottomLinearLayout.getChildCount();
List<View> listOfKids = new ArrayList<>() ;
for(int i = 0 ; i < childCount ; i++){
View currentImage = bottomLinearLayout.getChildAt(i);
listOfKids.add(currentImage);
}
bottomLinearLayout.removeAllViews();
for(int i = 0 ; i < listOfKids.size();i++){
topLinearLayout.addView( listOfKids.get(i));
}
}

Android: Use WebView at child in SimpleExpandableListAdapter

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.

Categories

Resources