I'm trying to save the data of 4 variables on the SharedPreferences and load it to a list. My problem is that it shows all the data in the first line of the list. I wanted to split like: Everytime I click the save button I wanted to save the 4 variables and when I click it again and I want to save the new variables and display it a new line.
Showing All on the same index:
TubeDataFragment (save):
public class TubeDataFragment extends Fragment {
List<String> tubeData = new ArrayList<>();
public TubeDataFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tube_data, container, false);
final Spinner spinnerMaterial = (Spinner) view.findViewById(R.id.snipperMaterial);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, getResources().getStringArray(R.array.material));
myAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinnerMaterial.setAdapter(myAdapter);
//controls
Button btn_saveTube = (Button) view.findViewById(R.id.btn_save_tube);
final EditText et_diameter = (EditText) view.findViewById(R.id.et_diameter);
final EditText et_clr = (EditText) view.findViewById(R.id.et_clr);
final EditText et_thickness = (EditText) view.findViewById(R.id.et_thickness);
//guarda dados do tubo
btn_saveTube.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tubeData.add(" Diameter: " + et_diameter.getText().toString());
tubeData.add(" Thickness: " + et_thickness.getText().toString());
tubeData.add(" CLR: " + et_clr.getText().toString());
tubeData.add(" Material: " + spinnerMaterial.getSelectedItem().toString());
StringBuilder stringBuilder = new StringBuilder();
int i = 1;
for(String data : tubeData)
{
stringBuilder.append(data);
stringBuilder.append(";");
if(i++ == tubeData.size())
{
stringBuilder.append("\n");
}
}
SharedPreferences settings = getActivity().getSharedPreferences("PREFS",0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("tubeData", stringBuilder.toString());
editor.commit();
}
});
return view;
}
}
ArchiveFragment (load)
public class ArchiveFragment extends Fragment {
public ArchiveFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_archive, container, false);
ListView tubeDataList = (ListView) view.findViewById(R.id.tubeData_list);
//load tube data
SharedPreferences settings = getActivity().getSharedPreferences("PREFS",0);
String tubeDataString = settings.getString("tubeData", "");
String[] tubeDataSplit = tubeDataString.split("\n");
List<String> tubeDataItems = new ArrayList<>();
for(int i=0; i<tubeDataSplit.length;i++)
{
tubeDataItems.add(tubeDataSplit[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, tubeDataItems);
// Assign adapter to ListView
tubeDataList.setAdapter(adapter);
return view;
}
}
Your if condition is wrong.
In TubeDataFragment, use below codes in onClick() method:
.............
..................
StringBuilder stringBuilder = new StringBuilder();
int i = 1;
for(String data : tubeData)
{
stringBuilder.append(data);
stringBuilder.append(";");
if(i == 4)
{
stringBuilder.append("\n");
i = 0;
}
i++;
}
...............
.......................
Related
setContentView(R.layout.activity_violation);
db = new DatabaseHelper(this);
sqLiteDatabase = db.getReadableDatabase();
spinner = (Spinner) findViewById(R.id.spinner1);
loadspinnerdata();
txtTexts = (TextView) findViewById(R.id.texts);
btnBack = (Button) findViewById(R.id.btnBack);
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listviewData();
}
});
strSelectedItem = spinner.getSelectedItem().toString();
listview = (ListView) findViewById(R.id.listView);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.list_header, listview, false);
listview.addHeaderView(header, null, false);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list_query = "select _id,section,offence,fine from tblSection";
Cursor clistview = sqLiteDatabase.rawQuery(list_query, null);
listadapter = new ListAdapter(this, clistview, 0);
listview.setAdapter(listadapter);private void listviewData() {
int cntChoice = listview.getCount();
String checked = "";
String unchecked = "";
SparseBooleanArray sparseBooleanArray = listview.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++)
{
if(sparseBooleanArray.get(i))
{
checked += listview.getItemAtPosition(i).toString() + "\n";
}
else if(!sparseBooleanArray.get(i))
{
unchecked+= listview.getItemAtPosition(i).toString() + "\n";
}
}
I am using cursorAdapter in order to populate listview from database?so I am not getting position as i get from getView method in arrayAdapter
public class ListAdapter extends CursorAdapter{
TextView tvSection,tvOffence,tvId,tvFine;
CheckBox chkBox;
ArrayAdapter<String> objects;
private boolean chkItem;
CompoundButton.OnCheckedChangeListener myCheckChangList;
public ListAdapter(Context context, Cursor c, int _id) {
super(context, c,_id);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
return LayoutInflater.from(context).inflate(R.layout.listview_item,viewGroup,false);
}
public class ViewHolder{
TextView tvSection,tvOffence,tvId,tvFine;
CheckBox chkBox;
boolean chkItem;
boolean selected = false;
public ViewHolder(boolean chkItem){
super();
this.chkItem=chkItem;
}
public ViewHolder() {
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder=null;
holder = new ViewHolder();
holder.tvSection = (TextView) view.findViewById(R.id.txtSection);
holder.tvOffence = (TextView) view.findViewById(R.id.txtOffence);
holder.tvId = (TextView) view.findViewById(R.id.txtId);
holder.tvFine = (TextView) view.findViewById(R.id.txtFine);
holder.chkBox = (CheckBox) view.findViewById(R.id.checkBox);
holder.chkBox.setOnCheckedChangeListener(myCheckChangList);
view.setTag(holder);
strFine=cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
String strSection = cursor.getString(cursor.getColumnIndexOrThrow("section"));
String strOffence = cursor.getString(cursor.getColumnIndexOrThrow("offence"));
int strFine = cursor.getInt(cursor.getColumnIndexOrThrow("fine"));
holder.tvSection.setText(strSection);
holder.tvOffence.setText(strOffence);
holder.tvFine.setText(String.valueOf(strFine));
final ViewHolder finalHolder = holder;
}
I tried this but this isn't working.I tried checkbox.setOnclicklistener which gives me the checked row value single item but not multiple checked item.I tried sparseboolean array also.
If you are trying to get selected values from listing using checkbox,try this way it will work
public class CardViewActivity extends AppCompatActivity {
private Toolbar toolbar;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<Student> studentList;
private Button btnSelection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
btnSelection = (Button) findViewById(R.id.btnShow);
studentList = new ArrayList<Student>();
for (int i = 1; i <= 15; i++) {
Student st = new Student("Student " + i, "androidstudent" + i
+ "#gmail.com", false);
studentList.add(st);
}
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Android Students");
}
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// create an Object for Adapter
mAdapter = new CardViewDataAdapter(studentList);
// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);
btnSelection.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String data = "";
List<Student> stList = ((CardViewDataAdapter) mAdapter)
.getStudentist();
for (int i = 0; i < stList.size(); i++) {
Student singleStudent = stList.get(i);
if (singleStudent.isSelected() == true) {
data = data + "\n" + singleStudent.getName().toString();
/*
* Toast.makeText( CardViewActivity.this, " " +
* singleStudent.getName() + " " +
* singleStudent.getEmailId() + " " +
* singleStudent.isSelected(),
* Toast.LENGTH_SHORT).show();
*/
}
}
Toast.makeText(CardViewActivity.this,
"Selected Students: \n" + data, Toast.LENGTH_LONG)
.show();
}
});
}
}
http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html
On clicking outside the adapter, use the following code. "ListAdapter" should be the Adapter class name.
ListAdapter.unCheck((ViewGroup)view.getParent());
If your on clicking is inside the adapter, then ignore the above code and use the below one.
unCheck((ViewGroup)view.getParent());
make the following function in Adapter.
public static void unCheck(ViewGroup vg) {
for (int i = 0; i < vg.getChildCount(); i++) {
View v = vg.getChildAt(i);
if (v instanceof CheckBox) {
((CheckBox) v).setChecked(true);
} else if (v instanceof ViewGroup) {
unCheck((ViewGroup) v);
}
}
}
The above function will checked all the checkBox of the list view. Have Fun. Take Care.
I need to put a strikethrough on the text after the item has been checked. I found solutions that use setPaintFlags(descriptionView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);. I don't use a textview but instead use simple_list_item_multiple_choice for my listview so how do I solve this? Here is my entire code:
public class Surv_list extends Fragment {
final String[] OPSys = new String[]{"item1","item2","item3","item4"
};
ListView myList;
Button getChoice, clearAll;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyUserChoice" ;
ArrayList<String> selectedItems = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listlay, container, false);
myList = (ListView)rootView.findViewById(R.id.list);
ListView list = (ListView) rootView.findViewById(R.id.list);
clearAll = (Button)rootView.findViewById(R.id.clearall);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, OPSys);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
list.setAdapter(adapter);
sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if(sharedpreferences.contains(MyPREFERENCES)){
LoadSelections();
}
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SaveSelections();
}
});
clearAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ClearSelections();
}
});
return rootView;
}
private void SaveSelections() {
// save the selections in the shared preference in private mode for the user
SharedPreferences.Editor prefEditor = sharedpreferences.edit();
String savedItems = getSavedItems();
prefEditor.putString(MyPREFERENCES.toString(), savedItems);
prefEditor.commit();
}
private String getSavedItems() {
String savedItems = "";
int count = this.myList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
if (this.myList.isItemChecked(i)) {
if (savedItems.length() > 0) {
savedItems += "," + this.myList.getItemAtPosition(i);
} else {
savedItems += this.myList.getItemAtPosition(i);
}
}
}
return savedItems;
}
private void LoadSelections() {
// if the selections were previously saved load them
if (sharedpreferences.contains(MyPREFERENCES.toString())) {
String savedItems = sharedpreferences.getString(MyPREFERENCES.toString(), "");
selectedItems.addAll(Arrays.asList(savedItems.split(",")));
int count = this.myList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
String currentItem = (String) myList.getAdapter()
.getItem(i);
if (selectedItems.contains(currentItem)) {
myList.setItemChecked(i, true);
} else {
myList.setItemChecked(i, false);
}
}
}
}
private void ClearSelections() {
// user has clicked clear button so uncheck all the items
int count = this.myList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
this.myList.setItemChecked(i, false);
}
// also clear the saved selections
SaveSelections();
}
}
any help would be very much appreciated.
You can create a custom adapter for your list view and in the getview method of the adpater take the handle of textview . Based on your condition you can combine the already available code to strikeout the text view.
for making your own view attributes in list view, you have to make a custom Adapter other than using default adapter. there is no way you can do this using default adapter. here you can learn how to make custom adapter. also I would suggest you to use RecyclerView instead of ListView
I am pretty new in android programming. What I have is an app with some tabs, each associate with some fragments. I want to change the layout of the fragment. But I don't want to mess up with the tabs. I am trying to achieve this from a class that extends AsyncTask's onPostExecute() method. I tried to inflate the layout, but it also affects the tabs. I just want to change the fragment only. Here is the code so far:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
lInflater = inflater;
mContainer = container;
final View rootView = inflater.inflate(R.layout.symptoms_layout, container, false);
textList = new ArrayList();
final LinearLayout mLinearLayout = (LinearLayout) rootView.findViewById(R.id.ll);
Button mButton = (Button) mLinearLayout.findViewById(R.id.btn_add);
Button test = (Button) mLinearLayout.findViewById(R.id.btn_search_sym);
cAdapter = new CustomArrayAdapter(textList,getActivity().getApplicationContext(),test);
ListView lView = (ListView)mLinearLayout.findViewById(R.id.list_symptoms);
lView.setAdapter(cAdapter);
test.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
try {
Log.d("SymFrag",String.valueOf(cAdapter.getCount()));
String[] list = new String[cAdapter.getCount()];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cAdapter.getCount(); i++) {
Log.d("SymFrag", "Im in for loop");
String temp = cAdapter.getItem(i).toString();
sb.append(temp);
sb.append(",");
}
/*new BackgroundManager(new BackgroundManager.AsyncResponse() {
#Override
public void processFinish(String output) {
String[] out = output.split(",");
for(int i = 0; i < out.length; i++){
Log.d("ProFin",out[i]);
}
}
}).execute(sb.toString());*/
new BackgroundManager(getActivity(),lInflater,mContainer).execute(sb.toString());
} catch (Exception e) {
Log.e("SymFag",e.toString());
}
}
});
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*LayoutInflater layoutInflater = (LayoutInflater)
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mLinearLayout.addView(layoutInflater.inflate(R.layout.new_added,null));
TextView tv = (TextView)rootView.findViewById(R.id.symtex);
TextView t = (TextView)mLinearLayout.findViewWithTag("tex");
textList.add(tv.getText().toString());
t.setText(tv.getText());*/
Button bSearch = (Button) mLinearLayout.findViewById(R.id.btn_search_sym);
TextView tv = (TextView) rootView.findViewById(R.id.symtex);
textList.add(tv.getText().toString());
bSearch.setVisibility(View.VISIBLE);
cAdapter.notifyDataSetChanged();
}
});
return rootView;
}
Above is the fragment I want to update/change.
Thanks in advance.
i'm working on a student record project And there are four fields in it like class name roll no. marks and User is putting data into it by edit text
and i'm storing the values into the json Array . Also i'm putting values in array list and i'm passing to my fragment class .
New record class
public class newrecord extends Activity {
public final static String TAG = "SomeValue";
Button but;
EditText roll, name, marks, Class;
TextView text;
String rollno, nameno, classno, marksno;
TopRatedFragment frags;
public static final String ROLL_key = "roll number";
public static final String NAME_key = "Name";
public static final String Class_key = "Class";
public static final String MARKS_key = "marks";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newrecord);
final newrecord rd = new newrecord();
// data = new dbbase(getApplicationContext());
text = (TextView) findViewById(R.id.textviewenter);
roll = (EditText) findViewById(R.id.editRollno);
name = (EditText) findViewById(R.id.editName);
marks = (EditText) findViewById(R.id.editMarks);
Class = (EditText) findViewById(R.id.edittextclass);
but = (Button) findViewById(R.id.btnadd);
frags = new TopRatedFragment();
final JSONArray ary = new JSONArray();
final JSONObject obj = new JSONObject();
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View V) {
try {
obj.put("Roll No", roll.getText().toString());
obj.put("name", name.getText().toString());
obj.put("marks", marks.getText().toString());
obj.put("Class", Class.getText().toString());
ary.put(obj);
rd.nameno = name.getText().toString();
rd.rollno = roll.getText().toString();
rd.marksno = marks.getText().toString();
rd.classno = Class.getText().toString();
Log.d(TAG, "Values Inserted");
} catch (Exception e) {
text.setText("error");
}
ArrayList<String> list_list = new ArrayList<String>();
list_list.add(rollno);
list_list.add(nameno);
list_list.add(classno);
list_list.add(marksno);
Intent i = new Intent(newrecord.this, MainActivity.class);
// Bundle b = new Bundle();
i.putStringArrayListExtra("fite", list_list);
startActivity(i);
}
});
}
public ArrayList<HashMap<String, String>> Getalldata() {
ArrayList<HashMap<String, String>> dude;
dude = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put(ROLL_key, rollno);
map.put(NAME_key, nameno);
map.put(Class_key, classno);
map.put(MARKS_key, marksno);
dude.add(map);
return dude;
}
}
And i want to return list in the listview in my fragment .
As in the values that i add in edittexts should appear in the list in my fragment class.
My topfragment class
public class TopRatedFragment extends Fragment {
ListView List;
ListAdapter mAdapter;
int nr = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated,
container, false);
Intent in = new Intent();
newrecord record = new newrecord();
String tim = in.getStringExtra("fite");
ArrayList<String> liist = new ArrayList<String>();
if (liist.size() == 0) {
liist.add("Template1");
} else {
liist=in.getStringArrayListExtra("fite")
}
List = (ListView) rootView.findViewById(R.id.listView1);
List.setAdapter(mAdapter);
mAdapter = new SelectionAdapter(getActivity(), R.layout.simplerow,
R.id.rowTextView, liist);
List.setAdapter(mAdapter);
List.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
((BaseAdapter) mAdapter).notifyDataSetChanged();
}
});
Try this out:
In NewRecord class
Intent menulist = new Intent(this, cart_activity.class);
menulist.putExtra("cartlist", cart_Array_List);
startActivity(menulist);
In your fragment:
intent = getIntent();
if (intent != null) {
arr_cart_items = (ArrayList<HashMap<String, String>>) intent
.getSerializableExtra("cartlist");
}
Make sure your ArrayList is not empty.
And one common mistake that I used to do was to set text color as white, so the contents of the listview was not actually visible. So please check that too.
Change this line
Intent in = new Intent();
To
Intent in = getActivity().getIntent();
I hope this one will help you :)
here I have a Fragment, I use this code and everything works normally, and what I want to do is update my shown list if there is a new file, could you guys give any advice or hint?
CODE:
public class HomeFragment extends Fragment {
public static final String TITLE = "title";
private List<String> library = new ArrayList<String>();
private TextView tv;
private ListView lv;
private ArrayAdapter<String> adapter;
public static Handler handHF;
private String[] temp;
private Object UIlock = new Object();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, container,
false);
library = getLibraryList();
if (!library.isEmpty()) {
if (tv != null) {
tv.setVisibility(View.GONE);
} else {
temp = library.toArray(new String[library.size()]);
lv = (ListView) rootView.findViewById(R.id.library_list);
adapter = new ArrayAdapter<String>(rootView.getContext(),
android.R.layout.simple_list_item_1, temp);
lv.setAdapter(adapter);
setListener(lv);
tv = (TextView) rootView.findViewById(R.id.library_tv1);
tv.setVisibility(View.GONE);
tv = null;
}
} else {
tv = (TextView) rootView.findViewById(R.id.library_tv1);
tv.setText("No Manga found...");
}
return rootView;
}
#SuppressLint("HandlerLeak")
#Override
public void onResume() {
/*
* Fragment on pause state
*/
super.onResume();
handHF = new Handler(Looper.getMainLooper()) {
#Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
refreshAdapter();
}
}
};
}
private void setListener(ListView lv) {
/*
* Sets listener on listView
*/
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(view.getContext(),
ChapterActivity.class);
myIntent.putExtra(TITLE, parent.getItemAtPosition(position)
.toString());
startActivity(myIntent);
}
});
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(view.getContext(),
parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
return true;
}
});
}
private final List<String> getLibraryList() {
/*
* Returns List<String> of library
*/
List<String> l = new ArrayList<String>();
File dir = new File(Constants.UNDUH);
if (dir.exists()) {
File[] dirs = dir.listFiles();
for (File i : dirs) {
l.add(i.getName());
}
return l;
} else {
return l;
}
}
private void refreshAdapter() {
/*
* It will update library and
*/
synchronized (UIlock) {
getActivity().runOnUiThread(new Runnable() {
public void run() {
if (tv != null) {
tv.setVisibility(View.GONE);
}
library = getLibraryList();
temp = library.toArray(new String[library.size()]);
lv = (ListView) getActivity().findViewById(
R.id.library_list);
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, temp);
lv.setAdapter(adapter);
}
});
}
}
}
any advice will be appreciated, thank you!
Update your list of string which you are passing to the ListView in your case you are using
private String[] temp;
Use notifyDataSetChanged Method, just call this after your adapter and it will automatically adds more items to list if your temp[] increments.
like this
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, temp);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
Where you are adding more data into temp[]
add an extra line
((ArrayAdapter) lv.getAdapter()).notifyDataSetChanged();