Skip to main content

Edittext in Listview with wrong input onresume



I have a a listview with each row having a text field and edittext field. I have them all fight on screen. When I resume the activity by either getting a call, going back etc the input in the edittext fields does not match up with what was originally enter into. I was wondering how I could setup onresume or a saved instant state to prevent that and insure that the correct input is in the correct edittext field.





This is the code I'm working with.







public class editview extends ListActivity {

private dbadapter mydbhelper;

private PopupWindow pw;

public static int editCount;

public static ListView listView;

public ItemAdapter adapter;



/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mydbhelper = new dbadapter(this);

mydbhelper.open();





View footer = getLayoutInflater().inflate(R.layout.footer_layout, null);

ListView listView = getListView();

listView.addFooterView(footer);

showResults();

}



//Populate view

private void showResults (){

Cursor cursor = mydbhelper.getUserWord();

startManagingCursor(cursor);

String[] from = new String[] {dbadapter.KEY_USERWORD};

int[] to = new int[] {R.id.textType};

adapter = new ItemAdapter(this, R.layout.edit_row, cursor,

from, to);

adapter.notifyDataSetChanged();

this.setListAdapter(adapter);

editCount = adapter.getCount();

adapter.notifyDataSetChanged();

}





//footer button

public void onClick(View footer){

final MediaPlayer editClickSound = MediaPlayer.create(this, R.raw.button50);

editClickSound.start();

if (ItemAdapter.inputValues.containsValue("")){

Toast.makeText(this, "Please fill in all fields", 1000).show();

}else{

startActivity(new Intent("wanted.pro.madlibs.OUTPUT"));

};



}

...



}

@Override

protected void onResume() {

super.onResume();

}



@Override

protected void onPause() {



super.onPause();



}





}

//custom cursor adapter

class ItemAdapter extends SimpleCursorAdapter {



private LayoutInflater mInflater;

private Cursor cursor;

static Map<Integer, String> inputValues = new LinkedHashMap<Integer, String>();

static String oldText;





public ItemAdapter(Context context, int layout, Cursor cursor, String[] from,

int[] to) {

super(context, layout, cursor, from, to);

this.cursor = cursor;

mInflater = LayoutInflater.from(context);



}





static class ViewHolder implements TextWatcher {

protected TextView text;

protected EditText edittext;

protected int position;



public void afterTextChanged(Editable editable) {

Log.e(String.valueOf(position), "Position in array");

inputValues.put(position, editable.toString());



}

public void beforeTextChanged(CharSequence s, int start, int count,

int after) {

// TODO Auto-generated method stub



}

public void onTextChanged(CharSequence s, int start, int before,

int count) {

// TODO Auto-generated method stub



}



}



@Override

public View getView(final int position, View convertView, ViewGroup parent) {





ViewHolder holder;

if (convertView == null) {

convertView = mInflater.inflate(R.layout.edit_row, null);





holder = new ViewHolder();

holder.text = (TextView) convertView.findViewById(R.id.textType);

holder.edittext = (EditText) convertView.findViewById(R.id.editText);

holder.edittext.addTextChangedListener(holder);

holder.position = position;

convertView.setTag(holder);



} else {

holder = (ViewHolder) convertView.getTag();



}

cursor.moveToPosition(position);

int label_index = cursor.getColumnIndex("userword");

String label = cursor.getString(label_index);



holder.text.setText(label);

oldText = inputValues.get(position);

holder.edittext.setText(oldText == null ? "" : oldText);



return convertView;



}

}




Comments

  1. First of all, EditTexts in ListViews are a big pain in the butt, just in case you start running into issues. Second, you don't seem to be saving the inputValue strings at any point. At least you should serialize the values in onSaveInstanceState() and read them back in onCreate(). You shouldn't be storing them in the adapter either. You should really have a proper "model" (an object with label and input values) backing the adapter. A SimpleCursorAdapter is not very well suited for modifying data in parallel.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.