it shows nullPointerException,which I will resolve.Sir, now the problem arises in list activity.
ListActivity displays list of items in a list.Like:
1(index autoincrement).Item Name1
2.Item Name2
3.Item Name3
4.Item Name4
5.Item Name5
6.Item Name6.........so on
Problem:If I delete any item say item 2,item 3 goes to item 2 position but the index is not getting updated.Index remains same just positions getting changed.
After deleting item 2.The listActivity displaying like this
1.Item Name1
3.Item Name3
4.Item Name4
5.Item Name5
6.Item Name6......
which is supposed to display as:
1.Item Name1
2.Item Name3
3.Item Name4
4.Item Name5
5.Item Name6......
codes:
DISPLAYCONTACT:
DisplayContact extends activity{
mydb.deleteContact(id_To_Delete);
Toast.makeText(getApplicationContext(), NAME+" 's"+"contact Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),com.dinesh.contactsBook.MainActivity.class);
startActivity(intent);
}
MAINACTIVITY:
MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
mydb = new DBHelper(this);
ArrayList<
String> array_list = mydb.getAllContacts();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array_list);
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(arrayAdapter);
obj.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
int id_To_Search = arg2;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),com.dinesh.contactsBook.DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
DBHELPER:
public class DBHelper extends SQLiteOpenHelper
{
public Integer deleteContact (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("myTable", "id = ? ", new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllContacts()
{
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from myTable", null );
res.moveToFirst();
while(res.isAfterLast() == false)
{
String Index=res.getString(res.getColumnIndex(CONTACTS_COLUMN_ID));
String Name=res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME));
array_list.add(Index+"."+Name);
res.moveToNext();
}
db.close();
return array_list;
}
}
PROBLEMS:
1.when item 2 is deleted ,item 3 comes to item 2 position but when it is clicked to see the details logcat displays
cursorIndexOutofBoundsException :Index 0 requested, with a size of 0.With my little knowledge I can understand that the error is arising because i am trying to access which is not present.Dear Ulf,Kindly check the code .