Hey guys.
So, I've spent the last two days watching every tutorial about working with sqlite, but I'm doing something wrong and can't find out what.
The app is done, only needs the SQLite part, Its basicly a webapp with favorites.
Altough I have changed the code several times, this is what I ended up with:
DBManager.java
<code>
package com.rjpf.mywebapps;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class DBManager
{
private DbHelper dbHelper;
private Context context;
private SQLiteDatabase database;
public DBManager(Context c)
{
context=c;
}
public DBManager open() throws SQLException
{
dbHelper = new DbHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
dbHelper.close();
}
public void insert(String name, String url)
{
ContentValues contentValue = new ContentValues();
contentValue.put(DbHelper.CONTACTS_COLUMN_NAME, name);
contentValue.put(DbHelper.CONTACTS_COLUMN_URL, url);
database.insert(dbHelper.CONTACTS_TABLE_NAME, null, contentValue);
}
}
</code>
When I try to call it in the main activity with:
MainActivity.java
<code>
(...)
private LinearLayout Layout_Add;
private TextView TxT_add_nomE;
private TextView TxT_add_urL;
private Button Button_Add_to_DB;
private Button btn_BACK_Add;
DbHelper myDB;
(...)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDB = new DbHelper(this);
(...)
Button_Add_to_DB.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
Add_ItemToDb();
}
});
(...)
public void Add_ItemToDb()
{
if (TxT_add_nomE.getText().toString().trim().length() == 0 || TxT_add_urL.getText().toString().trim().length() == 0)
{
TxT_add_nomE.setText("Please don't leave fields empty!");
return;
}
else
{
String addNome = TxT_add_nomE.getText().toString();
String addUrl = TxT_add_urL.getText().toString();
// ADD TO DB
DBManager DataManager = new DBManager(this);
DataManager.insert(addNome,addUrl); // This line is what gives the error, when I click the button to add the App craches
TxT_add_nomE.setText("Name");
TxT_add_urL.setText("Url");
}
}
</code>
Thanks in advance and sorry for the long post, This is my first app and also the first time in java
Related
I want to write a text file. But I am not sure how writing down works in Android. Here is my code:
Code:
EditText textbox1 = (EditText) findViewById(R.id.editText);
EditText textbox2 = (EditText) findViewById(R.id.editText2);
EditText textbox3 = (EditText) findViewById(R.id.editText3);
public void writeToPhonebook(View v) {
Button button=(Button) v;
try {
File fajl = new File("phonebook.txt");
Writer writer;
writer= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fajl, true), "UTF-8"));
writer.append(textbox1.getText() + "\t" + textbox2.getText() + "\t" + textbox3.getText());
writer.write("\r\n");
writer.close();
textbox1.setText("");
textbox2.setText("");
textbox3.setText("");
textbox1.requestFocus();
}
catch (IOException ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
}
can I just leave phonebook.txt or need to put something else? Because with this code apk can't even start on emulator.
OK I done this previus. I now need to load data from text file into table (table layout with 3 columns). Is it possible for application to determine number of needed rows without me adding certain number. Because that data will be changed.
You need permissions to write a file, you set them in the AndroidManifest xml (Every Android Studio project has their own manifest, set it in your project)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Then you need to open a file for input/output... You do this in whatever.java
Code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import android.util.Log;
public class FileOperations {
public FileOperations() {
}
public Boolean write(String fname, String fcontent){
try {
String fpath = "/sdcard/"+fname+".txt";
File file = new File(fpath);
// If file does not exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fcontent);
bw.close();
Log.d("Suceess","Sucess");
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public String read(String fname){
BufferedReader br = null;
String response = null;
try {
StringBuffer output = new StringBuffer();
String fpath = "/sdcard/"+fname+".txt";
br = new BufferedReader(new FileReader(fpath));
String line = "";
while ((line = br.readLine()) != null) {
output.append(line +"n");
}
response = output.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return response;
}
}
What is easier to display data from text file in: TableLayout or ViewList? If not sure total number of rows? Is there something for scrooling through it if can't fit the screen?
Scrollview
Grimspiller said:
What is easier to display data from text file in: TableLayout or ViewList? If not sure total number of rows? Is there something for scrooling through it if can't fit the screen?
Click to expand...
Click to collapse
1) Yes. Android provide scrollview to scroll on your query.
A. Horizontal scrollview
B. Vertical Scrollview.
Also you can you onTextwatcher() to check if the field size increase , onAfterTextchanged() method you can resize the box is.
private final TextWatcher passwordWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
textView.setVisibility(View.GONE);
} else{
textView.setText("You have entered : " + passwordEditText.getText());
}
}
};
I found this tutorial for multicolumn ListView on techlovejump.com ,but data for it is hardcoded. I am not sure is it suitable for dynamic data adding from text file.
Dynamic Data
Grimspiller said:
I found this tutorial for multicolumn ListView on techlovejump.com ,but data for it is hardcoded. I am not sure is it suitable for dynamic data adding from text file.
Click to expand...
Click to collapse
hey, As you see on after afterTextChanged(Editable s)
You can again call your table structure code to design it as per size.
I think you should try it once.
Share some amount of code if you find it difficult. I will help you afterTextChanged(Editable s).
I don't need to write text but to read it. Can you look at this code and tell me what is the problem? It can't find the file.
Code:
void showData(){
File fajl = new File("phonebook.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(fajl));
String line;
try {
while ((line = br.readLine()) != null) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
TextView im = new TextView(this);
im.setText(line);
im.setTextColor(Color.BLACK);
im.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
tr.addView(im);
tablel.addView(tr, new TableLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
button2.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
showData();
}
});
I have added to xml TableLayout and one row as header.
I use this for writing to text file:
Code:
button.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
try {
FileOutputStream fos = openFileOutput("phonebook.txt", Context.MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.append(textbox1.getText().toString() + "\t" + textbox2.getText().toString() + "\t" + textbox3.getText().toString());
osw.write("\r\n");
osw.close();
textbox1.setText("");
textbox2.setText("");
textbox3.setText("");
textbox1.requestFocus();
} catch (IOException ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
}
});
Debbuger says it's writting file and lines just fine. But why I can't read it?
It creates "files" folder inside my app folder. And inside files it is phonebook.txt.
hi.
i make login and register app' that use php,SQL and database, all work excellent,
I add a few thing like: user can upload image (base64) to his php folder and more,
So now i am stock when i want get image back (base64) from php folder
this my code:
Code:
public class SQLiteHandler extends SQLiteOpenHelper {
private static final String TAG = SQLiteHandler.class.getSimpleName();
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "u294011906_camel";
// Login table name
private static final String TABLE_LOGIN = "login";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";
public SQLiteHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT,"
+ KEY_CREATED_AT + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
Log.d(TAG, "Database tables created");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
* */
public void addUser(String name, String email, String uid, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_UID, uid); // Email
values.put(KEY_CREATED_AT, created_at); // Created At
// Inserting Row
long id = db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id);
}
/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
/**
* Getting user login status return true if rows are there in table
* */
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
/**
* Re crate database Delete all tables and create them again
* */
public void deleteUsers() {
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_LOGIN, null, null);
db.close();
Log.d(TAG, "Deleted all user info from sqlite");
}
}
Code:
/**
* Function to upload profile picture
* */
private void uploadProfilePicture(final String image,final String emailToSend)
{
// Tag used to cancel the request
pictureCheck = "null";
String tag_string_req = "profile_image";
pDialog.setMessage("Upload profile picture ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Upload profile picture Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
Toast.makeText(getApplicationContext(), "Your new profile picture sucessfully uploaded", Toast.LENGTH_LONG).show();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Profile picture Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
})
{
@Override
protected Map<String, String> getParams()
{
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "profile_image");
params.put("ImageName", emailToSend);
params.put("base64", image);
params.put("email", emailToSend);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
And Some code how i am get details of user
Code:
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();
String name = user.get("name");
String email = user.get("email");
EmailtoSend = email;
// Displaying the user details on the screen
txtName.setText(name);
i want get profile image from php folder, i put image(bae64) to folder that open when user register. All work excellent, when user upload image, image uploaded to user folder with name "profile_image"
so now i want get this image when login open app/login
someone can help me?
if someone have skype it's will be great
i work with this guild: Android Uploading Camera Image, Video to Server with Progress Bar
and when i take image i got this error:
Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/profile_imagephoto.jpg: open failed: ENOENT (No such file or directory
What can i do to fix this?
Code:
//Upload Profile Image
profileImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
pictureCheck = "to_profile";
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_action_search);
builder.setMessage("Select What To Do:")
// Positive button functionality
.setPositiveButton("Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg0) {
Toast.makeText(MainActivity.this, "Open Camera...", Toast.LENGTH_SHORT).show();
Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraintent, IMAGE_CAPTURE);
}
})
// Negative button functionality
.setNegativeButton("Gallery",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg0) {
Toast.makeText(
MainActivity.this, "Open Gallery...", Toast.LENGTH_SHORT).show();
// Do more stuffs
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//dialog.cancel();
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
galleryIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(galleryIntent, RESULT_LOAD_INAGE);
}
});
// Create the Alert Dialog
AlertDialog alertdialog = builder.create();
// Show Alert Dialog
alertdialog.show();
}
});
/**
* Here we store the file url as it will be null after returning from camera
* app
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on screen orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
@Override
public void onActivityResult(int requestCodeToProfile, int resultCodeToProfile, Intent dataToProfile) {
super.onActivityResult(requestCodeToProfile, resultCodeToProfile, dataToProfile);
launchUploadActivity(true);
}
private void launchUploadActivity(boolean isImage){
Intent i = new Intent(MainActivity.this, UploadActivity.class);
i.putExtra("filePath", fileUri.getPath());
i.putExtra("isImage", isImage);
startActivity(i);
}
/**
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/**
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir =
new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"profile_image");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "Oops! Failed create "
+ "profile_image" + " directory");
return null;
}
}
// Create a media file name
java.util.Date date= new java.util.Date();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(date.getTime());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + "photo.jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
This is a long shot, but the path of your image looks wrong. Are you missing a slash between the last dir and 'photo.jpg'. Should be this line:
mediaFile = new File(mediaStorageDir.getPath() + "photo.jpg");
Hi! I am currently doing on a project and I keep hitting the same error despite making changes. I have been hitting indexOutOfBound error and unable to delete my listView item because of that error. I am doing on Tab and Database. Can anyone help me with my error and problem? Thank you!!
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at itp231.dba.nyp.com.mabel_createchallenge.mabel_tabs.mabelUncompleted_Tab1$2.onClick(mabelUncompleted_Tab1.java:124)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Here are by Java Codes
Code:
package itp231.dba.nyp.com.mabel_createchallenge;
import android.content.Context;
import android.database.Cursor;
import java.util.ArrayList;
import itp231.dba.nyp.com.mabel_createchallenge.mabel_database.mabel_MyDBAdpater;
/*
* Created by Guest Account on 13/7/2016.
*/
public class mabel_creatingChallengeApp {
private static mabel_creatingChallengeApp ourInstance = new mabel_creatingChallengeApp();
public static mabel_creatingChallengeApp getInstance() {
return ourInstance;
}
public mabel_creatingChallengeApp() {
challengesCreatedAL = new ArrayList<mabel_challenges>();
}
//* for mabelUncompleted_tab1.java */
private ArrayList<mabel_challenges> challengesCreatedAL;
public ArrayList<mabel_challenges> getArray() {
return challengesCreatedAL;
} //getting the array from ArrayList<mabel_challenges>
public ArrayList<mabel_challenges> getChallengesCreatedAL() {
return challengesCreatedAL;
}
//add and delete entries in the database
//add to database
//context --> context of current state of the application/object
//call it to get information regarding another part of your program (activity and package/application)
public static long addToDatabase(mabel_challenges challenges, Context c) {
mabel_MyDBAdpater db = new mabel_MyDBAdpater(c);
db.open();
long rowIDofInsertEntry = db.insertEntry(challenges);
db.close();
return rowIDofInsertEntry;
}
public static boolean deleteFromDatabase(int rowID, Context c) {
mabel_MyDBAdpater db = new mabel_MyDBAdpater(c);
db.open();
boolean updateStatus = db.removeEntry(rowID);
db.close();
return updateStatus;
}
public static boolean updateDatabase(mabel_challenges cc, int rowID, Context c) {
mabel_MyDBAdpater db = new mabel_MyDBAdpater(c);
db.open();
boolean updateStatus = db.updateEntry(rowID, cc);
db.close();
return updateStatus;
}
//populate array --> retrieve the array
//get the context --> get the content from the page
//store all retrieve data from database
public void populateArrayFromDB(Context c) {
challengesCreatedAL.clear();
mabel_MyDBAdpater db = new mabel_MyDBAdpater(c);
db.open();
Cursor cur = db.retrieveAllEntriesCursor();
cur.moveToFirst();
while(cur.moveToNext()) {
int rowID = cur.getInt(mabel_MyDBAdpater.COLUMN_KEY_ID);
String nameOfChallenge = cur.getString(mabel_MyDBAdpater.COLUMN_NAME_ID);
String descOfChallenge = cur.getString(mabel_MyDBAdpater.COLUMN_DESC_ID);
String durationOfChallenge = cur.getString(mabel_MyDBAdpater.COLUMN_DURATION_ID);
mabel_challenges newChallenge = new mabel_challenges(rowID, nameOfChallenge, descOfChallenge, durationOfChallenge);
challengesCreatedAL.add(newChallenge);
}
db.close();
}
}
Code:
package itp231.dba.nyp.com.mabel_createchallenge.mabel_tabs;
/*
fragment is part of an activity
*/
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import itp231.dba.nyp.com.mabel_createchallenge.R;
import itp231.dba.nyp.com.mabel_createchallenge.mabel_EditChallengeActivity;
import itp231.dba.nyp.com.mabel_createchallenge.mabel_challengeDetailActivity;
import itp231.dba.nyp.com.mabel_createchallenge.mabel_challenges;
import itp231.dba.nyp.com.mabel_createchallenge.mabel_creatingChallengeApp;
import itp231.dba.nyp.com.mabel_createchallenge.mabel_database.mabel_myChallengesListAdapter;
public class mabelUncompleted_Tab1 extends Fragment{
ListView listOfItemsLV;
ArrayList<mabel_challenges> challengesCreatedAL;
mabel_creatingChallengeApp cc;
public int selectedItem;
mabel_challenges c;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mabel_tab_1_uncompleted, container, false);
listOfItemsLV = (ListView) v.findViewById(R.id.challengesUncompletedLV);
registerForContextMenu(listOfItemsLV);
// addPage = (ImageButton) v.findViewById(R.id.addPage);
//calling out Instance Variable before the adapater
//to get challenges item on the list item
cc = mabel_creatingChallengeApp.getInstance();
//retrieve array from database
cc.populateArrayFromDB(getActivity().getApplicationContext()); //because is fragment so getActivity --> fragment is the contents in the tab -->getActivity will get the whole screen contents including contents in the tab
challengesCreatedAL = cc.getArray();
//Adapter for List View
mabel_myChallengesListAdapter challengesAdapter = new mabel_myChallengesListAdapter(getActivity(), challengesCreatedAL);
listOfItemsLV.setAdapter(challengesAdapter);
listOfItemsLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//getting the position of item in the array list
mabel_challenges c = challengesCreatedAL.get(i);
//intent for challenge detail
//mabel_challengeDetailActivity.class --> get to here
Intent viewDetailsIntent = new Intent(getActivity().getApplicationContext(), mabel_challengeDetailActivity.class);
//put extra --> Add extended data to the intent
viewDetailsIntent.putExtra(mabel_challenges.INTENT_NAME_CHALLENGENAME, c.getName());
viewDetailsIntent.putExtra(mabel_challenges.INTENT_NAME_DESCRIPTION, c.getDesc());
viewDetailsIntent.putExtra(mabel_challenges.INTENT_NAME_DURATION, c.getDuration());
viewDetailsIntent.putExtra("position", i);
startActivity(viewDetailsIntent);
}
});
return v;
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Options");
menu.add(1,1,1, "Edit");
menu.add(1,2,2, "Delete");
}
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
selectedItem = menuInfo.position;
//mabel_challenges c = challengesCreatedAL.get(selectedItem);
switch(item.getItemId()) {
case 1:
//edit challenge
Intent editChallenge = new Intent (getActivity(), mabel_EditChallengeActivity.class);
editChallenge.putExtra(mabel_challenges.INTENT_NAME_ARRAY_ITEM, selectedItem);
startActivity(editChallenge);
break;
case 2:
//delete challenge
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
dialogBuilder.setMessage("Confirm delete ?");
dialogBuilder.setPositiveButton("Delete" ,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mabel_myChallengesListAdapter challengeAdapter = new mabel_myChallengesListAdapter(getActivity().getApplicationContext(), challengesCreatedAL);
listOfItemsLV.setAdapter(challengeAdapter);
challengeAdapter.notifyDataSetChanged();
//prac 7b sales tracker -->delete the item
//selectedItem is the index of the array
mabel_creatingChallengeApp ca = mabel_creatingChallengeApp.getInstance();
int challengeId = ca.getArray().get(selectedItem).getId();
mabel_creatingChallengeApp.deleteFromDatabase(challengeId, getActivity().getApplicationContext());
ca.populateArrayFromDB(getActivity().getApplicationContext());
Toast.makeText(getActivity().getApplicationContext(), "Deleted!", Toast.LENGTH_LONG).show();
}
});
dialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// dialogBuilder.setCancelable(true);
Toast.makeText(getActivity().getApplicationContext(), "Cancelled!", Toast.LENGTH_LONG).show();
}
});
dialogBuilder.create();
dialogBuilder.show();
break;
}
return true;
}
@Override
public void onResume() {
super.onResume();
cc.populateArrayFromDB(getActivity().getApplicationContext());
}
}
Code:
package itp231.dba.nyp.com.mabel_createchallenge.mabel_database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import itp231.dba.nyp.com.mabel_createchallenge.mabel_challenges;
import itp231.dba.nyp.com.mabel_createchallenge.mabel_creatingChallengeApp;
/**
* Created by Guest Account on 13/7/2016.
* for uncompleted Tab
*/
public class mabel_MyDBAdpater {
private static final String DATABASE_NAME = "Challenges.db"; //name of database
private static final String DATABASE_TABLE = "ChallengesDatabase"; //database table name
private static final int DATABASE_VERSION = 2;
private SQLiteDatabase _db; //sqlite database handler
private final Context context; //current context
public static final String KEY_ID = "_id";
public static final int COLUMN_KEY_ID = 0;
public static final String ENTRY_CHALLENGE_NAME = "Name"; //name of column
public static final int COLUMN_NAME_ID = 1; //retrieval, position
public static final String ENTRY_CHALLENGE_DESC = "Description";
public static final int COLUMN_DESC_ID = 2;
public static final String ENTRY_CHALLENGE_DURATION = "Duration";
public static final int COLUMN_DURATION_ID = 3;
protected static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + " " + "(" + KEY_ID + " integer primary key autoincrement, " +
ENTRY_CHALLENGE_NAME + " Text, " + ENTRY_CHALLENGE_DESC + " Text, " + ENTRY_CHALLENGE_DURATION + " Text);";
//making debugging easier
//a fix pid for Eclipse debugger
//open and close method
private String mabel_MyDBAdapter_LOG_CAT = "MY_LOG";
private MyDBOpenHelper dbHelper;
public mabel_MyDBAdpater(Context _context)
{
this.context = _context;
dbHelper = new MyDBOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION); //help to create object
}
public void close()
{
_db.close();
Log.w(mabel_MyDBAdapter_LOG_CAT, "DB closed");
}
public void open() throws SQLiteException
{
try
{
_db = dbHelper.getWritableDatabase();
Log.w(mabel_MyDBAdapter_LOG_CAT, "DB opened as writable database");
}
catch(SQLiteException e)
{
_db = dbHelper.getReadableDatabase();
Log.w(mabel_MyDBAdapter_LOG_CAT, "DB opened as readable database");
}
}
public long insertEntry(mabel_challenges cc)
{
// Create a new record
ContentValues newEntryValues = new ContentValues();
// Assign values for each row
newEntryValues.put(ENTRY_CHALLENGE_NAME, cc.getName());
newEntryValues.put(ENTRY_CHALLENGE_DESC, cc.getDesc());
newEntryValues.put(ENTRY_CHALLENGE_DURATION, cc.getDuration());
// Insert the row
Log.w(mabel_MyDBAdapter_LOG_CAT, "Inserted EntryName = " + cc.getName()
+ " EntryDesc = " + cc.getDesc() + " EntryDuration = " + cc.getDuration() + " into table " + DATABASE_TABLE);
return _db.insert(DATABASE_TABLE, null, newEntryValues);
}
//removing data
public boolean removeEntry(long _rowIndex)
{
if (_db.delete(DATABASE_TABLE, KEY_ID + " = " + _rowIndex, null) <= 0)
{
Log.w(mabel_MyDBAdapter_LOG_CAT, "Removing entry where id = "
+ _rowIndex + " Failed");
return false;
}
Log.w(mabel_MyDBAdapter_LOG_CAT, "Removing entry where id = "
+ _rowIndex + " Success");
return true;
}
//update method
public boolean updateEntry(long rowIndex, mabel_challenges cc) {
ContentValues updateValues = new ContentValues();
mabel_creatingChallengeApp ca = mabel_creatingChallengeApp.getInstance();
updateValues.put(ENTRY_CHALLENGE_NAME, cc.getName());
updateValues.put(ENTRY_CHALLENGE_DESC, cc.getDesc());
updateValues.put(ENTRY_CHALLENGE_DURATION, cc.getDuration());
String where = KEY_ID + "=" + rowIndex; //selected id for updating data
Log.w(mabel_MyDBAdapter_LOG_CAT, "Updated Challenge Name = " + cc.getName() + "Update Challenge Description = " + cc.getDesc() + "Update Duration = " + cc.getDuration() + " into table " +DATABASE_TABLE);
if (_db.update(DATABASE_TABLE, updateValues, where, null) <= 0) {
return true; //return success
}
return false; //newer update anything
}
//retrieve method
public Cursor retrieveAllEntriesCursor()
{
Cursor c = null;
try
{
c = _db.query(DATABASE_TABLE, new String[] {KEY_ID,ENTRY_CHALLENGE_NAME, ENTRY_CHALLENGE_DESC, ENTRY_CHALLENGE_DURATION}, null, null, null, null, null);
}
catch(SQLiteException e)
{
Log.w(mabel_MyDBAdapter_LOG_CAT, "Retrieve fail!");
}
return c;
}
public class MyDBOpenHelper extends SQLiteOpenHelper
{
public MyDBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override //compulsory method
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
Log.w(mabel_MyDBAdapter_LOG_CAT, "Helper : DB " + DATABASE_TABLE + " Created!!");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
}
} // End of myDBOpenHelper
}
Can help me to see what's wrong? Thank you !!!!
I can't read the all code, it's too long, but ai think that you want to access for example array [5], but array length is smaller.
Trimis de pe al meu Sony Z2 D6503
mabelll said:
Hi! I am currently doing on a project and I keep hitting the same error despite making changes. I have been hitting indexOutOfBound error and unable to delete my listView item because of that error. I am doing on Tab and Database. Can anyone help me with my error and problem? Thank you!!
Click to expand...
Click to collapse
Uncompleted_Tab1 onClick(mabelUncompleted _Tab1.java:124)
cannot see the line number. check line 124 for yourself.
Goodmorning, I've tried to implement the method that request permission first and than execute something but without success ...
Here is my MainActivity code:
Code:
public class MainActivity extends AppCompatActivity {
public static String latitudineCorrente = ""; //current Latitude
public static String longitudineCorrente = ""; //current Longitude
private SharedPreferences sharedPreferences; //shared preferences
private SharedPreferences.Editor mEditor; // shared preferences editor
/* Variable for getting location */
private FusedLocationProviderClient fusedLocationProviderClient;
private LocationRequest locationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String appLanguage = "ita";
LanguageUtil.setAppLanguage(getApplicationContext(), appLanguage);
/* .... other code.... */
sharedPreferences = this.getSharedPreferences("appname", MODE_PRIVATE);
mEditor = sharedPreferences.edit(); // set edit sharedpreferences
richiamaPermessi(); //permission request
requestLocationUpdates(); // function requestlocationupdates();
CaricamentoInizialeApplicazione(); // function for presetting configurations
}
private void CaricamentoInizialeApplicazione() {
boolean configIniziale = sharedPreferences.getBoolean("configurazioneinizialeok", false);
if(!configIniziale)
{
/* can't execute this because Double.parseDouble(this.latitudineCorrente), Double.parseDouble(this.longitudineCorrente) */
ApiMarkers.getMarkers(MainActivity.this, 0, 400,
Double.parseDouble(this.latitudineCorrente), Double.parseDouble(this.longitudineCorrente),
0, 0,0,1, 0,0,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
/* response elaborations */
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "" + error.getMessage(), Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
mEditor.putBoolean("configurazioneinizialeok", true);
mEditor.commit();
}
}
/* Function for requist location updates */
public void requestLocationUpdates() {
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) ==
PermissionChecker.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
PermissionChecker.PERMISSION_GRANTED) {
fusedLocationProviderClient = new FusedLocationProviderClient(this);
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setFastestInterval(1500);
locationRequest.setInterval(3000);
fusedLocationProviderClient.requestLocationUpdates(locationRequest, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
mEditor = sharedPreferences.edit();
mEditor.putString("latitudine", String.valueOf(locationResult.getLastLocation().getLatitude()));
mEditor.putString("longitudine", String.valueOf(locationResult.getLastLocation().getLongitude()));
MainActivity.latitudineCorrente = String.valueOf(locationResult.getLastLocation().getLatitude());
MainActivity.longitudineCorrente = String.valueOf(locationResult.getLastLocation().getLongitude());
Log.d("coordinate", "MAIN ACTIVITY: Latitudine: " + MainActivity.latitudineCorrente + "/"
+ "Longitudine: " + MainActivity.longitudineCorrente);
}
}, getMainLooper());
} else richiamaPermessi();
}
/* Function for requesting access */
public void richiamaPermessi()
{
Permissions.check(this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
"Permessi di locazione obbligatori per determinare la propria posizione!",
new Permissions.Options().setSettingsDialogTitle("Avviso").setRationaleDialogTitle("Permessi locazione"),
new PermissionHandler() {
@Override
public void onGranted() {
requestLocationUpdates();
}
@Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
super.onDenied(context, deniedPermissions);
richiamaPermessi();
}
});
}
}
The problem is executing function ApiMarkers.getMarkers(.... with latitude and longitude filled ...) but can't execute because latitude and longitude are not loaded... I spent two nights without solution Thanks for any help! Cristian
Up! I tried to follow this video on youtube youtube.com/watch?v=gEcFf2Mv4L0 and in any case I could not understand where or when I can call the method "CaricamentoInizialeApplicazione ()" in order to call a method that uses the location coordinates! What I want is to make sure that I don't call the method already said until the location coordinates are loaded into the variables indicated in the code! Is it possible that someone is not able to solve the problem? Thank you
Oh thanks for any help!