So, I tried to call all the methods from overView but is not working, I tried with "view" in the parentheses and then is constantly crashing when I start the app.... Can someone help, this is the whole code. I tried not to include the first four methods but then is not working the way it's suppose to.
public class MainActivity extends AppCompatActivity {
int health = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void checkQuestionOne(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_one_yes:
if(checked)
health += 1;
break;
case R.id.question_one_no:
if(checked)
health -= 1;
break;
}
}
public void checkQuestionTwo(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_two_yes:
if(checked)
health += 1;
break;
case R.id.question_two_no:
if(checked)
health -= 1;
break;
}
}
public void checkQuestionThree(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_three_yes:
if(checked)
health += 1;
break;
case R.id.question_three_no:
if(checked)
health -= 1;
break;
}
}
public void checkQuestionFour(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.question_four_yes:
if(checked)
health += 1;
break;
case R.id.question_four_no:
if(checked)
health -= 1;
break;
}
}
public void checkQuestionFive() {
EditText gettingQuestionFive = findViewById(R.id.sleep_hours);
int answerQuestionFive = Integer.parseInt(gettingQuestionFive.getText().toString());
if (answerQuestionFive > 7) {
health += 1;
} else if (answerQuestionFive == 7) {
health += 1;
} else {
health -=1;
}
}
private void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.summary);
orderSummaryTextView.setText(message);
}
public void overView(View view){
checkQuestionOne();
checkQuestionTwo();
checkQuestionThree();
checkQuestionFour();
checkQuestionFive();
String Message = health + " is your score. If is 3 or above, you have a healthy life";
displayMessage(Message);
}
}
I think you have forgot to call the overView() method in the onCreate() method of activity. You can call it like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Pass your view in argument
overView(View view)
}
I'm a newbie. I'm editing Searchable Dictionary given to sample on SDK.
Dictionary follows a database on \res\raw\definitions.txt
when searching word shows result (defination) like below-
line1 line2 line3
But I want to add line break on result and show (defination) like below-
line1
line2
line3
code of WordActivity.java is here
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word);
Uri uri = getIntent().getData();
Cursor cursor = managedQuery(uri, null, null, null, null);
if (cursor == null) {
finish();
} else {
cursor.moveToFirst();
TextView word = (TextView) findViewById(R.id.word);
TextView definition = (TextView) findViewById(R.id.definition);
int wIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_WORD);
int dIndex = cursor.getColumnIndexOrThrow(DictionaryDatabase.KEY_DEFINITION);
word.setText(cursor.getString(wIndex));
definition.setText(cursor.getString(dIndex));
}}
public void onBackPressed() {
onSearchRequested();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// TODO Auto-generated method stub
case R.id.search:
onSearchRequested();
return true;
case R.id.about:
setContentView(R.layout.abouttanzil);
return true;
case R.id.exit:
moveTaskToBack(true);
return true;
default:
return false;
}
}
}
Please let me know what should i add and where to make line break??
Thanks in advance.
Display it as HTML and use \n.
yourTextView.setText(Html.fromHtml(someText + "\n" + someOtherText));
Obviously there are smarter ways. Just showing you the Html.fromHtml() method.
I would like to show the context menu of my activity while keeping the soft keyboard shown. Here is my code in creating menu for my main activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
MenuItem m = menu.add(0, LOGIN_MENU_ITEM, 0, "Login");
m.setIcon(android.R.drawable.ic_menu_preferences);
m = menu.add(0, CONFIGURE_MENU_ITEM, 0, R.string.menu_settings);
m.setIcon(android.R.drawable.ic_menu_preferences);
m = menu.add(0, EXIT_MENU_ITEM, 0, R.string.menu_exit);
m.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
m = menu.add(0, ABOUT_MENU_ITEM, 0, R.string.menu_about);
m.setIcon(android.R.drawable.ic_menu_info_details);
return result;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean result = super.onOptionsItemSelected(item);
Intent intent = null;
switch (item.getItemId()) {
case ABOUT_MENU_ITEM:
if (m_AlertDlg != null)
{
m_AlertDlg.cancel();
}
final SpannableString s = new SpannableString(getString(R.string.about).replace("\\n","\n").replace("${VERSION}", getVersion(this)));
Linkify.addLinks(s, Linkify.WEB_URLS);
m_AlertDlg = new AlertDialog.Builder(this)
// .setMessage(getString(R.string.about).replace("\\n","\n").replace("${VERSION}", getVersion(this)))
.setMessage(s)
.setTitle(getString(R.string.menu_about))
.setIcon(R.drawable.icon22_click)
.setCancelable(true)
.show();
((TextView)m_AlertDlg.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
break;
case EXIT_MENU_ITEM:
on(this,false);
// final Boolean exit_rem = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Settings.PREF_REMEMBER, false);
// if (!exit_rem){
// Editor edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
// edit.putString(Settings.PREF_USERNAME, "");
// edit.putString(Settings.PREF_PASSWORD, "");
// edit.commit();
// }
Receiver.pos(true);
Receiver.engine(this).halt();
Receiver.mSipdroidEngine = null;
Receiver.reRegister(0);
stopService(new Intent(this,RegisterService.class));
finish();
break;
case CONFIGURE_MENU_ITEM:
try {
intent = new Intent(this, org.sparksoftsip.sipua.ui.Settings.class);
startActivity(intent);
} catch (ActivityNotFoundException e) {
}
break;
case LOGIN_MENU_ITEM:
LoginScreen();
break;
}
return result;
}
In my main activity, I've built a custom AutoCompleteTextView, wherein it receives focus on start. I listen to key presses here, and plans to open the context menu by listening to the menu button. However nothing is happening to my current code:
public class InstantAutoCompleteTextView extends AutoCompleteTextView {
public InstantAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
}
private Activity getActivity() {
Context context = getContext();
if (context instanceof Activity) {
return (Activity) context;
} else {
return null;
}
}
#Override
public boolean enoughToFilter() {
return true;
}
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event)
{
Activity activity = getActivity();
if (KeyEvent.KEYCODE_BACK == event.getKeyCode() && event.getAction () == KeyEvent.ACTION_DOWN)
{
Log.v("", "Back Pressed");
activity.onBackPressed();
//Want to call this method which will append text
//init();
}
if (KeyEvent.KEYCODE_MENU == event.getKeyCode() && event.getAction () == KeyEvent.ACTION_DOWN)
{
Log.e("hello", event.toString());
activity.openContextMenu(activity.getWindow().getDecorView().findViewById(android.R.id.content));
}
return true;
//return super.dispatchKeyEventPreIme(event);
}
}
I have tried using
openContextMenu(activity.getWindow().getDecorView().findViewById(android.R.id.content))
however, nothing is happening. I have also tried (activity.findViewById(android.R.id.content)) and (activity.findViewById(android.R.id.content).getViewRoot()), but nothing shows up. How do I remedy this?
It looks I made a foolish mistake. I did not use a context menu, but rather an options menu. So, the code for my on key press should be
openOptionsMenu()
instead of using
openContextMenu(View)
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb = new AlertDialog.Builder(CategoriesTab.this);
adb.setTitle("Selected Category");
adb.setMessage("Selected Item is = "+lv1.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
This at the moment displays an alertbox when an item from listview is clicked. I want to convert the alertbox to load a specific xml for each choices clicked. How can i do this?
thanks for your help.
switch(position) {
case 0:
setContentView(R.layout.xml0);
break;
case 1:
setContentView(R.layout.xml1);
break;
default:
setContentView(R.layout.default);
break;
}
i hope this will do the job!
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.:
break;
case R.id.:
break;
default:
break;
}
}
switch(position) {
case 0:
...
break;
case 1:
...
break;
default:
...
}
Did you mean that?
You can do this:
#Override
protected Dialog onCreateDialog(int id) {
String messageDialog;
String valueOK;
String valueCancel;
String titleDialog;
switch (id) {
case id:
titleDialog = itemTitle;
messageDialog = itemDescription
valueOK = "OK";
return new AlertDialog.Builder(HomeView.this).setTitle(titleDialog).setPositiveButton(valueOK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.d(this.getClass().getName(), "AlertItem");
}
}).setMessage(messageDialog).create();
and then call to
showDialog(numbreOfItem);
I'm using SQlite database to store my listview data.
I added opportunities to change listview items, delete them by swiping etc. Everything works fine when I run my app using IDE - data can be deleted or changed, but when I'm trying to restart it using my phone, I get this error:
02-04 22:13:03.276 31682-31682/com.example.jeavie.deadlineyesterday E/InputEventReceiver: Exception dispatching input event.
02-04 22:13:03.276 31682-31682/com.example.jeavie.deadlineyesterday E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
02-04 22:13:03.278 31682-31682/com.example.jeavie.deadlineyesterday E/MessageQueue-JNI: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.jeavie.deadlineyesterday.MainActivity$2.onTouch(MainActivity.java:411)
at android.view.View.dispatchTouchEvent(View.java:11772)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2962)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2643)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:448)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1829)
at android.app.Activity.dispatchTouchEvent(Activity.java:3307)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:410)
at android.view.View.dispatchPointerEvent(View.java:12015)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4795)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4609)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4293)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4350)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6661)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6635)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6596)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6764)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:186)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:325)
at android.os.Looper.loop(Looper.java:142)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.
My MainActivity class:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public final static int INTENT_REQUEST_CODE = 1;
public final static int INTENT_REQUEST_CODE_TWO = 2;
public static int INTENT_RESULT_CODE = 1;
public static int INTENT_RESULT_CODE_TWO = 2;
public final static int INTENT_EMPTY_CODE = 0;
public static Integer listNumber = 0;
public static Integer dataNumber = 1;
public static Integer editNumber = 1;
//Swiping
private boolean mSwiping = false; // detects if user is swiping on ACTION_UP
private boolean mItemPressed = false; // Detects if user is currently holding down a view
private ListView listView;
DeadlineActivityAdapter deadlineActivityAdapter;
List<DeadlineActivity> list;
String summary, getData, getTime;
DbActivity db;
Cursor fullData;
boolean full;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
db = new DbActivity(this);
fullData = db.getAllData();
if (fullData.getCount() > 0){
if (fullData.moveToFirst()) {
list = new ArrayList<>();
do {
String check = fullData.getString(7);
if (check.startsWith("li")){
summary=fullData.getString(2);
getData=fullData.getString(3);
getTime=fullData.getString(4);
String deadline=fullData.getString(5);
String tags=fullData.getString(6);
list.add(new DeadlineActivity(String.valueOf(dataNumber - 1), summary, getData, getTime, deadline,
tags));
full = true;
listNumber++;
}
} while (fullData.moveToNext());
}
}
if (!full) list = new ArrayList<>();
Toolbar toolbar = findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
FloatingActionButton addTask = findViewById(R.id.addTask);
addTask.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, AddTaskActivity.class);
startActivityForResult(intent, INTENT_REQUEST_CODE);
}
});
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.navigation);
navigationView.setNavigationItemSelectedListener(this);
listView = findViewById(R.id.listDeadlines);
TextView emptyText = findViewById(android.R.id.empty);
listView.setEmptyView(emptyText);
deadlineActivityAdapter = new DeadlineActivityAdapter(this, list, mTouchListener);
listView.setAdapter(deadlineActivityAdapter);
}
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == INTENT_RESULT_CODE){
if(resultCode == INTENT_RESULT_CODE) {
Cursor newDeadline = db.getAllData();
newDeadline.moveToLast();
summary=newDeadline.getString(2);
getData=newDeadline.getString(3);
getTime=newDeadline.getString(4);
String deadline=newDeadline.getString(5);
String tags=newDeadline.getString(6);
list.add(new DeadlineActivity(String.valueOf(dataNumber), summary, getData, getTime, deadline,
tags));
dataNumber++;
deadlineActivityAdapter.notifyDataSetChanged();
super.onActivityResult(requestCode, resultCode, data);
}
}
else if (requestCode == INTENT_RESULT_CODE_TWO){
if (resultCode == INTENT_RESULT_CODE_TWO) {
String a = list.get(editNumber).getId();
Cursor newDeadline = db.getData(a);
String id = newDeadline.getString(0);
summary=newDeadline.getString(1);
getData=newDeadline.getString(2);
getTime=newDeadline.getString(3);
String deadline=newDeadline.getString(4);
String tags=newDeadline.getString(5);
list.remove(Integer.valueOf(id) - 1);
list.add(Integer.valueOf(id) - 1, new DeadlineActivity(id, summary, getData, getTime, deadline, tags));
deadlineActivityAdapter.notifyDataSetChanged();
}
}
}
#Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.toWeek:
startActivity(new Intent(this, WeekActivity.class));
return true;
case R.id.history:
startActivity(new Intent(this, HistoryActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.tags:
startActivity(new Intent(this, TagsActivity.class));
return true;
case R.id.notifications:
// startActivity(new Intent(this, HistoryActivity.class));
return true;
case R.id.info:
// startActivity(new Intent(this, HistoryActivity.class));
return true;
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private View.OnTouchListener mTouchListener = new View.OnTouchListener()
{
float mDownX;
private int mSwipeSlop = -1;
boolean swiped;
#Override
public boolean onTouch(final View v, MotionEvent event) {
if (mSwipeSlop < 0) {
mSwipeSlop = ViewConfiguration.get(MainActivity.this).getScaledTouchSlop();
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mItemPressed) return false; // Doesn't allow swiping two items at same time
mItemPressed = true;
mDownX = event.getX();
swiped = false;
break;
case MotionEvent.ACTION_CANCEL:
v.setTranslationX(0);
mItemPressed = false;
break;
case MotionEvent.ACTION_MOVE:
{
float x = event.getX() + v.getTranslationX();
float deltaX = x - mDownX;
float deltaXAbs = Math.abs(deltaX);
if (!mSwiping) {
if (deltaXAbs > mSwipeSlop) {
mSwiping = true; // tells if user is actually swiping or just touching in sloppy manner
listView.requestDisallowInterceptTouchEvent(true);
}
}
if (mSwiping && !swiped) { // Need to make sure the user is both swiping and has not already completed a swipe action (hence mSwiping and swiped)
v.setTranslationX((x - mDownX)); // moves the view as long as the user is swiping and has not already swiped
if (deltaX > v.getWidth() / 3) { // swipe to right
mDownX = x;
swiped = true;
mSwiping = false;
mItemPressed = false;
v.animate().setDuration(300).translationX(v.getWidth()/3);
int i = listView.getPositionForView(v);
String a = list.get(i).getId();
Cursor newDeadline = db.getData(a);
String id = newDeadline.getString(0);
summary=newDeadline.getString(1);
getData=newDeadline.getString(2);
getTime=newDeadline.getString(3);
String deadline=newDeadline.getString(4);
String tags=newDeadline.getString(5);
boolean isInserted = db.updateData(id, id, summary, getData, getTime, deadline, tags, "history");
if (isInserted)
Toast.makeText(getApplicationContext(), "Deadline completed", Toast.LENGTH_SHORT).show();
list.remove(i);
deadlineActivityAdapter.notifyDataSetChanged();
return true;
}
else if (deltaX < -1 * (v.getWidth() / 3)) { // swipe to left
mDownX = x;
swiped = true;
mSwiping = false;
mItemPressed = false;
v.animate().setDuration(300).translationX(-v.getWidth()/3);
int i = listView.getPositionForView(v);
String a = list.get(i).getId();
Cursor newDeadline = db.getData(a);
String id = newDeadline.getString(0);
summary=newDeadline.getString(1);
getData=newDeadline.getString(2);
getTime=newDeadline.getString(3);
String deadline=newDeadline.getString(4);
String tags=newDeadline.getString(5);
boolean isInserted = db.updateData(id, id, summary, getData, getTime, deadline, tags, "history");
if (isInserted)
Toast.makeText(getApplicationContext(), "Deadline completed", Toast.LENGTH_SHORT).show();
list.remove(i);
deadlineActivityAdapter.notifyDataSetChanged();
return true;
}
}
}
break;
case MotionEvent.ACTION_UP: {
if (mSwiping) { // if the user was swiping, don't go to the and just animate the view back into position
v.animate().setDuration(300).translationX(0).withEndAction(new Runnable() {
#Override
public void run() {
mSwiping = false;
mItemPressed = false;
listView.setEnabled(true);
}
});
}
else { // user was not swiping; registers as a click
//set item click "animation"
ColorDrawable[] color = {
new ColorDrawable(getColor(R.color.grey)),
new ColorDrawable(getColor(R.color.dark_dark_grey))
};
TransitionDrawable trans = new TransitionDrawable(color);
v.setBackground(trans);
trans.startTransition(1000); // duration 2 seconds
// Go back to the default background color of Item
ColorDrawable[] color2 = {
new ColorDrawable(getColor(R.color.dark_dark_grey)),
new ColorDrawable(getColor(R.color.the_darkest_grey))
};
TransitionDrawable trans2 = new TransitionDrawable(color2);
v.setBackground(trans2);
trans2.startTransition(1000); // duration 2 seconds
editNumber = listView.getPositionForView(v);
String a = list.get(editNumber).getId();
Cursor newDeadline = db.getData(a);
String id = newDeadline.getString(0);
Intent intent = new Intent();
intent.putExtra("number", id);
intent.setClass(MainActivity.this, EditTaskActivity.class);
startActivityForResult(intent, INTENT_REQUEST_CODE_TWO);
mItemPressed = false;
listView.setEnabled(true);
return true;
}
}
default:
return false;
}
return true;
}
};
}
I added OnTouch method.
At a guess you may be a little confused with how to handle cursors.
The rule so very often found as broken is that a Cursor, unless specifically set to null, will NEVER be null. Any check for a null Cursor is more than likely unnecessary.
Additionally moveToFirst without then doing something with that first row may well result in handling issues.
As such I'd suggest that you change the getData method to be something like :-
public Cursor getData(String id) {
SQLiteDatabase db = this.getReadableDatabase();
return dq.query(true,
new String[] { DB_NUMBER, DB_SUMMARY,
DB_DATE, DB_TIME, DB_DEADLINE, DB_TAGS},
DB_ID + "=?",
new String[]{id},
null,null,null,null
);
}
i.e. just return the cursor as the null check is useless, moveToFirst is quite likely to result in issues.
In the code that calls the getData method, as it's finding by id and thus only find 1 row then :-
Cursor csr = mydbhlpr.getData();
if(csr.moveToFirst) {
//your code to retrieve and act upon the data from the cursor
} else {
//your code, if needed, to handle no row being found.
}
My guess is that you've assumed done something like :-
Cursor csr = mydbhlpr.getData(); // assuming it is not empty and moved to first row
String mydata = csr.getString(whatever_column_offset);
Note, this is just guessing at what your issue may be (see comment re adding onTouch)
your data base is empty and you are requesting for a row from it
that's what the error says
populate your database before querying it for rows
else include a if statement by checking the cursor size
call the DatabaseHelper insertData function from your MainActivity to populate the database
From your MainActivity you are just querying data from database
I do not see anywhere that you have inserted data to you database