mardi 21 juillet 2015

how to get text from textview in custom ListView item in android?

I have a ListView which of it's items are separate Layout. They are populated by using SimpleCursorAdapter from SQLite Database. I want to set ContextMenu fro that ListView in order to Edit and Delete items from database. ContextMenu appearing but when I try to get TextView text from custom list item only the first item's text only get selected. I have posted my code. Please help.

public class ViewLocations extends Activity {

DatabaseHelper databaseHelper;
SQLiteDatabase db;
ListView listLocations;
TextView tvLocation;
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_locations);

    listLocations=(ListView)findViewById(R.id.listView);

    databaseHelper=new DatabaseHelper(this);
    db=databaseHelper.getWritableDatabase();
    String query1="Select rowid _id,*from locationTable";
    try {
        Cursor cursor=db.rawQuery(query1,null);
        if (cursor.getCount()>0){
            while (cursor.moveToNext()){
                adapter=new SimpleCursorAdapter(this,R.layout.locations_list,cursor,new String[]{"locationName","latitude","longitude"},new int[]{R.id.tvLocationName,R.id.textView3,R.id.textView4});
                listLocations.setAdapter(adapter);
                registerForContextMenu(listLocations);
            }
        }

    }catch (SQLiteException e){
        Toast.makeText(ViewLocations.this,e.getMessage(),Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_delete, menu);
    menu.setHeaderTitle("Options");
}

@Override public boolean onContextItemSelected(MenuItem item) {

    AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

    tvLocation=(TextView)findViewById(R.id.tvLocationName);

    switch (item.getItemId()) {

        case R.id.edit:
            Toast.makeText(ViewLocations.this,"Edit",Toast.LENGTH_SHORT).show();
            break;

        case R.id.delete:
            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(ViewLocations.this);
            alertBuilder.setTitle("Delete");
            alertBuilder.setMessage("Do you want to Delete " + "\"" + tvLocation.getText().toString() + "\"" + " ?");
            alertBuilder.setIcon(R.drawable.delete);
            AlertDialog alert=alertBuilder.create();
            alert.show();
            break;

        default:
            return super.onContextItemSelected(item);
    }

    return true;
}

private AlertDialog AskOption() {
    AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this)
            .setTitle("Delete")
            .setMessage("Do you want to Delete "+"\""+tvLocation.getText().toString()+"\""+" ?")
            .setIcon(R.drawable.delete)
            .setPositiveButton("Delete", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {

                    long delete=db.delete("locationTable","locationName='"+tvLocation.getText().toString()+"'",null);

                    if (delete!=-1){
                        Toast.makeText(ViewLocations.this,"Deleted Successfully",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(ViewLocations.this,"Deleting not Successful",Toast.LENGTH_SHORT).show();
                    }
                    dialog.dismiss();
                    listLocations.setAdapter(adapter);
                    adapter.getCursor().requery();
                }

            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            })
            .create();
    return myQuittingDialogBox;
}

}

Aucun commentaire:

Enregistrer un commentaire