Androidplot chart is duplicating label values x-axis & y-axis - java

I am using androidplot to show a graph, the labels get repeated. i searched forums and stackoverflow haven't found any solution.
now i noticed, if arraysize for x-axis or y-axis is less than 10, the array values get duplicated.
The screenshots show the left axis repeating the value 0,1,2. x-axis values are getting repeated. What is your best suggestion on solving this repeating label issue? Any help would be grateful. If you need any further information let me know.
/**
* A simple XYPlot
*/
public class SimpleXYPlotActivity extends Activity {
private XYPlot plot;
String[] domainLabels;
String oldDateStr, newDateStr;
Date newDate;
int totalDays;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.graph_xy_plot_example);
// initialize our XYPlot reference:
plot = (XYPlot) findViewById(R.id.plot);
Intent intent = getIntent();
Bundle bd = intent.getExtras();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
if (bd != null) {
oldDateStr = (String) bd.get("START_DATE");
newDateStr = (String) bd.get("END_DATE");
plot.setTitle("From: "+oldDateStr+" To: "+newDateStr);
try {
Date date1 = format.parse(oldDateStr);
Date date2 = format.parse(newDateStr);
long diff = date2.getTime() - date1.getTime();
totalDays = Integer.parseInt(String.valueOf(TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS)));
System.out.println ("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
e.printStackTrace();
}
}
SimpleDateFormat formatterDateGraph = new SimpleDateFormat("MMM d");
SimpleDateFormat formatterDate = new SimpleDateFormat("yyyy-MM-dd");
try {
newDate = format.parse(newDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
DatabaseHandler db = new DatabaseHandler(this);
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(newDate);
domainLabels= new String[totalDays];
Number[] series1Numbers = new Number[totalDays];
//domain 0 set outside loop for oldDateStr
domainLabels[0]=String.valueOf(formatterDateGraph.format(newDate.getTime()));
List<Orders> reportForDayList = db.getReportByTranType(oldDateStr, oldDateStr,"1");
series1Numbers[0]=reportForDayList.size();
for (int i = 1; i <totalDays ; i++) {
cal.add(Calendar.DAY_OF_YEAR, -1);
domainLabels[i] = String.valueOf(formatterDateGraph.format(cal.getTime()));
//fetch ordercount for that day
reportForDayList = db.getReportByTranType(formatterDate.format(cal.getTime()),formatterDate.format(cal.getTime()),"1");
series1Numbers[i]=reportForDayList.size();
}
series1Numbers = reverseSeries(series1Numbers);
domainLabels = reverseDomain(domainLabels);
//reverseNumber array
// domainLabels = reverseNumber(domainLabels);
// turn the above arrays into XYSeries':
// (Y_VALS_ONLY means use the element index as the x value)
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Sales");
/* XYSeries series2 = new SimpleXYSeries(
Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");*/
// create formatters to use for drawing a series using LineAndPointRenderer
// and configure them from xml:
LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);
LineAndPointFormatter series2Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);
// add an "dash" effect to the series2 line:
/* series2Format.getLinePaint().setPathEffect(new DashPathEffect(new float[] {
// always use DP when specifying pixel sizes, to keep things consistent across devices:
PixelUtils.dpToPix(20),
PixelUtils.dpToPix(15)}, 0));*/
// just for fun, add some smoothing to the lines:
// see: http://androidplot.com/smooth-curves-and-androidplot/
series1Format.setInterpolationParams(
new CatmullRomInterpolator.Params(10, CatmullRomInterpolator.Type.Centripetal));
series2Format.setInterpolationParams(
new CatmullRomInterpolator.Params(10, CatmullRomInterpolator.Type.Centripetal));
// add a new series' to the xyplot:
plot.addSeries(series1, series1Format);
// plot.addSeries(series2, series2Format);
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.LEFT).setFormat(new DecimalFormat("#"));
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
#Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
int i = Math.round(((Number) obj).floatValue());
return toAppendTo.append(domainLabels[i]);
}
#Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});
}
public static Number[] reverseSeries(Number[] a)
{
int l = a.length;
for (int j = 0; j < l / 2; j++)
{
Number temp = a[j];
a[j] = a[l - j - 1];
a[l - j - 1] = temp;
}
return a;
}
public static String[] reverseDomain(String[] a)
{
int l = a.length;
for (int j = 0; j < l / 2; j++)
{
String temp = a[j];
a[j] = a[l - j - 1];
a[l - j - 1] = temp;
}
return a;
}
}

You are creating the domain values by transforming a 6 day range into long values and then subdividing the total range within those values into 10 distinct segments: You can't get 10 day labels from a range of 6 days.
To solve this problem you can either reduce the number of domain segments you are displaying or show more days worth of data.

Related

Linechart x-axis value reapiting in library MPAndroidchart

I have a problem when i set value from sqlite to x-axis label.
this is the picture
And my code of X-axis forrmated is
Axis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
String selectQueryz = "SELECT * FROM table_palembang";
db = new DBHelper(getApplicationContext());
SQLiteDatabase dbz = db.getWritableDatabase();
Cursor cursorz = dbz.rawQuery(selectQueryz, null);
countz = cursorz.getCount();
String[] datez = new String[countz];
ArrayList<String> arral = new ArrayList<>();
for (int k = 0; k < countz; k++) {
cursorz.moveToNext();
datez[k] = cursorz.getString(2);
arral.add(datez[k]);
}
return datez[countz % arral.size()];
}
});
can anyone help me?
thanks ...
ValueFormatter is used to format your data which you set with chart.setData() not to set data itself.
Here is sample code how to format dates
xAxis.setValueFormatter(new IAxisValueFormatter() {
private final SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm", Locale.ENGLISH);
#Override
public String getFormattedValue(float value, AxisBase axis) {
long millis = TimeUnit.HOURS.toMillis((long) value);
return mFormat.format(new Date(millis));
}
});
EDIT
I just took a look that you store indices of date array to chart data. If so, you just need to return date from dates array.
String[] datez;
String selectQueryz = "SELECT * FROM table_palembang";
db = new DBHelper(getApplicationContext());
SQLiteDatabase dbz = db.getWritableDatabase();
Cursor cursorz = dbz.rawQuery(selectQueryz, null);
countz = cursorz.getCount();
datez = new String[countz];
for (int k = 0; k < countz; k++) {
cursorz.moveToNext();
datez[k] = cursorz.getString(2);
}
xAxis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return datez[(int) value];
}
});
P.S. DataBase calls should be in background thread.

Octave dfdp.m for JAVA

I want to implement Levenberg Marquardt fitting in JAVA and found apache commons math suitable. Since I want to fit a function, where I dont have the derivative to calculate the gradient or Jacobian, I need somthing like dfdp.m from GNU octave to calculate numerical derivatives. Has someone done this already?
I did it myself, in case someone else needs it here is the approach
dfdp.m code
m=size(x,1); if (m==1), m=size(x,2); end %# PAK: in case #cols > #rows
n=length(p); %dimensions
ps=p; prt=zeros(m,n);del=zeros(n,1); % initialise Jacobian to Zero
for j=1:n
del(j)=dp(j) .*p(j); %cal delx=fract(dp)*param value(p)
if p(j)==0
del(j)=dp(j); %if param=0 delx=fraction
end
p(j)=ps(j) + del(j);
if del(j)~=0, f1=feval(func,x,p); %FJ ~= not equal (!=) ...> p is now (p + dp*p)
if dp(j) < 0, prt(:,j)=(f1-f)./del(j);
else
p(j)=ps(j) - del(j); %FJ ...> p is now (p - dp*p)
prt(:,j)=(f1-feval(func,x,p))./(2 .*del(j)); %FJ 2 steps from (ps + del) to (ps - del)
end
end
p(j)=ps(j); %restore p(j)
end
JAVA code
private static class GhoosProblem {
private double[][] data;
private double[] dp;
public GhoosProblem(double[][] datapoints, double[] delta_p) {
data = datapoints;
//dp= fractional increment of p for numerical derivatives
//dp(j)>0 central differences calculated
//dp(j)<0 one sided differences calculated
//dp(j)=0 sets corresponding partials to zero; i.e. holds p(j) fixed
dp = delta_p;
}
public MultivariateVectorFunction getModelFunction() {
return new MultivariateVectorFunction() {
public double[] value(double[] params) {
double[] values = new double[data.length];
for (int i = 0; i < values.length; ++i) {
final double t = data[i][0]; // get the double value
values[i] = params[0] *
Math.pow(t, params[2]) *
Math.exp(-params[1] * t); // Ghoos function
}
return values; // function values
}
};
}
public MultivariateMatrixFunction getModelFunctionJacobian2() {
return new MultivariateMatrixFunction() {
public double[][] value(double[] params) {
double[][] jacobian = new double[data.length][params.length];
final double a = params[0];
final double b = params[2];
final double c = params[1];
for (int i = 0; i < jacobian.length; ++i) {
final double t = data[i][0]; // get the double value
jacobian[i][0] = Math.pow(t, b) * Math.exp(-c*t);
jacobian[i][2] = a * Math.exp(-c*t) * Math.pow(t, b) * Math.log(t);
jacobian[i][1] = a * Math.pow(t, b) * (-t*Math.exp(-c*t));
}
//System.out.println("Jacobian= "+ Arrays.deepToString(jacobian));
return jacobian;
}
};
}
// compared to Ge2.m octave
public MultivariateMatrixFunction getModelFunctionJacobian() {
return new MultivariateMatrixFunction() {
public double[][] value(double[] params) {
int m = data.length; // cols
int n = params.length; // rows
double[] p = params;
double[] ps = params;
double[] del = new double[n];
double[] f = new double[n];
double[] f1 = new double[n];
BlockRealMatrix prt = new BlockRealMatrix(m, n); // initializes to zeros
f=feval(p);
for (int j=0; j<n; ++j) {
del[j]=dp[j] * p[j]; //delta_x=fractional(dp) * param value(p)
if (p[j]==0)
del[j]=dp[j]; //if param=0 delta_x=fractional(dp)
p[j]=ps[j] + del[j];
if (del[j]!=0) {
f1=feval(p); //p is now (p + dp*p)
if (dp[j]<0)
prt.setColumn(j,(new ArrayRealVector(f1)).subtract(new ArrayRealVector(f)).mapDivideToSelf(del[j]).toArray()); // one sided diff
else {
p[j]=ps[j] - del[j]; // p is now (p - dp*p)
prt.setColumn(j,(new ArrayRealVector(f1)).subtract(new ArrayRealVector(feval(p))).mapDivideToSelf(2*del[j]).toArray()); // central diff
}
}
p[j]=ps[j]; //restore p(j)
}//for
//System.out.println("Jacobian= "+ Arrays.deepToString(prt.getData()));
return prt.getData(); //jacobian, dimension is (m x n)
}
};
}
public double[] feval(double[] params) {
double[] values = new double[data.length];
for (int i = 0; i < values.length; ++i) {
final double t = data[i][0]; // get the double value
values[i] = params[0] *
Math.pow(t, params[2]) *
Math.exp(-params[1] * t); // Ghoos function
}
return values;
}
}//GhoosProblem
sorry if idention of code did not come out nice!
the relevant part is the getModelFunctionJacobian() -Function
I have renamed the analytical derivatives part as getModelFunctionJacobian2(), and posted here for comparison
to complete with here is the levenberg marquardt setup to use the GhoosFunction
public void fit() {
final double[][] dataPoints = { // x, y
//{0.0/60, 0.0}, // never use {0, 0} => org.apache.commons.math3.exception.ConvergenceException: illegal state: unable to perform Q.R decomposition on the 17x3 jacobian matrix
{15.0/60, 8.891104},
{30.0/60, 13.21852},
{45.0/60, 28.09051},
{60.0/60, 43.0011},
{75.0/60, 57.43561},
{90.0/60, 67.06862},
{105.0/60, 82.60239},
{120.0/60, 72.4649},
{135.0/60, 61.4},
{150.0/60, 43.97924},
{165.0/60, 30.6},
{180.0/60, 20.77112},
{195.0/60, 15.5},
{210.0/60, 10.85442},
{225.0/60, 9.33},
{240.0/60, 7.260234},
};
final double[] initialGuess = { 1.0, 1.0, 1.0 }; // p
final double[] fract_change = { 1E-4, 1E-4, 1E-4 }; // dp should be below 0.0001
final GhoosProblem problem = new GhoosProblem(dataPoints, fract_change);
final int len = dataPoints.length;
final double[] weights = new double[len];
final double[] target = new double[len];
for (int i = 0; i < len; i++){
weights[i] = 1.0;// / dataPoints[i][1];
target[i] = dataPoints[i][1];
}
final LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer()
.withCostRelativeTolerance(1E-4) // stol in octave
.withParameterRelativeTolerance(1E-4); // dp should be below 0.0001
final Optimum optimum = optimizer.optimize(
builder(problem)
.weight(new DiagonalMatrix(weights))
.target(target)
.start(initialGuess)
.maxIterations(100)
.build()
);
final RealVector solution = optimum.getPoint();
solution.setEntry(0, solution.getEntry(0) / 60.0); // go back to minutes
System.out.println("solution= " + solution);
System.out.println("CostRelativeTolerance= " + optimizer.getCostRelativeTolerance());
System.out.println("ParameterRelativeTolerance= " + optimizer.getParameterRelativeTolerance());
System.out.println("evaluations= " + optimum.getEvaluations());
System.out.println("iterations= " + optimum.getIterations());
//System.out.println("residuals= " + optimum.getResiduals());
System.out.println("RMS= " + optimum.getRMS());
System.out.println("sigma= " + optimum.getSigma(1E-10));
}//fit
public LeastSquaresBuilder builder(GhoosProblem problem){
return new LeastSquaresBuilder()
.checkerPair(new SimpleVectorValueChecker(1e-6, 1e-6)) // The SimpleVectorValueChecker Class (Simple implementation of the ConvergenceChecker) contains a method that uses the value of the function between two successive iterations of the optimisation algorithm to check if convergence has occured
.maxEvaluations(Integer.MAX_VALUE)
.maxIterations(Integer.MAX_VALUE)
//.lazyEvaluation(true)
.model(problem.getModelFunction(), problem.getModelFunctionJacobian());
}

XYPlot doesn't draw all points correctly

I'm trying to display 550 data points with periodic peaks (the flat line is 61). The problem is, that androidplot isn't drawing all the points correctly! From my log:
ECG I values 61,61,62,63,62,61,61,61,61,67,71,68,61,53,61,61,61,61,61,61,61,61,62,63,64,64,64,63,62,61,61,61
I've got the rangeboundaries set to plot.setRangeBoundaries(0,100, BoundaryMode.AUTO);, but as you can see, the peaks never drop to the 53 data point. I can see this lower point sometimes, but it gets smoothed out a fraction of a second later (as you can see in the screenshot).
My line and point formatter is:
LineAndPointFormatter lapf = new LineAndPointFormatter(p.color, null, null, null);
lapf.getLinePaint().setStrokeJoin(Paint.Join.MITER);
lapf.getLinePaint().setStrokeWidth(1);
I've tried with the both Paint.Join.ROUND and Paint.Join.BEVEL and got the same effect. I've also used the debugger to check that 53 is being inserted into the series.
EDIT
After some debugging, it looks like my pulse loop thread is wrong:
while (keepRunning) {
for (PulseXYSeries j : series) {
for (int k = 0; k < j.plotStep; k++) {
int at = (j.position + k) % j.getSize();
if (j.pulsing) {
if (j.pulsePosition == j.pulseValues.size() - 1) {
j.pulsing = false;
j.pulsePosition = 0;
} else {
try {
int pulseVal = j.pulseValues.get(j.pulsePosition);
j.setY(pulseVal,at);
j.pulsePosition += 1;
} catch(IndexOutOfBoundsException e) {
j.pulsePosition = 0;
}
}
} else {
j.setY(j.pulseValues.get(0), at);
long currTime = SystemClock.elapsedRealtime();
if (currTime - j.getLastPulse() >= j.getPulseDelay()) {
j.pulsing = true;
j.setLastPulse(currTime);
}
}
j.remove(((at + j.eraserSize) % j.getSize()));
}
j.position = (j.position + 1) % j.getSize(); // fixed it by changing +1 to + j.plotStep
}
Thread.sleep(delay);
}
My custom series looks like:
private class PulseXYSeries implements XYSeries {
private List<Integer> pulseValues = new ArrayList<Integer>();
private int pulsePerMinute;
public int pulsePosition;
public int position;
private ArrayList<Integer> values;
private String title;
private long lastPulse;
public boolean pulsing = false;
public int eraserSize = 20;
public int plotStep = 3;
}

How to separate the elements of an iterator?

I want to randomly generate times and numbers.. Here I used hashmap generate some records. Now I can generate the numbers but I cant separate them. I have to separate the values so that I can set those in database..
Here is my code...
public class DateTimePopulation {
private Random rand = new Random();
private Date theDay;
private String callDuration = null;
private String endDay = null;
SimpleDateFormat mytimeFormat = new SimpleDateFormat("HH:mm:ss");
public static void main(String[] args) throws ParseException {
DateTimePopulation d = new DateTimePopulation();
for (int i = 1; i <= 3; i++) {
Map rec = d.getRecord();
for (int j = 0; j < rec.size(); j++) {
Collection c = rec.values();
Iterator itr = c.iterator();
int count=0;
while (itr.hasNext()) {
Object element=itr.next();
System.out.println(element);
}
System.out.println();
}
}
}
private Map getRecord() {
Map<String, Object> rec = new LinkedHashMap<String, Object>();
Date startDate;
try {
startDate = getRandStartDate();
rec.put("StartTime", startDate);
int start = 7200000, end = 0;
int duration = getRandDuration(start, end);
rec.put("Duration", duration);
Date endDate = getRandEndDate(startDate, duration);
rec.put("EndTime", endDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rec;
}
private Date getRandEndDate(Date startDate, int duration) {
Random r = new Random();
int ranSec = r.nextInt(duration - 0) + 0;
return new Date(startDate.getTime() + ranSec * 1000);
}
private int getRandDuration(int High, int Low) {
Random r = new Random();
return r.nextInt(High - Low) + Low;
}
private Date getRandStartDate() throws ParseException {
Date theDay = new SimpleDateFormat("yyyyMMdd hh:mm:ss")
.parse("20130101 00:00:00");
int High = 60 * 60 * 24 * 30 * 6;
Random r = new Random();
int Low = 10;
int R = r.nextInt(High - Low) + Low;
return new Date(theDay.getTime() + R * 1000);
}
}
Here is the output. I am showing 2 set of it. I have to separate the time, duration etc.
Tue Jan 08 11:01:57 IST 2013
6074479
Fri Jan 18 12:56:24 IST 2013
Firstly, your design is strange to start with - why are you calling getRecord() on an instance, when it doesn't do anything with the fields of the object you're calling it on?
Additionally, when you're iterating over a map, you're actually iterating over the same map 3 times:
for (int j = 0; j < rec.size(); j++) {
Collection c = rec.values();
Iterator itr = c.iterator();
int count=0;
while (itr.hasNext()) {
Object element=itr.next();
System.out.println(element);
}
System.out.println();
}
Your outer loop is pointless here - you're never using j, after all. I would probably iterate over the entries rather than the values, if you really want to - then you can print out the key which goes with each value.
Next, I would encourage you not to use a map for this at all - create a separate type with fields for start time, duration and end time. That will fix things very simply.
Next, if you still want to use a map, stop using the raw types - it'll make your life simpler.
Finally, if you really still want to use a map, you already know the keys, so there's no point in iterating over it:
System.out.println("Start: " + map.get("StartTime");
System.out.println("Duration: " + map.get("Duration");
System.out.println("End: " + map.get("EndTime");
Basically, I strongly suggest that you take a step back and revisit your whole design. You may well find it's better to start from scratch than to change your existing code.
Try to generify this code:
private Map getRecord() {
Map<String, Object> rec = new LinkedHashMap<String, Object>();
Date startDate;
try {
startDate = getRandStartDate();
rec.put("StartTime", startDate);
int start = 7200000, end = 0;
int duration = getRandDuration(start, end);
rec.put("Duration", duration);
Date endDate = getRandEndDate(startDate, duration);
rec.put("EndTime", endDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rec;
}
Something like that:
private DateInterval getRecord() {
DateInterval interval = null;
try {
Date startDate = getRandStartDate();
int start = 7200000, end = 0;
int duration = getRandDuration(start, end);
Date endDate = getRandEndDate(startDate, duration);
interval = new DateInterval(startDate, endDate, duration);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return interval;
}
See also:
public class DateInterval {
private Date startDate;
private Date endDate;
private int duration;
public DateInterval(Date startDate, Date endDate, int duration) {
this.startDate = startDate;
this.endDate = endDate;
this.duration = duration;
}
public Date getStartDate() {
return startDate;
}
public Date getEndDate() {
return endDate;
}
public int getDuration() {
return duration;
}
}
I mean, you don't need Map for this purpose, use the your own entity. If you need to hold all your entities at Collection, use Map with DB UUIDs as a keys or List.

Android Listview countdown timer is causing strange results

I have a listview and want to countdown each item. My issue is when I scroll the countdown times jump around to incorrect items (so it shows another items countdown time) and there is no rhyme or reason to it? here is the code
private final Map<String, TextView> mcounterlist= new HashMap<String, TextView>();
Then in my getview on my adapater I do
String gmtFormat = "yyyy-MM-dd HH:mm:ss";
TimeZone tz = TimeZone.getDefault();
SimpleDateFormat sdf = new SimpleDateFormat(gmtFormat);
sdf.setTimeZone(tz);
Date dd = null;
try {
dd = sdf.parse(o.endAt);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date now = new Date();
String sss = timeCalculate((dd.getTime() /1000)- (now.getTime() /1000) );
ea.setText("Time Left: " + sss);
TextView cdi = new TextView();
mcounterlist.put(o.id , cdi);
So at this point I I just run a repeating thread to update.
private final Runnable mRunnable = new Runnable() {
public void run() {
// TextView textView;
String gmtFormat = "yyyy-MM-dd HH:mm:ss";
TimeZone tz = TimeZone.getDefault() ;
SimpleDateFormat sdf = new SimpleDateFormat(gmtFormat);
sdf.setTimeZone(tz);
// if counters are active
if(mcounterlist.size() > 0)
{
for (int i = 0; i < m_orders.size() && i < mcounterlist.size() ;i++) {
Order o = m_orders.get(i);
TextView c = mcounterlist.get(o.id);
Date dd = null;
try {
dd = sdf.parse(o.endAt);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date now = new Date();
long l = (dd.getTime() /1000)- (now.getTime() /1000);
String sss;
if (l < 0)
{
sss = "ENDED";
}
else
{
sss = timeCalculate(l);
}
if(l < 60 * 60 * 24)
{
c.setBackgroundColor(Color.parseColor("#E3170D"));
}
else
{
c.setBackgroundColor(Color.parseColor("#00000000"));
}
c.setText("Time Left: " + sss);
}
}
mHandler.postDelayed(this, 1000);
}
};
But when I scroll through my list, the textviews jump all over the place. I believe part of my issue is related to how I sort the list every 30 seconds, but the ID's of the items don't change. Is there anything obvious that is wrong?
Thanks.
So the issue is that the views in a listview get reused, the solution was to implement the following change the id to the position in my hashmap and put it in GETVIEW
mcounterlist.remove(position);
mcounterlist.put(position , ea);
You probably don't need to remove the position, but I left it in there since I spent a hour on this, and it works so I don't wnat to change anything:)
Then in my repeat task I need to exclude anyitem not visibe, so here is my handler code
int fp = lv.getFirstVisiblePosition() -1 < 0 ? 0 :lv.getFirstVisiblePosition() -1;
int lp = lv.getLastVisiblePosition();
for (int i = fp; i < mcounterlist.size() && i < lp;i++) {
//Log.i("TEST231231",String.valueOf(i));
if (i < lv.getFirstVisiblePosition() -1 )
{
continue;
}
if (i >= lv.getLastVisiblePosition())
{
continue;
}
//my adapter list
Order o = m_orders.get(i);
//since I set the position as the refereance, I can use that for the hashmap
TextView c = mcounterlist.get(i);
...
So in essance since the same textviews were getting recycled, I could not update all of them because items that were not on the list were sharing the active text boxes. To eliminate this issue, I check and only edit the textboxes for items that are visible.

Categories

Resources