views:

279

answers:

1

hi, it is possible to have single listview with 2 or more columns ,which is operatable with paging property (i.e. at a time listview will show only 4 items in single column and on press of right arrow it will show next 4 items ).. can u please tell me he procedure to implement it or any idea. Thanks puneet

+1  A: 

Not sure if this is what you're looking for, but basically this code creates a subset of objects based on the main dataset you're using to populate your list. It goes something like this:

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity {

// Constant for limiting array to match desired number of values in column
private final int NUMBER_OF_ITEMS_IN_COLUMN = 4;

// Index for starting point of array subset
private int mStartingIndex = 0;

// Data set array for list
private String[] mDataSet = new String[]{ 
    "One", "Two", "Three", "Four", "Five", 
    "Six","Seven","Eight", "Nine", "Ten",
    "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", 
    "Sixteen", "Seventeen", "Eightteen", "Nineteen", "Twenty",
 };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    changeListViewModel(0);
}

private void changeListViewModel(int startingIndex) {

 // Check staring index meets certain criteria
 if(startingIndex < 0) 
  startingIndex = 0;
 else if(startingIndex >= mDataSet.length)
  startingIndex -= NUMBER_OF_ITEMS_IN_COLUMN;

 // Set starting and ending index
 mStartingIndex  = startingIndex;
 int endingIndex = startingIndex + NUMBER_OF_ITEMS_IN_COLUMN;

 // Make sure ending index isn't outside the bounds of the data set array
 if(endingIndex > mDataSet.length) endingIndex = mDataSet.length;

 // Create subset and set listview adapter
 String[] subSet = getDataSubset(startingIndex, endingIndex);
 setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subSet));
}

private String[] getDataSubset(int startingIndex, int endingIndex){

 String[] toRet = new String[endingIndex - startingIndex];

 int index = -1;
 for(int x = startingIndex; x < endingIndex; x++)
  toRet[++index] = mDataSet[x];

 return toRet;
}

/*
 * Called from layout main.xml file
 */
public void backButtonClicked(View v) {
 changeListViewModel(mStartingIndex - NUMBER_OF_ITEMS_IN_COLUMN);
}


/*
 * Called from layout main.xml file
 */
public void nextButtonClicked(View v) {
 changeListViewModel(mStartingIndex + NUMBER_OF_ITEMS_IN_COLUMN);
}
}

It's pretty basic, but it should be able to get your started. Plus, using something like this, you could also tie in the listview with a SQLite database by having a database class that returns a list of objects as a subset for the listview.

You can download the source here: Download Source

psuguitarplayer
Puneet kaur