Dynamic edittext passed to string for another activity - android

I'm trying to setup my program to take edittext from a list view, send to a string. Then to use these strings replace text pulled from database. So depending on what the user selected first there will be between 4-10 edittext for them to fill out. Once they fill out these edittext they will click the confirm button where it takes them to the next activity. On the next activity it take these strings from the edittext where it will do some replacing of text pulled from my database. I have it working for one edittext but I want to set this up dynamically so I have a few lines take care of this function no matter how many edittext fields I had on the previous activity.
code to display edittext page in form of listview
public class editpage extends ListActivity {
public static String editstring1;
int editCount;
private dbadapter mydbhelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_list);
mydbhelper = new dbadapter(this);
mydbhelper.open();
fillData();
}
private void fillData() {
Cursor e = mydbhelper.getUserWord();
startManagingCursor(e);
// Create an array to specify the fields we want to display in the list (TITLE,DATE,NUMBER)
String[] from = new String[] {dbadapter.KEY_USERWORD,};
// an array of the views that we want to bind those fields to (in this case text1,text2,text3)
int[] to = new int[] {R.id.textType,};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter editadapter =
new SimpleCursorAdapter(this, R.layout.edit_row, e, from, to);
ListView list = getListView();
View footer = getLayoutInflater().inflate(R.layout.footer_layout, list, false);
list.addFooterView(footer);
setListAdapter(editadapter);
editCount = e.getCount();
}
public void onClick(View footer){
final MediaPlayer editClickSound = MediaPlayer.create(this, R.raw.button50);
editClickSound.start();
EditText editstory = ((EditText)findViewById(R.id.editText));
editstring1 = editstory.getText().toString();
startActivity(new Intent("wanted.pro.madlibs.OUTPUT"));
};
#Override
protected void onListItemClick(ListView list, View v, int position, long id)
{
super.onListItemClick(list, v, position, id);
}}
Next code is my output code that will display results
public class output extends ListActivity {
private dbadapter mydbhelper;
#Override
public void onCreate(Bundle savedInstantState){
super.onCreate(savedInstantState);
setContentView(R.layout.outview);
mydbhelper = new dbadapter(this);
mydbhelper.open();
fillData();
mHandler.postDelayed(mTask, 10);
}
private final Runnable mTask = new Runnable(){
public void run(){
TextView textView = (TextView)findViewById(R.id.outputText);
String story = textView.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"edit1", Html.fromHtml("<font color=\"red\">"+ editpage.editstring1 +"</font>"));
textView.setText(modifitedText);
}
};
private final Handler mHandler = new Handler();
private void fillData() {
Cursor st = mydbhelper.getStory();
startManagingCursor(st);
// Create an array to specify the fields we want to display in the list (TITLE,DATE,NUMBER)
String[] from = new String[] {dbadapter.KEY_TITLESTORY};
// an array of the views that we want to bind those fields to (in this case text1,text2,text3)
int[] to = new int[] {R.id.outputText};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, R.layout.out_row, st, from, to);
setListAdapter(adapter);
}
}
The code in my database looks something like this
This is sample code from edit1. It will display the raw text before they user edit2 it. etc etc
Simply put I'm having trouble figuring out/finding help on how to make below code work for me dynamically
EditText editstory = ((EditText)findViewById(R.id.editText));
editstring1 = editstory.getText().toString();
CharSequence modifitedText = Replacer.replace(story,
"edit1", Html.fromHtml("<font color=\"red\">"+ editpage.editstring1 +"</font>"));
textView.setText(modifitedText);

Do you create the EditTexts in your adapter? And if you do I think you should add an onChange method to your EditTexts and handle the data from there

Related

Android if-else code not working properly

The problem i am facing is that i have my code is executing "else" block even when the "if " condition is true.
public class MsgNewPackage extends Activity {
private DatabaseHandler dbhandler;
private SimpleCursorAdapter dataAdapter;
private ListView listView;
String PKG_NAME,PKG_DUR,PKG_PRICE,PKG_ITEMS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_msg_new_package);
dbhandler = new DatabaseHandler(this);
displayListView();
}
private void displayListView()
{
String check= "Mobilink";
Intent intent = getIntent();
String conn = intent.getStringExtra("NET_CONN");
// Toast.makeText(getApplicationContext(), conn, Toast.LENGTH_SHORT).show();
if(conn == check)
{
Cursor cursor = dbhandler.fetchAllMOBSMSPackages();
String[] columns = new String[]{DatabaseHandler.KEY_PKG_NAME,DatabaseHandler.KEY_NO_OF_FREE_ITEMS,DatabaseHandler.KEY_DURATION,DatabaseHandler.KEY_CHARGES};
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.title,R.id.no_of_item,R.id.duration,R.id.pkgcharges};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(this, R.layout.list_row2, cursor,columns,to);
listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
else
{
Cursor cursor = dbhandler.fetchAllSMSPackages();
String[] columns = new String[]{DatabaseHandler.KEY_PKG_NAME,DatabaseHandler.KEY_NO_OF_FREE_ITEMS,DatabaseHandler.KEY_DURATION,DatabaseHandler.KEY_CHARGES};
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.title,R.id.no_of_item,R.id.duration,R.id.pkgcharges};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(this, R.layout.list_row2, cursor,columns,to);
listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
The toast displays that conn has "Mobilink" but the listview gets populated with results of fetchAllSMSPackages() (i.e function called in else block ) which should not be the case. Pleas help.
Try using conn.equals(check) instead of conn == check.
Strings in Java are objects, and not primitives, so you cannot compare their value using ==. This will compare the object as a whole, and not the text.
You should use .equals() when comparing String values. Not ==.
Try using:
conn.equals(check);

Unable to access query

I get db cannot be resloved error in this class. At the displayListView() function. Can anyone please help me solve it. Thanks
got code from here http://kdehairy.com/2012/08/19/using-a-preloaded-sqlite-database-with-sqliteopenhelper/
public class PrepopSqliteDbActivity extends Activity {
// private static final String DB_NAME = "hymnals";
//A good practice is to define database field names as constants
private SimpleCursorAdapter dataAdapter;
private Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Our key helper
ExternalDbOpenHelper repo = ExternalDbOpenHelper.getInstance( context );
SQLiteDatabase db = repo.getWritableDatabase();
displayListView();
}
private void displayListView() {
//all hymns are fetched
*final Cursor cursor = db.fetchAllHymns();*
// The desired columns to be bound
String[] columns = new String[] {
ExternalDbOpenHelper.HYMN_ID,
ExternalDbOpenHelper.HYMN_NAME,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.tVid,
R.id.name,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.activity_main,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.list);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
}
});
}
}
The variable db is a local variable within the onCreate method. Use an instance variable instead:
class /* ... */ {
private SQLiteDatabase db;
/* ... */
#Override
public void onCreate(Bundle savedInstanceState) {
/* ... */
db = repo.getWritableDatabase();
/* ... */
}
/* ... */
}

Populating ListView from a SQLite database

public class ConnectDatabase extends Activity {
private SimpleDBAdapter mDbHelper;
private ListView list;
private String[] values;
private String[] list1;
private int i=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView )findViewById(R.id.simpleListView);
mDbHelper = new SimpleDBAdapter(ConnectDatabase.this);
mDbHelper.createDatabase();
mDbHelper.open();
values = mDbHelper.getEditTextValue();
mDbHelper.close();
for(i=0;i<10;i++) {
String tempvalue;
tempvalue=values[i];
list1[i]=tempvalue;
Log.v("log_tag"," tempvalue "+tempvalue);
Log.v("log_tag", "list1 is"+list1[i]);
//Log.v("log_tag"," list1"+list1[i]);
}
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values));
//list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
/*for(int i=0;i<values.length;i++) {
String templist=values[i];
list1[i]=templist;
}*/
String fname=values[position];
Log.v("log_tag", "The fname is"+ fname);
Intent intent=new Intent(ConnectDatabase.this, Userinfo.class);
intent.putExtra("FirstName",fname );
startActivity(intent);
}
});
// to remove the last comma
}
}
Here I am doing ListView from SQLite database and want to display in ListView with only the first 10 ListView rows at the time but I get the error at the line list1[i]=tempvalue; so I can't copy array here anyone can help me to do copy the array name values where I get all the Column index values from my SQLite database.
Hope anyone have an idea to do this.
Your post is quite hard to read, but I think the issue is that list1 has not been initialised.
You need to add a line like this:
list1 = new String[10];
before you attempt to set a value within the array.
ps - I picked a value of 10 as your for loop has i<10.

Database Quandry

Been trying to get an answer on what I am doing wrong all over the place. I would like the user to select a button in the calling class, open a called listactivity which displays the contents of a database, let the user click an entry, copy that entry into a new database, send back the rowid from the new database to the calling class and have the calling class assign the title from the new database entry to the original button that was pushed.
Here is the calling class
static final private int CHOOSE_MONDAY = 0;
static final private int CHOOSE_TUESDAY = 0;
private int ButtonPushed = 0;
private NotesDbAdapter mDbHelper;
private MenuDbAdapter menuDbHelper;
private Long mRowId;
String menuTitle;
String menuProtein;
String menuBody;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.plan_menu);
Toast.makeText(this, "Choose a day to pick a meal for!", Toast.LENGTH_LONG).show();
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
menuDbHelper = new MenuDbAdapter(this);
menuDbHelper.open();
}
public void mButtonHandler(View target)
{
switch(target.getId())
{
case R.id.monday:
// Create new intent object and tell it to call the ColorPicker class
Intent question = new Intent(this, PlanMenuList.class);
// Start ColorPicker as a new activity and wait for the result
startActivityForResult(question, CHOOSE_MONDAY);
break;
case R.id.tuesday:
// Create new intent object and tell it to call the ColorPicker class
Intent question1 = new Intent(this, PlanMenuList.class);
// Start ColorPicker as a new activity and wait for the result
startActivityForResult(question1, CHOOSE_TUESDAY);
break;
}
And then this is the called class where I am trying to copy in the user's selection to the new database and then send back the id to the calling class.
public class PlanMenuList extends ListActivity {
private NotesDbAdapter mDbHelper;
private MenuDbAdapter menuDbHelper;
private List<Data>data;
String menuTitle;
String menuProtein;
String menuBody;
private Long mRowId;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes_list);
mDbHelper = new NotesDbAdapter(this);
menuDbHelper = new MenuDbAdapter(this);
mDbHelper.open();
menuDbHelper.open();
fillData();
}
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text1};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
setListAdapter(notes);
}
private void populateFields() {
if (mRowId != null) {
Cursor note = mDbHelper.fetchNote(mRowId);
startManagingCursor(note);
menuTitle=(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
menuProtein=(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_PROTEIN)));
menuBody=(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
}
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mDbHelper.fetchNote(id);
mRowId = id;
//populateFields();
menuDbHelper.createMenu("Monday", menuTitle, menuProtein, menuBody);
Intent answer = new Intent();
answer.putExtra("MenuDbAdapter.KEY_ROWID", mRowId);
setResult(RESULT_OK, answer);
finish();
}
}
I have been messing around with this thing for days and can't seem to get it to do what I want - any help would be appreciated.
Can you post your onActivityResult implementation?
When I've passed data back to an activity as a result, I've used Bundles. For example:
Intent answer = new Intent();
Bundle extras = new Bundle();
extras.putLong("MenuDbAdapter.KEY_ROWID", mRowId);
answer.putExtras(extras);
Then, in the activity result handler, you'd call Intent.getExtras and pull your value from there.
Edit: here are some examples from the android dev guide:
http://developer.android.com/guide/appendix/faq/commontasks.html (search for onActivityResult)
http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html
protected void onListItemClick(ListView l, View v, int position, long id) {
the variable id is not the same as the database row number and thats where the issue is. I'd create a custom adapter and store the _id (row number) as a tag in the view. Retrieve the tag in the OnItemClickListener and do the db queries.
Custom Adapter code:
private class Cursy extends CursorAdapter {
public Cursy(Context context, Cursor c) {
super(context, c);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
viewHolder holder = (viewHolder) view.getTag();
holder.tv.setText(cursor.getString(cursor
.getColumnIndex(NotesDbAdapter.KEY_TITLE)));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
viewHolder holder = new viewHolder();
holder._id = ursor.getString(cursor.getColumnIndex(NotesDbAdapter._ID));
View v = getLayoutInflater().inflate(
R.layout.list_layout, null);
holder.tv = (TextView) v
.findViewById(R.id.tv);
v.setTag(holder);
return v;
}
}
OnItemClickListener:
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
viewHolder holder = (viewHolder) view.getTag();
// use holder._id and do the db queries
}
});
Firstly, I think you want to make sure your CHOOSE_MONDAY and CHOOSE_TUESDAY constants are different values, so that you can differentiate between them later.
Secondly, you are sending back the wrong row ID to your original activity. Assuming your createMenu() method is based on SQLiteDatabase.insert(), it should return the row ID after insertion (or -1 if there was a problem). You can use this as the row ID:
mRowId = menuDbHelper.createMenu("Monday", menuTitle, menuProtein, menuBody);

How to click a ListView item in Android

I'm trying to have another activity launch when a list item gets clicked. Below is my code:
public class AvoidForeclosure extends CustomListTitle {
/** Called when the activity is first created. */
private DbAdapter db;
private SimpleCursorAdapter clients;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list = getListView();
setContentView(R.layout.main);
this.title.setText("Avoid Foreclosure");
db = new DbAdapter(this);
db.open();
fillData();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
int viewId = view.getId();
TextView theView = (TextView) findViewById(viewId);
String name = theView.getText().toString();
Cursor clientData = db.getClientByName(name);
Intent intent = new Intent();
intent.setClass(view.getContext(), CurrentMarketValue.class);
intent.putExtra("clientId", clientData.getInt(0));
startActivity(intent);
}
});
}
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = db.fetchAllClients();
startManagingCursor(c);
String[] from = new String[] { DbAdapter.KEY_NAME };
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
clients = new SimpleCursorAdapter(this, R.layout.clientsrow, c, from, to);
setListAdapter(clients);
}
}
Yet when I click it, nothing happens at all. Any ideas?
Try switching this line:
ListView list = getListView();
to be after this one:
setContentView(R.layout.main);
Otherwise you'll be getting a handle to the ListActivity's default layout's ListView, rather than R.layout.main's listview (which is the one you really want).

Categories

Resources