i tried to search this but was not able to get any proper help.I am trying to keep the first item in the list view fixed and add data from database after that.I am successful in displaying this sort of behavior but problem starts when i use on item click in list view, i dont get correct item i clicked...snippet as follows copyFetchedData has the data from database
for(int i = 0;i<copyFetchedData.size();i++){
recentSearchedList.add(i, copyFetchedData.get(i).getName());
selectedAdapter.notifyDataSetChanged();
}
recentSearchedList.add(0, "My location");
in the above snippet the last line overwrites the position of the added element and puts My Location in first position.Now the snippet for onitemclick as follows.
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if (displayList == arg0) {
if(position == 0){
editLocation.setText("");
}
else{
String fetchedName = (String) displayList.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), "Name is "+fetchedName, Toast.LENGTH_LONG).show();
Constants.searchedLocLat = copyFetchedData.get(position).getLat();
Constants.searchedLocLon = copyFetchedData.get(position).getLon();
Constants.searchedLocName = copyFetchedData.get(position).getName();
editLocation.setText(Constants.searchedLocName);
}
}
}
the output i get using this fetches me element of wrong position.I know its because i forcefully add element at position 0 which causes this kind of behavior can someone please tell me right way to do it..
You can use the addHeaderView()
to do that,
ListView lv = (ListView)findViewById(R.id.listview1); //your listview object
View hvw = findViewById(R.id.listrow);
//Suppose you have one textview in that Row
TextView tv = hvw.findViewById(R.id.tv1); // get textview object
tv.setText("Your first item value"); // set text
lv.addHeaderView(hvw); // add header view