Open a row in listview automatically (android project) - android

For example; I have long listview and it has 30 lines(items). I want to show this listview but after open the screen, it will show 15. lines. That is, the middle of the listview will be shown automatically. Is it possible? (NOTE: I don't want to show 15. line as first row.)
UPDATE:
i don't want to delete rows. I have listview and it works well. I want to show the middle of list. Scrool will flow until 15. row and i will see all of them but it will show 12. or 15. row when listview opens.

Bind your ListView to ArrayAdapter
final ListView lv = (ListView) findViewById(R.id.lv);
String[] fruits = new String[] {
"Cereus peruvianus",
"Bacupari",
"Beach Plum",
"Black raspberry"};
// Create a List from String Array elements
final List fruits_list = new ArrayList(Arrays.asList(fruits));
// Create an ArrayAdapter from List
final ArrayAdapter arrayAdapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1, fruits_list);
// DataBind ListView with items from ArrayAdapter
lv.setAdapter(arrayAdapter);
Remove / Delete first item from List. You may remove multiple in a loop.
fruits_list.remove(0);
// Notify adapter
arrayAdapter.notifyDataSetChanged();
Edit:
// For scrolling to specific item in your list
lv.smoothScrollToPosition(15); // Here 15 is the position of the item

Answer is lv.setSelection(15);

Related

how to let user enter number of Listview rows to be displayed

I am using a custom array adapter to display grades in a ListView, my question is how can I let a user set the number of rows displayed? I will take their selection in an editbox and then when they hit a button called "select" I will limit the display. I have searched for this elsewhere but couldn't find exactly how to perform this method. Thanks.
Just implement this code after onClick()
for (int i = 0; i < userInput; i++) {
yourList.add(data);
}
MyCustomAdapter dataAdapter = new MyCustomAdapter(yourCkass.this.getActivity(),
R.yourLayoutListItem, yourList);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);

Add new items on top of list view

Because android automatically moves new items to the bottom of list view I want that to be reversed. In any case my condition is met, I want to add new items on top of list view.
I have seen this post here but I don't know how to add that to my code, here it is:
if(condition){
listView = (ListView) findViewById(R.id.ListView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, R.layout.list_b_text, R.id.list_content, ArrayofName);
listView.setAdapter(adapter);
}
Just simply add every item at position 0 of your ArrayList so when you call listView.notifyDataSetChanged(); it will show latest items on top.
for (Object obj : objectList) {
ArrayofName.add (0, obj); // this adds new items at top of ArrayList
}
objectList is basically an ArrayList or List of Object or String (whatever is your case). If you want to add items one by one, remove for loop. This loop actually iterates every item of objectList and adds it in your ArrayList at top position.

How to move multiple selected items from listview to another listview

I have two listviews SAMPLE IMAGE HERE.i want to move items from one listview to another.
i have two buttons,"move to right" and "move to left".
Its a multi selection listview.after select items we need to move this items to another listview.
First listview data from database.code shown below
public void fillcategory() {
Cursor cursor = dataBase
.select("SELECT * FROM t_Category ORDER BY CategoryName");
lacategory = new list(this,
android.R.layout.simple_list_item_activated_1, cursor,
new String[] { "CategoryName" }, new int[] { android.R.id.text1 },1);
lvcategory.setAdapter(lacategory);
lvcategory.setOnItemClickListener(new listclick());
lvcategory.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
There are twio options you can do this, first by adding checkbox in each listitem and then populate the other listview with selected items. for this here is good tutorial Multiple Check
Second, you can set you listview property to multiple_select in adapter and then populated those selected item through position in other listview.
I hope atleast one will work.

Getting checkbox values from ListView

I have a listview of that's tied to an array adapter. For the life of me I can't figure out how to get a list of the checked boxes in the listview.
CheckViewArrayAdapter adapter;
int[] intarray;
paramListView = (ListView) findViewById(R.id.datalog_paramselectlist);
// get all supported params
intarray = ConMan.Ecu.getSupportedParamArrayVals();
LinkedHashMap<Integer,String> hm = new LinkedHashMap<Integer,String>();
for( x=0;x<intarray.length;x++){
hm.put(intarray[x] , ConMan.Ecu.paramToText(intarray[x]));
}
adapter = new CheckViewArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice , android.R.id.text1, hm);
adapter.setBoolArray(ConMan.Ecu.getSelectedParamFlagArray());
// Assign adapter to ListView
paramListView.setAdapter(adapter);
I have a setOnItemClickListener for paramListView that works, but I just want to get the final set of checked checkboxes when the screen exits. I simply don't know where to look.
I have a setOnItemClickListener for paramListView that works, but I just want to get the final set of checkboxes when the screen exits.
I assume that you are not setting the choice mode for your ListView. If you do use:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
then in your onPause() method, you can ask for an index of the checked rows with ListView#getCheckedItemPosition() and you don't need to change the checked state manually in your OnItemClickListener.

Using a Spinner with a SimpleCursorAdapter

I have an activity which has a Spinner widget to display categories. Initially I was using an ArrayAdapter to populate the the spinner as in the following code
private static final String[] arrayCategories = {
"Business",
"Personal"
};
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
ArrayAdapter<String> catAdapter = new ArrayAdapter<String>(this, R.layout.track_category_item, arrayCategories);
catAdapter.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner.setAdapter(catAdapter);
This works fine, and the spinner displays the first array item by default if no selection is made. It does show the selected item when an item is actually selected
But now I want to use a SimpleCursorAdapter to pull the list contents from a db. So I changed it to
SimpleCursorAdapter scaCategories = new SimpleCursorAdapter(this, R.layout.track_category_item,cCategories,new String[] {DBAdapter.KEY_CATEGORIES_NAME},new int[]{R.id.text1});
scaCategories.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
mCatSpinner.setAdapter(scaCategories);
This populates the dropdown, but it does not display the first item in the spinner. Even if selected, it does not show the selected item.
I tried to setSlection to the first item using
if(mCatSpinner.isSelected() != true) {
mCatSpinner.setSelection(0);
}
but it didn't work
What is wrong?
Ok, it would help if I specified the widget id in the layout xml. <:(

Categories

Resources