I have app like Bulletin Board.I can add record(records adding to ListView,at ListView I have 2 fields:ID and text),delete them through OnLongItemClick.But when I deleting the record,ID still increment.And my app looks like: 1."..." 4."..." How I can to fix this issue? MainActivity:
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
Button btnadd;
LinearLayout llMain;
Time today = new Time(Time.getCurrentTimezone());
DBAdapter myDb;
ImageButton imgStar;
String changedname;
public long idchange;
private ListView myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
btnadd = (Button) findViewById(R.id.button);
//llMain = (LinearLayout) findViewById(R.id.llMain);
btnadd.setOnClickListener(this);
openDB();
populateListView();
imgStar = (ImageButton)findViewById(R.id.imageButton2);
imgStar.setOnClickListener(this);
myList = (ListView) findViewById(R.id.listView);
listViewItemClick();
listViewItemLongClick();
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(this, result.class);
startActivityForResult(intent, 1);
break;
case R.id.imageButton2:
ifTop();
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==1) {
String name = data.getStringExtra("task");
String grup = data.getStringExtra("grup");
today.setToNow();
String timestamp = today.format("%Y-%m-%d");
myDb.insertRow(name, timestamp, grup);
populateListView();
}
else if(requestCode==2){
changedname = data.getStringExtra("changedname");
updateTask(idchange,changedname);
populateListView();
}
}
private void openDB(){
myDb = new DBAdapter(this);
myDb.open();
}
private void populateListView() {
Cursor cursor = myDb.getAllRows();
String[] fromFieldNames = new String[]{DBAdapter.KEY_ROWID,DBAdapter.KEY_TASK,DBAdapter.KEY_DATE};
int[] toViewIDs = new int[]{R.id.textView3,R.id.textView6,R.id.textView7};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_layout,cursor,fromFieldNames,toViewIDs,0);
myList = (ListView)findViewById(R.id.listView);
myList.setAdapter(myCursorAdapter);
}
private void ifTop(){
Cursor c = myDb.ifTopViews();
String[] fromFieldNames = new String[]{DBAdapter.KEY_ROWID,DBAdapter.KEY_TASK,DBAdapter.KEY_DATE};
int[] toViewIDs = new int[]{R.id.textView3,R.id.textView6,R.id.textView7};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_layout,c,fromFieldNames,toViewIDs,0);
myList.setAdapter(myCursorAdapter);
}
private void listViewItemClick(){
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent1 = new Intent(MainActivity.this,edit.class);
startActivityForResult(intent1, 2);
idchange = id;
}
});
}
private void updateTask(long id,String changedname){
Cursor cursor = myDb.getRow(id);
if(cursor.moveToFirst()){
today.setToNow();
String date = today.format("%Y-%m-%d %H:%M:%S");
this.changedname = changedname;
myDb.updateRow(id,changedname,date,null);
}
cursor.close();
}
private void listViewItemLongClick(){
myList = (ListView)findViewById(R.id.listView);
myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
myDb.deleteRow(id);
populateListView();
return false;
}
});
}
}
Aucun commentaire:
Enregistrer un commentaire