0

whenever I add a new value in an array it creates two rows with one empty row in listview. when i delete a value in array the value is removed but both row still exists.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listcontact);               sharedPreferences=getSharedPreferences(preferences,Context.MODE_PRIVATE);
        for(int i=1;i<=n+1;i++) {
        String st=sharedPreferences.getString(String.valueOf(i), "");
        conact_list.add(st);
        }
ArrayAdapter adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,conact_list);
ListView cv= (ListView) findViewById(R.id.listview);
cv.setAdapter(adapter);
}

I have checked arraylist and arrayadapter both doesnt have empty value or extra size but in list view it displays as follow : enter image description here

Log:

04-25 12:40:31.491 30892-30892/com.symbols.syedibrahim.phonetrack D/ERR﹕ entered 04-25 12:40:31.501 30892-30892/com.symbols.syedibrahim.phonetrack D/ERR﹕ + 04-25 12:40:31.501 30892-30892/com.symbols.syedibrahim.phonetrack D/ERR﹕ +93838394 04-25 12:40:31.501 30892-30892/com.symbols.syedibrahim.phonetrack D/ERR﹕ +947574748 04-25 12:40:31.501 30892-30892/com.symbols.syedibrahim.phonetrack D/ERR﹕ +87478494 04-25 12:40:31.501 30892-30892/com.symbols.syedibrahim.phonetrack D/ERR﹕ aA created[93838394, 947574748, 87478494] 04-25 12:40:31.501 30892-30892/com.symbols.syedibrahim.phonetrack D/ERR﹕ lv3count3list view count:0 04-25 12:40:31.501 30892-30892/com.symbols.syedibrahim.phonetrack D/ERR﹕ la

13
  • have you checked your shared preference file ..? Commented Apr 25, 2015 at 7:29
  • check if sharedPerference is empty, use st.isEmpty() eg : for(int i=1;i<=n+1;i++) { String st=sharedPreferences.getString(String.valueOf(i), ""); if( ! st.isEmpty() ) { conact_list.add(st);} } Commented Apr 25, 2015 at 7:29
  • It looks like your SharedPreferences are not populated as you expect, so you are getting the default empty String. Commented Apr 25, 2015 at 7:29
  • if my shared preference is wrong ,my array will have empty values. but my array and array adapter has exact right values and no empty values. Commented Apr 25, 2015 at 7:31
  • Take Log for each iteration. and show us the log. Commented Apr 25, 2015 at 7:31

1 Answer 1

1

Below is my opinion:

Case 1: Check the number of data object is matching with your item in the view. It mean in for(int i=1;i<=n+1;i++ we check the value of n, does it equal to the number items displayed in the view. If two values are same, you should check you data in preference again.

Otherwise, go to case 2 : check your adapter, in adapter you should check getView and getCount method. The number value returned by getCount must be same with number of data object.

Sign up to request clarification or add additional context in comments.

2 Comments

I doubt if there is any method named as onCreateView in adapter.
Sorry for my misstake. I mean getView function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.