Tengo un cronometro android(q lo programe yo debido a que necesito los milisegundos), donde al presionar un boton se registra el tiempo para corredores. cuando se presiona el boton el tiempo pasa a un listview que tiene un editext que espera recibir el numero del corredor, y actualizo el adapter.Mi problema es que el listview se pone mas lento a medida que voy agregando tiempos, y me va trabando el cronometro. si bien el tiempo continua la aplicacion no queda bien si el reloj se va trabando cada vez que marco un lap.Todos estos datos tienen que guardarse luego en una base de datos.
CLASE ADAPTER
public class ItemListAdapter extends ArrayAdapter { protected static final String LOG_TAG = ItemListAdapter.class.getSimpleName();
private ArrayList<Persona> items; private int layoutResourceId; private Context context; public ItemListAdapter(Context context, int layoutResourceId, ArrayList<Persona> items) { super(context, layoutResourceId, items); this.layoutResourceId = layoutResourceId; this.context = context; this.items = items; } @SuppressLint("ViewHolder") @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; Holder holder = null; LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new Holder(); holder.bean = items.get(position); //holder.ibDelete = (ImageButton)row.findViewById(R.id.ibDelete); //holder.ibDelete.setTag(holder.bean); holder.etVal1 = (EditText)row.findViewById(R.id.nrocompetidor); //holder.etVal2 = (EditText)row.findViewById(R.id.edValue2); holder.tvTotal=(TextView) row.findViewById(R.id.et_laps); setVal1TextChangeListener(holder); //setVal2TextListeners(holder); row.setTag(holder); setupItem(holder); return row; } private void setupItem(Holder holder) { holder.etVal1.setText(String.valueOf(holder.bean.getNumero())); //holder.etVal2.setText(String.valueOf(holder.bean.getVal2())); holder.tvTotal.setText(String.valueOf(holder.bean.getTiempo())); } public static class Holder { Persona bean; EditText etVal1; EditText etVal2; TextView tvTotal; ImageButton ibDelete; } private void setVal1TextChangeListener(final Holder holder) { holder.etVal1.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(s.toString().length()>0) holder.bean.setNumero(Integer.parseInt(s.toString())); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); }
}
CLASE MAINACTIVITY protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
//Instantiating all member variables names=new ArrayList<Persona>(); mContext = this; mBtnStart = (Button) findViewById(R.id.btn_start); mBtnLap = (Button) findViewById(R.id.btn_lap); mBtnStop = (Button) findViewById(R.id.btn_stop); mTvTimer = (TextView) findViewById(R.id.tv_timer); //mEtLaps = (EditText) findViewById(R.id.et_laps); // mEtLaps.setEnabled(false); //prevent the et_laps to be editable //mSvLaps = (ScrollView) findViewById(R.id.sv_lap); adapter = new ItemListAdapter(MainActivity.this, R.layout.item_list,names); listView = (ListView)findViewById(R.id.listViewMain); listView.setAdapter(adapter); //btn_start click handler mBtnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //if the chronometer has not been instantiated before... if(mChrono == null) { //instantiate the chronometer mChrono = new Chronometer(mContext); //run the chronometer on a separate thread mThreadChrono = new Thread(mChrono); mThreadChrono.start(); //start the chronometer! mChrono.start(); //clear the perilously populated et_laps // mEtLaps.setText(""); //empty string! //reset the lap counter mLapCounter = 1; } } }); //btn_stop click handler mBtnStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //if the chronometer had been instantiated before... if(mChrono != null) { //stop the chronometer mChrono.stop(); //stop the thread mThreadChrono.interrupt(); mThreadChrono = null; //kill the chrono class mChrono = null; } } }); mBtnLap.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //if chrono is not running we shouldn't capture the lap! if(mChrono == null) { Toast.makeText(mContext , R.string.warning_lap_button, Toast.LENGTH_SHORT).show(); return; //do nothing! } Persona p=new Persona(0,mTvTimer.getText().toString()); names.add(p); adapter.notifyDataSetChanged(); mSvLaps.post(new Runnable() { @Override public void run() { mSvLaps.smoothScrollTo(0, mEtLaps.getBottom()); } });*/ } }); } /** * Update the text of tv_timer * @param timeAsText the text to update tv_timer with */ public void updateTimerText(final String timeAsText) { runOnUiThread(new Runnable() { @Override public void run() { mTvTimer.setText(timeAsText); } }); } public class Persona implements Serializable { private int id; private String nombre; private String apellido; private String categoria; private int numero;
CLASE PERSONA public Persona(String nombre, String apellido,String categoria, int numero) { this.nombre = nombre; this.numero=numero; this.apellido = apellido; this.categoria = categoria; }
public int getNumero() { return numero; } public void setNumero(int numero) { this.numero = numero; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNombre() { return nombre; } public void setNombre(String nombre) { this.nombre = nombre; } public String getApellido() { return apellido; } public void setApellido(String apellido) { this.apellido = apellido; } public String getCategoria() { return categoria; } public void setCategoria(String categoria) { this.categoria = categoria; }
}
LAYOUT MAIN
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" tools:context="com.embedonix.chronometer.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/tv_timer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:ellipsize="end" android:scrollbars="none" android:text="00:00:00:000" android:textAlignment="center" android:textColor="#0dff00" android:textSize="48dp"/> </LinearLayout> <LinearLayout android:layout_weight="0.2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/listViewMain" android:background="#ffffff" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout> <LinearLayout android:layout_weight="0.9" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#1f1d1d" android:orientation="horizontal"> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="START" android:textSize="18dp" android:typeface="monospace"/> <Button android:id="@+id/btn_lap" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="LAP" android:textSize="18dp" /> <Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="STOP" android:textSize="18dp"/> <Button android:id="@+id/btn_terminar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="FIN" android:textSize="18dp"/> </LinearLayout>
LAYOUT ITEM_LIST
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/texto" android:layout_width="0sp" android:layout_height="fill_parent" android:layout_weight="1" android:text="Corredor Nª" android:gravity="center_vertical" android:textAppearance="?android:attr/textAppearanceSmall" /> <EditText android:id="@+id/nrocompetidor" android:layout_width="0sp" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:inputType="numberDecimal" android:textAppearance="?android:attr/textAppearanceSmall" /> <TextView android:id="@+id/et_laps" android:layout_width="0sp" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:text="Total" android:textAppearance="?android:attr/textAppearanceSmall" />