What am I doing wrong? I cannot get my data to transfer from where I click it on the list to the detailed view activity that the click opens.
Here is my code at the button click... and the data is available here due to the fact that the list shows the items.
RecipeRepo repo = new RecipeRepo(this);
final ArrayList<HashMap<String, String>> recipeList = repo.getRecipeList();
if(recipeList.size()!=0) {
ListView lv = (ListView) findViewById(R.id.list);//getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
recipe_Id = (TextView) view.findViewById(R.id.recipe_Id);
String recipeId = recipe_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),RecipeDetail.class);
objIndent.putExtra("recipe_Id", Integer.parseInt( recipeId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( SousChef.this,recipeList, R.layout.view_recipe_entry, new String[] { "id","name"}, new int[] {R.id.recipe_Id, R.id.recipe_list_name});
lv.setAdapter(adapter);
}else {
Toast.makeText(this, "No recipe!", Toast.LENGTH_SHORT).show();
}
Like I said, I'm the data is available at this point because the listview shows the ID and the Name of each item.
After I click it using the following code... the activity is blank, and the toast in the item below comes up ID 0
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
btnEdit = (Button) findViewById(R.id.detail_edit);
btnClose = (Button) findViewById(R.id.detail_close);
textName = (EditText) findViewById(R.id.detail_recipe_name);
textIngredients = (EditText) findViewById(R.id.detail_recipe_ingredients);
textInstruct = (EditText) findViewById(R.id.detail_recipe_instruct);
textCookTemp = (EditText) findViewById(R.id.detail_cook_temp);
textCookTime = (EditText) findViewById(R.id.detail_recipe_cooktime);
textServes = (EditText) findViewById(R.id.detail_recipe_servings);
btnEdit.setOnClickListener(this);
btnClose.setOnClickListener(this);
_Recipe_Id =0;
Intent intent = getIntent();
_Recipe_Id = intent.getIntExtra("recipe_Id", 0);
RecipeRepo repo = new RecipeRepo(this);
Recipe recipe = new Recipe();
recipe = repo.getRecipeById(_Recipe_Id);
textName.setText(recipe.name);
Toast.makeText(this, "Recipe id is " + recipe.recipe_Id, Toast.LENGTH_SHORT).show();
textIngredients.setText(recipe.ingredients);
textInstruct.setText(recipe.instructions);
textCookTemp.setText(recipe.cooktemp);
textCookTime.setText(recipe.cooktime);
textServes.setText(recipe.serves);
From everything I have read this should be working but I must be leaving something out. Also I am not producing any errors in the logCat or anything.
Word of advise... always make sure you mark the correct columns in your database... A simple mistake can cause you 2 days worth of work to go out the window. In this case I had the getRecipeByID code looking for the column category instead of the column recipe_Id. simple fix but could have been avoided.
Related
I'm developing an application which contains a listview with some article but I have confusion with using of notifyDataSetChanged().
My confusion derived from insert operation because I want add a new item to listview from dialog. The "problem" is on add button click of dialog: I create a new Product item and add to productsList then call notifyDataSetChanged() to refresh listview, but I could not call it because my listview is updated anyway and I not understand if it is correct or not also because if I not use dialog to add, without notify method listview doesn't refresh.
I did some research and all of them use notify method.
Below the code that I use in my Fragment:
productsList = new ArrayList<>();
mAdapter = new products_list_adapter(getActivity(), productsList);
asyncProducts = new GetProductsAsyncTasks(getActivity(), productsList, listViewProducts);
asyncProducts.execute();
final Dialog dialog = new Dialog(getActivity());
btnAggiungiProdotto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.setTitle("Add");
dialog.setContentView(R.layout.input_dialog);
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, 1000);
dialog.setCancelable(true);
Button btnConferma = (Button) dialog.findViewById(R.id.btnConferma);
btnConferma.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText txtDescr = (EditText) dialog.findViewById(R.id.edtDescrizione);
EditText txtBarcode = (EditText) dialog.findViewById(R.id.edtBarocode);
Product p = new Product();
p.SetDescription(txtDescr.getText().toString());
p.SetBarCode(txtBarcode.getText().toString());
productsList.add(p);
mAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
I would know this behaviour is correct or not.
I have created Activity with a listView which uses setOnItemClickListener to determine which index has been pressed. Then it should pass the data into the next activity based on the selected index.
Passing of the values works, but it only works for the first listView index pressed after the app is reloaded.
To make it more clear. When the app is loaded I can press on any index of the ListView which will pass the data to the next Activity and display it correctly, but then when I go back to the ListView and select different index it still displays the same data from the previous index without updating it. Therefore, I have to reload the app to get new index results displayed.
Here is my ViewListActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_artist);
artistList = (ListView) findViewById(R.id.artistList);
databasehandler = new SqliteHandler(ViewArtistActivity.this);
allArtists = databasehandler.getAllArtists();
artistName = new ArrayList<>();
singleArtistDetails = new ArrayList<>();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (allArtists.size() > 0) {
for (int i=0; i < allArtists.size(); i++) {
ArtistModel artistModel = allArtists.get(i);
artistName.add(artistModel.getArtistName());
}
}
adapter = new ArrayAdapter(ViewArtistActivity.this, android.R.layout.simple_list_item_1, artistName);
artistList.setAdapter(adapter);
artistList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ArtistModel artistModel = allArtists.get(position);
singleArtistDetails.add(artistModel.getArtistName());
singleArtistDetails.add(artistModel.getArtistDOB());
singleArtistDetails.add(artistModel.getArtistBiography());
Intent intent = new Intent(ViewArtistActivity.this, SavedArtistDetailsActivity.class);
intent.putExtra("NAME", singleArtistDetails.get(0));
intent.putExtra("DOB", singleArtistDetails.get(1));
intent.putExtra("BIOGRAPHY", singleArtistDetails.get(2));
startActivity(intent);
}
});
}
Here is my code for displaying selected index results Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saved_artist_details);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
artistSavedNameLbl2 = (TextView) findViewById(R.id.artistSavedNameLbl2);
artistSavedDOBLbl2 = (TextView) findViewById(R.id.artistSavedDOBLbl2);
artistSavedBiographyLbl2 = (TextView) findViewById(R.id.artistSavedBiographyLbl2);
btnDeleteDetails = (Button) findViewById(R.id.btnDeleteDetails);
updateUI();
}
private void updateUI() {
artistSavedNameLbl2.setText(getIntent().getStringExtra("NAME"));
artistSavedDOBLbl2.setText(getIntent().getStringExtra("DOB"));
artistSavedBiographyLbl2.setText(getIntent().getStringExtra("BIOGRAPHY"));
}
Do one thing , initialize [not saying declare] your array list singleArtistDetails inside the onClick method, before adding any values to it.
like
singleArtistDetails = new ArrayList();
Hope this will help.
Layout
Hi! So I've been trying to make a basic note app and I've run into a wall. I've spent hours trying to get my data to save to my sharedPreferences, but no matter what I try it doesn't seem to work. I've added logs to the app so we can examine what is happening.
LOGS:
02-22 18:27:56.767 4929-4929/com.example.jackson.collegeplanner I/TEST: notesSet didn't return null!
(When I click the addnote button)
02-22 18:29:54.500 4929-4929/com.example.jackson.collegeplanner I/TEST: newNote added to notesSet
Code:
public class Schedule extends AppCompatActivity {
ArrayList<String> notes = new ArrayList<>();
// SharedPreferences sharedPreferences = this .getSharedPreferences("com.example.jackson.collegeplanner", Context.MODE_PRIVATE);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, notes);
ListView listView = (ListView) findViewById(R.id.listView);
SharedPreferences myPref = this.getSharedPreferences("com.example.jackson.collegeplanner", Context.MODE_PRIVATE);
Set<String> notesSet = myPref.getStringSet("NN", null);
if(notesSet != null){
notes.addAll(notesSet);
listView.setAdapter(arrayAdapter);
Log.i("TEST", "notesSet didn't return null!");
}
else{
notesSet = new HashSet<String>();
notesSet.add("Ya note's set is empty");
notes.addAll(notesSet);
listView.setAdapter(arrayAdapter);
Log.i("TEST", "noteSet returned null");
}
myPref.edit().putStringSet("NN", notesSet).apply();
}
public void AddNote(View view){
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, notes);
ListView listView = (ListView) findViewById(R.id.listView);
EditText editText = (EditText) findViewById(R.id.editText);
String newNote = editText.getText().toString();
SharedPreferences myPref = this.getSharedPreferences("com.example.jackson.collegeplanner", Context.MODE_PRIVATE);
Set<String> notesSet = myPref.getStringSet("NN", null);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
notesSet.add(newNote);
Log.i("TEST", "newNote added to notesSet");
notes.clear();
notes.addAll(notesSet);
editText.setText("");
myPref.edit().putStringSet("NN", notesSet).apply();
listView.setAdapter(arrayAdapter);
}
}
You can use
if(myPref.edit().putStringSet("notes", set).commit())
Log.d(TAG , "SAVED");
else
Log.d(TAG , "Not Saved");
To check if shared preferences were actually saved.
in your code change
`
if(set == null){
set = new HashSet<String>();
notes.add("Initial Notes");
set.addAll(notes);
}
`
to
`
if(set == null){
set = new HashSet<String>();
notes.add("Initial Notes");
set.addAll(notes);
myPref.edit().putStringSet(set).apply();
}
`
the answer is from a duplicate post I made and it was answered there.
Oh, I just remembered a catch with String Sets in SharedPreferences that you're probably coming up against. (I admit I didn't really test your code very thoroughly when I did it.) You cannot try to modify the Set you get from getStringSet() and then save it back. You need to instantiate a new one, and pass that to putStringSet(). In your code, a simple fix would be: Set<String> notesSet = new HashSet<String>(myPref.getStringSet("NN", null));. – Mike M.
I'm really strugling to figure this out and had not found an answer or a way to do it.
What I'm trying to do is having questions that are saved in a SQLite colum to be displayed in a TextView one after another until they finish.
What I have done so far on the main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabaseHelper = new DatabaseHelper(this);
btn1star = (Button) findViewById(R.id.btn1star);
btn2star = (Button) findViewById(R.id.btn2star);
btn3star = (Button) findViewById(R.id.btn3star);
mListview = (ListView) findViewById(R.id.qlistview);
qTextView = (TextView) findViewById(R.id.qTextView);
questionsListView();
}
private void questionsListView() {
Cursor data = mDatabaseHelper.getData();
data.moveToFirst();
qTextView.setText(data.getString(1));
}
public void VoteClick(View view){
mDatabaseHelper.getReadableDatabase();
Cursor data = mDatabaseHelper.getData();
if (data.getCount() >=1){
for (int i = 0; i< data.getCount(); i++) {
data.moveToNext();
Log.i("Counted Questions are: ", String.valueOf(data.getCount()));
qTextView.setText(data.getString(1));
}
}
}
private void toastmessage (String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
I'm able to display the first and when i click the button the last question
I'm unable to display the questions between.
Any tip or suggestion?
In your VoteClick method you do your getReadableDatabase, this is too late, move this line into onCreate method. You must open the database before reading from it (in questionsListView).
mDatabaseHelper.getReadableDatabase();//move to onCreate
add this:
startManagingCursor(data);
data.moveToFirst();
BEFORE the line:
if (data.getCount() >=1){
Hello found a solution to my problem
It turns out that i have to make a new int variable
Get its value from the first id
and incrise this by one at the end of each button press until i reach the last cursor place whhere i move the cursor to the first position
public void VoteClick(View view) {
Cursor data = mDatabaseHelper.getData();
data.moveToPosition(q);
data.moveToNext();
qTextView.setText(data.getString(1));
while (data.isLast()) {
data.moveToFirst();
q = data.getPosition();
}
q = q+1;
}
Right, I know there's alot of similar questions. I've researched on stackoverflow as well as on the internet about this but still stumped.
This code is in a fragment.
...
private Context context = getActivity();
public void Dialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View mView = inflater.inflate(R.layout.dialog, null);
alertDialogBuilder.setView(mView);
EditText a = (EditText) mView.findViewById(R.id.a);
EditText b = (EditText) mView.findViewById(R.id.b);
EditText c = (EditText) mView.findViewById(R.id.c);
//a.setText("abc");
//b.setText("xyz");
//c.setText("123");
strA = a.getText().toString();
strB = b.getText().toString();
final String strC = c.getText().toString();
}
This should be a typical approach to getting the view of the inflated layout and using it to access the elements inside the view, but it's not working no matter what I tried, I just could not get strA, strB and strC values using getText().toString().
//a.setText("abc");
//b.setText("xyz");
//c.setText("123");
But, if I uncomment the above 3 lines, the values get sent across. And I can receive them inside strA, strB, strC. Why is this so? I don't get it at all.
Any help greatly appreciated, thank you!
You are trying to get values at the time you initialize the dialog. you need to get values after an action, like a button click
alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
strA = a.getText().toString();
strB = b.getText().toString();
}
});
Have a look at this display textview in alert dialog
Looks to me like you're checking the values of the EditTexts as soon as they're created. Which of course the value is going to be null every time since your user hasn't had time to type anything. I think you need a SubmitButton or something similar that checks the EditTexts in its onClickListener. Though I'll admit I've never inflated a view into an alert dialog.
Try like this.
MyDialogue is your Activity name.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MyDialogue.this);
// Get the layout inflater
LayoutInflater inflater = MyDialogue.this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View mView = inflater.inflate(R.layout.dialog, null);
alertDialogBuilder.setView(mView);
final EditText a = (EditText) mView.findViewById(R.id.a);
final EditText b = (EditText) mView.findViewById(R.id.b);
final EditText c = (EditText) mView.findViewById(R.id.c);
a.setText("abc");
b.setText("xyz");
c.setText("123");
alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
final String strA = a.getText().toString();
final String strB = b.getText().toString();
final String strC = c.getText().toString();
Log.e("tag", strA + " " + strB + " " + strC );
}
});
alertDialogBuilder.show();
The layout is not yet initiated when you are trying to set the values, you need to call AlertDialog alertDialog = alertDialogBuilder.create(); and then alertDialog.show(); or just alertDialogBuilder.show() before you can edit the textView content.