When I delete any data then listitem click is showing error when opening listitem and data are also not correct on custom listview. After deleting row 0 data is not updating properly. Please help..
mydb = new DBHelper(this);
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
// means this is the view part not the add contact part.
Cursor crs = mydb.getData(Value);
id_To_Update = Value;
crs.moveToFirst();
String nam = crs.getString(crs.getColumnIndex(DBHelper.C_NAME));
String phon = crs.getString(crs
.getColumnIndex(DBHelper.C_PHONE));
String addr = crs.getString(crs
.getColumnIndex(DBHelper.C_ADDRESS));
String dat = crs.getString(crs.getColumnIndex(DBHelper.C_DATE));
String typ = crs.getString(crs.getColumnIndex(DBHelper.C_TYPE));
if (!crs.isClosed()) {
crs.close();
}
Button b = (Button) findViewById(R.id.button1);
b.setVisibility(View.INVISIBLE);
name.setText((CharSequence) nam);
name.setFocusable(false);
name.setClickable(false);
phone.setText((CharSequence) phon);
phone.setFocusable(false);
phone.setClickable(false);
type.setText((CharSequence) typ);
type.setFocusable(false);
type.setClickable(false);
address.setText((CharSequence) addr);
address.setFocusable(false);
address.setClickable(false);
date.setText((CharSequence) dat);
date.setFocusable(false);
date.setClickable(false);
}
}
}
case R.id.Delete_Contact:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(),
"Deleted Successfully",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(
getApplicationContext(),
com.example.addressbook.MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void run(View view) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
if (mydb.updateContact(id_To_Update, name.getText().toString(),
phone.getText().toString(), type.getText().toString(),
address.getText().toString(), date.getText().toString())) {
Toast.makeText(getApplicationContext(), "Updated",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),
com.example.addressbook.MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "not Updated",
Toast.LENGTH_SHORT).show();
}
} else {
if (mydb.insertContact(name.getText().toString(), phone
.getText().toString(), type.getText().toString(),
address.getText().toString(), date.getText().toString())) {
Toast.makeText(getApplicationContext(), "done",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "not done",
Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(getApplicationContext(),
com.example.addressbook.MainActivity.class);
startActivity(intent);
}
DBHelper.java
public Integer deleteContact(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts", "id = ? ",
new String[] { Integer.toString(id) });
}
}
Finally I have got the solution. I have a text view in custom list view which has Table id which is unique for every row and after deleting the row it's value does not change so I am opening the data of that row on different activity.
Related
I am creating an auto caller app in which i have used a button which takes data from arrayList, iterate through the arraylist and ask user on each element if want to do next call or pause on that for this i have used alert dialog, but the loop is not getting pause on each element it went to the last element and show alert dialog for that contact number.
private List<ContactEntity> mContactsList = new ArrayList<>();
private ContactEntity c;
private int i = 0;
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.start_call) {
Toast.makeText(this, "Call Started" + mContactsList.size(), Toast.LENGTH_LONG).show();
startAutoCall();
}
return super.onOptionsItemSelected(item);
}
private void startAutoCall() {
if (mContactsList.isEmpty()) {
Toast.makeText(this, "Import Contacts ", Toast.LENGTH_LONG).show();
} else {
c = mContactsList.get(i);
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + c.getPersonContactNumber()));
startActivity(intent);
while(i < mContactsList.size()){
c = mContactsList.get(i);
Log.d(TAG, "startAutoCall: " + c.getId());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Auto Dialer Start");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + c.getPersonContactNumber()));
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
dialog.show();
i++;
}
}
}
It's not a good idea to build alert dialog inside a loop. Instead, create one and update the value for each element in the list on button click. Override onClickListener for the dialog so that you can control the flow better.
Here's a snippet in kotlin.
if(mContactList.isNotEmpty()){
c = mContactList.get(i)
val builder = AlertDialog.Builder(this)
builder.setTitle("Auto Dialer Start")
builder.setMessage("Your message here...")
builder.setPositiveButton("OK", null)
val dialog = builder.create()
dialog.setOnShowListener{ dialogInterface->
val btnOk = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
btnOk.setOnClickListener{
//Your code here...
if(i == mContactList.size-1){
dialogInterface.dismiss()
}else {
i++
c = mContactList.get(i)
}
}
}
dialog.show()
}
I want to create a Toast Text (about I haven't registered in the database) on the Login Activity. However, there is a problem when I put this Toast text. If I put it inside a while loop Cursor.MoveToNext(), it will loop as many times as many entries it has.
Inside the loop
if(cursor!=null)
{
{
int i = 0;
while(cursor.moveToNext())
{
if(cursor.getString(i).equals(input_email) && cursor.getString(i).equals(input_password))
{
Log.d(TAG, "Udah masuk belum " );
Toast.makeText(getApplicationContext(),"Login Successful!", Toast.LENGTH_SHORT).show();
Intent intent =new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();
i = i + 1;
}
else
{
Toast.makeText(getApplicationContext(),"You haven't Registered yet!", Toast.LENGTH_SHORT).show();
Intent intent =new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();
}
}
cursor.close();
}
}
But when I putted outside of the loop, it brings error. Do you know what is the solution of this one?
Outside the loop
String [] projection ={ServiceProvidersContract.Columns.SEmail, ServiceProvidersContract.Columns.spPassword};
Cursor cursor = contentResolver.query(ServiceProvidersContract.CONTENT_URI, projection, null, null, null);
Log.d(TAG, "Checking Cursor" + cursor );
if(cursor!=null)
{
{
int i = 0;
while(cursor.moveToNext())
{
if(cursor.getString(i).equals(input_email) && cursor.getString(i).equals(input_password))
{
Log.d(TAG, "Udah masuk belum " );
Toast.makeText(getApplicationContext(),"Login Successful!", Toast.LENGTH_SHORT).show();
Intent intent =new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();
i = i + 1;
}
}
cursor.close();
if(!cursor.getString(i).equals(input_email) && cursor.getString(i).equals(input_password))
{
Toast.makeText(getApplicationContext(),"You haven't Registered yet!", Toast.LENGTH_SHORT).show();
Intent intent =new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();
}
}
}
Just want to clarify, I did finally login successfully in my login parts using For and While.
The other problem is coming from the Toast Text ( where I couldn't put the failed Toast Text in the loop). This is my codes :
private static final String TAG = "LoginActivity";
private Button btnLogin, btnLinkToRegister;
private SessionManager session;
private EditText password, email;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Defining everything
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegister);
btnLogin = (Button) findViewById(R.id.btnLogin);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ContentResolver contentResolver = getContentResolver() ;
String input_email = email.getText().toString().trim();
String input_password = password.getText().toString().trim();
Log.d(TAG, "onClick: " + input_email + input_password);
// Check for empty data in the form
if (!input_email.isEmpty() && !input_password.isEmpty()) {
Log.d(TAG, "onClick: if statement succeseful");
// checking from the database
String [] projection ={ServiceProvidersContract.Columns.SEmail, ServiceProvidersContract.Columns.spPassword};
Cursor cursor = contentResolver.query(ServiceProvidersContract.CONTENT_URI, projection, null, null, null);
Log.d(TAG, "Checking Cursor" + cursor );
if(cursor!=null)
{
cursor.moveToFirst();
while(cursor.moveToNext())
{
Log.d(TAG, "while process");
for(int i=0; i<cursor.getColumnCount(); i++)
{
Log.d(TAG, "for process " + cursor.getString(i));
if (cursor.getString(0).equals(input_email) && cursor.getString(1).equals(input_password)) {
Log.d(TAG, "if process");
Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();
break;
}
}
}
if (!cursor.getString(0).equals(input_email) && cursor.getString(1).equals(input_password)) {
Log.d(TAG, "if process");
Toast.makeText(getApplicationContext(), "You haven't registered yet!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();
}
cursor.close();
}
} else {
// Prompt user to enter credentials
Toast.makeText(getApplicationContext(),
"Please enter the credentials!", Toast.LENGTH_LONG)
.show();
}
}
});
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(i);
finish();
}
});
}
}
I want to send the integer value from one activity to another but I am not getting the value,There is no error in my project.If I am giving the static value it is working so problem with intent only..
Passing the data
try {
JSONObject jsonobject = new JSONObject(doc);
final String statusCode=jsonobject.get("code").toString();
System.out.print("Code......>>>>>>>>>>>>>>"+statusCode);
switch (statusCode){
case "400":
Toast.makeText(getApplicationContext(), "" + doc, Toast.LENGTH_LONG).show();
break;
case "200":
final Dialog dialog = new Dialog(SecondActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.link_dialog);
Button dialogButtonCustomercare = (Button) dialog
.findViewById(R.id.button_ok);
Button dialogButtonCustomer = (Button) dialog
.findViewById(R.id.button_contact_us);
dialogButtonCustomercare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("StatusCode", 200);
startActivity(i);
// dialog.dismiss();
}
});
dialogButtonCustomer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
makeCall(phonenumber);
}
});
dialog.show();
break;
default:
Toast.makeText(getApplicationContext(), "Oops something went wrong! ", Toast.LENGTH_LONG).show();
break;
}
} catch (Exception e) {
Log.i("Error", e.getMessage());
}
Getting the data
int responseCode ;
Intent i = getIntent();
responseCode = i.getIntExtra("StatusCode",0);
System.out.print("Status Code" + responseCode);
if (responseCode==200) {
DetailsImageView.setVisibility(View.VISIBLE);
// textview.settext(bank details verification done)
} else {
DetailImageView.setVisibility(View.INVISIBLE);
}
dialogButtonOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
i.putExtra("StatusCode", 200);
// Toast.makeText(getApplicationContext(), "sucessful intent" +200, Toast.LENGTH_LONG).show();
startActivity(i);
finish();
dialog.dismiss();
}
});
I used the android alertdialog in order to redirect to a url, the redirection should go according to user choice, here is the code:
final CharSequence[]stringArray = {"1" ,"2" , "3"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (stringArray.toString().equals("1")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(st));
startActivity(intent);
}
else if (stringArray.toString().contains("2")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(st2));
startActivity(intent);
}
}
});
AlertDialog alert = builder.create();
alert.show();
but when I click 1 or 2 there is no redirection to the url
what is wrong with the code?
You are checking your arrays items which is not valid way to implement. You need to check for selected item's id which you can get from onclick method only.
As the array count starts from "0" so can check your selected item with "0 to 2" where your 0 will be considered as "1" selected and so on.
Check out below code:
final CharSequence[] stringArray = { "1", "2", "3" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (item == 0)
// if (stringArray.toString().equals("1"))
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(st));
startActivity(intent);
} else if (item == 1)
// else if (stringArray.toString().contains("2"))
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(st2));
startActivity(intent);
}
}
});
AlertDialog alert = builder.create();
alert.show();
Please use following code:-
final CharSequence[] stringArray = { "1", "2", "3" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (position == 0) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st));
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st2));
startActivity(intent);
}
else if (position == 2) {
// write some code
}
}
});
AlertDialog alert = builder.create();
alert.show();
I want to uninstall multiple applications by checking checkbox.. problem is when i check multiple apps to uninstall it alerts "Are you sure you want to uninstall this App" every time. I want only one alert and the code uninstalls all checked Applications.
private void displayListView() {
ArrayList<App> appList = new ArrayList<App>();
App app = null;
List<PackageInfo> list = getPackageManager().getInstalledPackages(0);
String name = "";
String path = "";
String capName = "";
for (PackageInfo p : list) {
if ((p.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
} else {
name = p.applicationInfo.loadLabel(getPackageManager())
.toString();
capName = getApplicationContext().getPackageName();
path = p.applicationInfo.packageName;
if ((!name.equals("com.android.gesture.builder"))
&& (!capName.equals(path))) {
app = new App(path, name, false);
app.setVersionCode(p.versionCode);
app.setVersionName(p.versionName);
app.setIcon(p.applicationInfo.loadIcon(getPackageManager()));
appList.add(app);
}
}
}
// create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.layout_app_list,
appList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// app app = (app)
// parent.getItemAtPosition(position);
App ap = (App) parent.getItemAtPosition(position);
// Toast.makeText(getApplicationContext(), ap.getName(),
// Toast.LENGTH_SHORT).show();
Intent itn = new Intent(AppManagement.this, AppInfo.class);
Bundle extras = new Bundle();
extras.putString("name", ap.getName());
extras.putString("path", ap.getpath());
extras.putString("vName", ap.getVersionName());
extras.putString("vCode", ap.getVersionCode() + "");
itn.putExtras(extras);
startActivity(itn);
}
});
}
public void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
ArrayList<App> appList = dataAdapter.appList;
App app = null;
for (int i = 0; i < appList.size(); i++) {
app = appList.get(i);
if (app.isSelected()) {
urr = Uri.fromParts("package", app.getpath(), null);
Log.d("", urr.toString());
responseText.append("\n" + app.getName() + " \t "
+ app.getpath());
Intent intent = new Intent(Intent.ACTION_DELETE, urr);
startActivityForResult(intent, 1);
}
}
if (responseText.length() == 0) {
Toast.makeText(getApplicationContext(), "No App selected ",
Toast.LENGTH_SHORT).show();
} else {
// Toast.makeText(getApplicationContext(), responseText,
// Toast.LENGTH_LONG).show();
}
}
});
}
public void onActivityResult(int rCode, int resultCode, Intent data) {
if (rCode == 1) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Ok", Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
displayListView();
}
}
}
This is not currently available to third party applications. You will have to take permission from user for each install/uninstall.