6. MainActivity.java package id.co.lazada.flanealdemerchant;
import android.content.Context; import android.content.Intent;
import android.content.SharedPreferences; import android.os.AsyncTask;
import android.os.Bundle; import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils; import android.util.Patterns;
import android.view.Menu; import android.view.MenuItem;
import android.view.View; import android.widget.Button;
import android.widget.EditText; import android.widget.RadioButton;
import android.widget.RadioGroup; import android.widget.Toast;
import org.json.JSONArray; import org.json.JSONException;
import org.json.JSONObject; import java.io.IOException;
import java.io.InputStream; import
id.co.lazada.flanealdemerchant.helper.DbHelperCategory; import
id.co.lazada.flanealdemerchant.helper.DbHelperQuisioner; import id.co.lazada.flanealdemerchant.model.User;
public class MainActivity extends ActionBarActivity { private DbHelperCategory helper;
EditText userName; EditText userEmail;
RadioButton userGender; Override
protected void onCreateBundle savedInstanceState { super.onCreatesavedInstanceState;
setContentViewR.layout.activity_main; helper = new
DbHelperCategorygetApplicationContext; userName = EditText
findViewByIdR.id.userEditText;
userEmail = EditText findViewByIdR.id.emailEditText;
Button next = Button findViewByIdR.id.nextButton;
next.setOnClickListenernew View.OnClickListener {
Override public void onClickView view {
nextProcess; }
}; }
Override protected void onStart {
super.onStart; ItemDataConfig itemConfig = new ItemDataConfig;
itemConfig.execute; }
public void nextProcess { String name =
userName.getText.toString.trim; String email =
userEmail.getText.toString.trim; RadioGroup genderGroup = RadioGroup
findViewByIdR.id.radioGroup; int radioSelected =
genderGroup.getCheckedRadioButtonId; if name.length == 0 ||
email.length == 0 || isValidEmailuserEmail.getText ||
radioSelected == -1 { Toast.makeTextgetApplicationContext,
Invalid data, Toast.LENGTH_LONG.show; } else {
userGender = RadioButton findViewByIdradioSelected;
int gender = 1; if
userGender.getText.toString.equalsFemale { gender = 2;
} User isUser =
helper.getRowByEmailAddressemail; Long userId;
if isUser = null { userId = isUser.getId;
} else {
userId = helper.addNewUsername, email, gender;
} helper.closeDatabase;
SharedPreferences preferences = getSharedPreferencesuser, Context.MODE_PRIVATE;
SharedPreferences.Editor editor = preferences.edit;
editor.putIntuserId, userId.intValue; editor.apply;
Intent intent = new Intentthis, UserQuestionActivity.class;
startActivityintent; finish;
} }
public final static boolean isValidEmailCharSequence target {
if TextUtils.isEmptytarget { return false;
} else { return
Patterns.EMAIL_ADDRESS.matchertarget.matches; }
} Override
public void onActivityResultint requestCode, int resultCode, Intent data {
if resultCode == RESULT_OK { String notify =
data.getExtras.getStringresult; Toast.makeTextgetApplicationContext,
notify, Toast.LENGTH_LONG.show; }
} Override
public boolean onCreateOptionsMenuMenu menu { getMenuInflater.inflateR.menu.menu_main,
menu; return true;
} Override
public boolean onOptionsItemSelectedMenuItem item { int id = item.getItemId;
if id == R.id.option_administrator { Intent intent = new
IntentgetApplicationContext, AdminActivity.class; intent.addFlagsIntent.FLAG_ACTIVITY_NEW_TASK;
intent.addFlagsIntent.FLAG_ACTIVITY_CLEAR_TASK; startActivityintent;
} return super.onOptionsItemSelecteditem;
} public class ItemDataConfig extends AsyncTaskVoid,
Void, Void { public ItemDataConfig {
} Override
protected Void doInBackgroundVoid... params { DbHelperQuisioner quisionerHelper = new
DbHelperQuisionergetApplication; DbHelperCategory categoryHelper = new
DbHelperCategorygetApplication; quisionerHelper.removeAllQuestion;
categoryHelper.removeAllCategoryItems; try {
JSONArray jArrayQuestion = new JSONArrayloadJsonFromAssetquestion.json;
for int x = 0; x jArrayQuestion.length; x++ {
JSONObject question = jArrayQuestion.getJSONObjectx;
Integer catId = question.isNullcategoryId ?
question.getIntcategoryId : null; String askType =
question.isNullquizType ? question.getStringquizType : null;
String ask = question.isNullquestion ?
question.getStringquestion : null; String one =
question.isNulloptOne ? question.getStringoptOne : null;
String two = question.isNulloptTwo ? question.getStringoptTwo
: null;
String three = question.isNulloptThree ?
question.getStringoptThree : null; String four =
question.isNulloptFour ? question.getStringoptFour : null;
String five = question.isNulloptFive ?
question.getStringoptFive : null; quisionerHelper.addQuisionercatId,
askType, ask, one, two, three, four, five; }
JSONArray jArrayItems = new JSONArrayloadJsonFromAssetitems.json;
for int z = 0; z jArrayItems.length; z++ {
JSONObject item = jArrayItems.getJSONObjectz;
Integer catId = item.isNullcatId ? item.getIntcatId : null;
String merk = item.isNullmerk ? item.getStringmerk : null;
String type = item.isNulltype ? item.getStringtype : null;
String capacity = item.isNullcapacity ? item.getStringcapacity :
null; String speed = item.isNullspeed
? item.getStringspeed : null; String price = item.isNullprice
? item.getStringprice : null; categoryHelper.addItemcatId, merk,
type, capacity, speed, price; }
} catch JSONException e {
e.printStackTrace; }
return null; }
public String loadJsonFromAssetString fileName {
String ret = null; try {
InputStream inputStream = getAssets.openfileName;
int size = inputStream.available;
byte[] buffer = new byte[size]; inputStream.readbuffer;
inputStream.close; ret = new Stringbuffer, UTF-8;
} catch IOException e { e.printStackTrace;
return null; }
return ret; }
} }
7. QuestionerActivity.java package id.co.lazada.flanealdemerchant;