I'm very new to android and seeking for help about listview optimization for data fetched from the database. "I want to check the boxes and then press the get button to show them in a toast."
with SimpleCursorAdapter :
package com.example.adapterx;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
DBAdapter myDb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openDB();
display();
insertdata();
}
private void insertdata() {
String pap = "1";
String pap1 = "2";
String pap2 = "3";
String pap3 = "4";
String pap4= "5";
String pap5 = "6";
String pap6 = "7";
String pap7 = "8";
String pap8 = "9";
String pap9 = "10";
String pap10 = "11";
String pap11 = "12";
String pap12 = "13";
String pap13= "14";
String pap14 = "15";
String pap15 = "16";
String pap16 = "17";
String pap17 = "18";
String pap18 = "19";
String pap19 = "20";
String pap20 = "21";
String pap21 = "22";
String pap22 = "23";
myDb.insertRow(pap);
myDb.insertRow(pap1);
myDb.insertRow(pap2);
myDb.insertRow(pap3);
myDb.insertRow(pap4);
myDb.insertRow(pap5);
myDb.insertRow(pap6);
myDb.insertRow(pap7);
myDb.insertRow(pap8);
myDb.insertRow(pap9);
myDb.insertRow(pap10);
myDb.insertRow(pap11);
myDb.insertRow(pap12);
myDb.insertRow(pap13);
myDb.insertRow(pap14);
myDb.insertRow(pap15);
myDb.insertRow(pap16);
myDb.insertRow(pap17);
myDb.insertRow(pap18);
myDb.insertRow(pap19);
myDb.insertRow(pap20);
myDb.insertRow(pap21);
myDb.insertRow(pap22);
Toast.makeText(this, "inserted successfully.", Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
private void openDB() {
myDb = new DBAdapter(this);
myDb.open();
}
private void closeDB() {
myDb.close();
}
private void display() {
final Cursor cursor = myDb.getAllRows();
String[] fromFieldNames = new String[]{DBAdapter.KEY_NAME};
int[] toViewIDs = new int[]{R.id.textsv};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter= new SimpleCursorAdapter(getBaseContext(),R.layout.item_layoutt,cursor , fromFieldNames , toViewIDs ,0);
final ListView myList = (ListView) findViewById(R.id.listView1);
myList.setAdapter(myCursorAdapter);
}
Activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.adapterx.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/textView1" >
</ListView>
<Button
android:id="@+id/get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/listView1"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/listView1"
android:text="Get" />
</RelativeLayout>
item_layoutt.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/textcb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textsv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="TextView" />
</LinearLayout>
It displays the list perfectly, but when I click on any checkbox the checked item repeats and changes when scrolled up and down.
I don't know how to solve this with ViewHolder .I read an article http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ ,but I fail to optimize the listview.
How can I modify MainActivity class with MyCustomAdapter class to solve this problem.Please help me with the entire code.
private class MyCustomAdapter extends dataAdapter {
private LayoutInflater mInflater;
private Cursor cur;
public MyCustomAdapter(Context context, Cursor c) {
super(context,c);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = this.mInflater.inflate(R.layout.chamber_item, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView)convertView.findViewById(R.id.textsv);
viewHolder.checkbox = (CheckBox)convertView.findViewById(R.id.textcb);
convertView.setTag(viewHolder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
this.cur.moveToPosition(position);
viewHolder.text.setText(this.cur.getString(this.cur.getColumnIndex("texts")));
viewHolder.checkbox.setText(this.cur.getString(this.cur.getColumnIndex("istrue")));
return convertView;
}
static class ViewHolder
{
TextView text;
CheckBox checkbox;
}
}