Last active
December 19, 2015 04:19
-
-
Save JafarKhQ/5896645 to your computer and use it in GitHub Desktop.
a Test for notifyDataSetChanged() function on Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends Activity { | |
private Adapter ad; | |
private String[] arr = new String[5]; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
for (int i = 0; i < arr.length; i++) { | |
arr[i] = "1"; | |
} | |
ad = new Adapter(this, arr); | |
ListView l = (ListView) findViewById(R.id.list); | |
l.setAdapter(ad); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
for (int i = 0; i < arr.length; i++) { | |
arr[i] = "2"; | |
} | |
ad.notifyDataSetChanged(); | |
return true; | |
} | |
private static class Adapter extends BaseAdapter { | |
private LayoutInflater mInflater; | |
private String[] arr; | |
public Adapter(Context c, String[] arr) { | |
this.arr = arr; | |
mInflater = LayoutInflater.from(c); | |
} | |
@Override | |
public int getCount() { | |
// TODO Auto-generated method stub | |
return arr.length; | |
} | |
@Override | |
public Object getItem(int position) { | |
return null; | |
} | |
@Override | |
public long getItemId(int position) { | |
return 0; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if (null == convertView) { | |
convertView = mInflater.inflate(R.layout.a, parent, false); | |
} | |
TextView t = (TextView) convertView.findViewById(R.id.textView1); | |
t.setText(arr[position]); | |
return convertView; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment