Move to next item or position in List view on button click
Posted
by praveenb
on Stack Overflow
See other posts from Stack Overflow
or by praveenb
Published on 2010-05-14T19:01:37Z
Indexed on
2010/05/14
19:04 UTC
Read the original article
Hit count: 455
android
Hi, im new to Android programing
I build a listview showing station names, that grabs its data from a URL.
I used ArrayAdapter to accomplish listview with data.
I need to navigate through station names in listview, using previous, next buttons click.
I google for this task and tried in different ways to workout bt im not able to solve.
shall any one pls help me out for this issue.
Thanks in advance......
I tried like this
` private ListView stationList; . . public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.playscreen);
// move up event handler
preButton= (ImageButton) findViewById(R.id.prevButton);
preButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
movePre();
}
});
// move down event handler
nxtButton= (ImageButton) findViewById(R.id.nextButton);
nxtButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
moveNxt();
}
});
stationList.setAdapter(new StationAdapter(this,
android.R.layout.simple_list_item_1, _stationList));
} else {
Log.d(TAG, "No Stations");
}
}
private void movePre(){
stationList.setSelection(stationList.getSelectedItemPosition() + 1);
}
// Move selected item "down" in the ViewList.
private void moveNxt(){
stationList.setSelection(stationList.getSelectedItemPosition() + 1);
}
private class StationAdapter extends ArrayAdapter {
private Vector<Station> items;
public StationAdapter(Context context, int textViewResourceId,
Vector<Station> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Station st = items.get(position);
if (st != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (tt != null) {
tt.setText(st.getStationName());
}
if (bt != null) {
bt.setText(st.getCT());
}
}
return v;
}
}
`
© Stack Overflow or respective owner