identify what I have chosen from a wheel android - android

I am using the code for the wheel chooser from here. So lets say that i want to implement something like the citiesactivities.java. Code is below. What I want now is:
when the user chooses with the slider what he want, press a button Go and take him to a new activity.
So I must insert somewhere a buttonOnClickListener but I do not know where? I mean where in the code, the item selected by the user is specified? I mean if select Canada and Vancouver, press go and take him to CanadaVancouverActivity, if Toronto to CanadaTorontoActivity and so on.
Code is here:
package kankan.wheel.demo;
import kankan.wheel.R;
import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.AbstractWheelTextAdapter;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class CitiesActivity extends Activity {
// Scrolling flag
private boolean scrolling = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cities_layout);
final WheelView country = (WheelView) findViewById(R.id.country);
country.setVisibleItems(3);
country.setViewAdapter(new CountryAdapter(this));
final String cities[][] = new String[][] {
new String[] {"New York", "Washington", "Chicago", "Atlanta", "Orlando"},
new String[] {"Ottawa", "Vancouver", "Toronto", "Windsor", "Montreal"},
new String[] {"Kiev", "Dnipro", "Lviv", "Kharkiv"},
new String[] {"Paris", "Bordeaux"},
};
final WheelView city = (WheelView) findViewById(R.id.city);
city.setVisibleItems(5);
country.addChangingListener(new OnWheelChangedListener() {
public void onChanged(WheelView wheel, int oldValue, int newValue) {
if (!scrolling) {
updateCities(city, cities, newValue);
}
}
});
country.addScrollingListener( new OnWheelScrollListener() {
public void onScrollingStarted(WheelView wheel) {
scrolling = true;
}
public void onScrollingFinished(WheelView wheel) {
scrolling = false;
updateCities(city, cities, country.getCurrentItem());
}
});
country.setCurrentItem(1);
}
/**
* Updates the city wheel
*/
private void updateCities(WheelView city, String cities[][], int index) {
ArrayWheelAdapter<String> adapter =
new ArrayWheelAdapter<String>(this, cities[index]);
adapter.setTextSize(18);
city.setViewAdapter(adapter);
city.setCurrentItem(cities[index].length / 2);
}
/**
* Adapter for countries
*/
private class CountryAdapter extends AbstractWheelTextAdapter {
// Countries names
private String countries[] =
new String[] {"USA", "Canada", "Ukraine", "France"};
// Countries flags
private int flags[] =
new int[] {R.drawable.usa, R.drawable.canada, R.drawable.ukraine, R.drawable.france};
/**
* Constructor
*/
protected CountryAdapter(Context context) {
super(context, R.layout.country_layout, NO_RESOURCE);
setItemTextResource(R.id.country_name);
}
#Override
public View getItem(int index, View cachedView, ViewGroup parent) {
View view = super.getItem(index, cachedView, parent);
ImageView img = (ImageView) view.findViewById(R.id.flag);
img.setImageResource(flags[index]);
return view;
}
#Override
public int getItemsCount() {
return countries.length;
}
#Override
protected CharSequence getItemText(int index) {
return countries[index];
}
}
}

Finally I found the answer. There is a function in the package named getCurrentItem().

Related

RecyclerView of Button array - How to update the data?

This is my first Q on the site, I'll try to form it well.
I have a recyclerView, and the Item is an array of buttons.
Button click shows a popup menu that allows the user to change the color of the button.
I've managed to set that the onClick method will change the color, but I'm clueless about how to save the chosen color in the ButtonArrayList, that Holds the colors.
The problem is that when the button is pressed, I don't know how to understand programatically on which button, in which button array it was pressed.
Thanks!
Just to demonstrate the problem. when button is clicked, How to identify which button of which item was clicked?
1
The code of the fragment:
package com.examples.recyclerViewWithButtonArray;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.LinkedList;
/**
* A simple {#link Fragment} subclass.
* Use the {#link Game#newInstance} factory method to
* create an instance of this fragment.
*/
public class Game extends Fragment {
// TODO: Rename parameter arguments, choose names that match
protected static int[] mButtonsColors;
private final LinkedList<ButtonArray> mButtonArrayList = new LinkedList<>();
RecyclerView mGuessLinesRecyclerView;
ButtonArrayListAdapter mButtonArrayListAdapter;
FloatingActionButton mFab;
// TODO: Rename and change types of parameters
public Game() {
// Required empty public constructor
}
public static Game newInstance(String param1, String param2) {
Game fragment = new Game();
return fragment;
}
private void initGameColors(){
mButtonsColors = new int[4];
int[] c = getContext().getResources().getIntArray(R.array.buttonColors);
//asign colors
for (int i = 0; i < 4; i++) {
this.mButtonsColors[i] = c[i];
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initGameColors();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vw = inflater.inflate(R.layout.fragment_game, container, false);
return vw;
}
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mGuessLinesRecyclerView = view.findViewById(R.id.recycler_view);
mFab = view.findViewById(R.id.fab);
//initialize recyclerView
// Create an adapter and supply the data to be displayed.
mButtonArrayListAdapter = new ButtonArrayListAdapter(getContext(), mButtonArrayList, this);
// Connect the adapter with the RecyclerView.
mGuessLinesRecyclerView.setAdapter(mButtonArrayListAdapter);
// Give the RecyclerView a default layout manager.
mGuessLinesRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//initializeFAB
mFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mButtonArrayList.add(new ButtonArray(getContext(),mButtonsColors));
mGuessLinesRecyclerView.setAdapter(mButtonArrayListAdapter);
}
});
//add first array to the recycler view.
// Next guess lines will be added when clicking on movableFab
mButtonArrayList.add(new ButtonArray(getContext(), this.mButtonsColors));
}
}
The code of ButtonArrayListAdapter:
package com.examples.recyclerViewWithButtonArray;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ButtonArrayListAdapter extends RecyclerView.Adapter<ButtonArrayListAdapter.ButtonArrayViewHolder> {
private final Context mContext;
private final List<ButtonArray> mData;
class ButtonArrayViewHolder extends RecyclerView.ViewHolder {
public ArrayList<Button> mButtons;
final ButtonArrayListAdapter mAdapter;
public ButtonArrayViewHolder(#NonNull View itemView, ButtonArrayListAdapter adapter) {
super(itemView);
this.mAdapter = adapter;
mButtons =new ArrayList<>();
if(4==4)
{
//create an array of button for binding
mButtons.add((Button)itemView.findViewById(R.id.button_Guess1));
mButtons.add((Button)itemView.findViewById(R.id.button_Guess2));
mButtons.add((Button)itemView.findViewById(R.id.button_Guess3));
mButtons.add((Button)itemView.findViewById(R.id.button_Guess4));
}
}
}
public ButtonArrayListAdapter(Context mContext, List<ButtonArray> mData, Game mGame) {
this.mContext = mContext;
this.mData = mData;
}
#NonNull
#Override
public ButtonArrayViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View layout;
layout = LayoutInflater.from(mContext).inflate(R.layout.guess_line,parent,false);
return new ButtonArrayViewHolder(layout,this);
}
#Override
public void onBindViewHolder(#NonNull final ButtonArrayViewHolder buttonArrayViewHolder, final int position) {
//bind data here
//initiate each guessLineButton
for (int i = 0; i < 4; i++) {
int c = mData.get(position).mAnswerButtonsColors[i];
final Button bt = buttonArrayViewHolder.mButtons.get(i);
//set initial button color
bt.setBackgroundColor(c);
//set button clik to open color chooser
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int[] chooseColorButtons = new int[4];
// inflate the layout of the popup window
final View popupView = LayoutInflater.from(mContext).inflate(R.layout.choose_color_popup,null);
// create the popup window
int width = bt.getWidth();
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
int[] loc = new int[]{0,0};
bt.getLocationOnScreen(loc);
popupWindow.showAtLocation(v, Gravity.TOP|Gravity.LEFT, loc[0], loc[1] + bt.getHeight());
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
//initiate each color choose button
if(chooseColorButtons.length==4) {
chooseColorButtons[0] = R.id.buttonColor1;
chooseColorButtons[1] = R.id.buttonColor2;
chooseColorButtons[2] = R.id.buttonColor3;
chooseColorButtons[3] = R.id.buttonColor4;
}
for (int j = 0; j < 4 ; j++) {
Button colbt = (Button)(popupView.findViewById(chooseColorButtons[j]));
colbt.setBackgroundColor(Game.mButtonsColors[j]);
colbt.setTextColor(Game.mButtonsColors[j]);
colbt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bt.setBackgroundColor(((ColorDrawable)(((Button)v).getBackground())).getColor());
//WHAT SHOULD I DO HERE?
popupWindow.dismiss();
}
});
}
}
});
}
}
#Override
public int getItemCount() {
return mData.size();
}
}
After reading your question what I get is you want to save the color against every button so next time when that button click the same color should load if yes then you can consider these tw0 suggestions
1- if the number of buttons is static and you are not adding them dynamically then you can hardcode the color against each button by adding them in the list and assign them manually against each button.
2- you can use key-value pair(hash map), these are the best solution for any case either you're are manually adding the button or dynamically, just store the color against each key. In this case, buttons should be your keys and colors will be value.
Thanku

recycling listview with checkbox

i have already searched high and low on the net.
tried several tips and tutorials but none is really working for me.
What i am trying to do is using a multiple choicelist to delete more than 1 entry in my list.
everything is working, and the list is set to multiplechoice in the xml, but when trying to update my custom list i can't seem to fix that recycling.
I am using a resourcecursoradapter, no idea if that is the problem or not, but i'm at a loss right now, so if any body could help me that would be great.
Now for the code of my Activity.
package com.ShaHar91.ivlibrary;
import java.io.IOException;
import java.io.InputStream;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
#SuppressWarnings("deprecation")
public class DeleteMultiPoke extends ListActivity {
public static final String ROW_ID = "row_id"; // Intent extra key
private long rowID;
MyAdapter mListAdapter;
Menu menu;
MenuItem delete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
DatabaseConnector myDbHelper = new DatabaseConnector(
DeleteMultiPoke.this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
DatabaseConnector.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
Cursor myCur = null;
myCur = myDbHelper.getAllPokes();
mListAdapter = new MyAdapter(DeleteMultiPoke.this, myCur);
setListAdapter(mListAdapter);
}
private class MyAdapter extends ResourceCursorAdapter {
public MyAdapter(Context context, Cursor cur) {
super(context, R.layout.poke_list_remove_item, cur);
}
#Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.poke_list_remove_item, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cur) {
TextView pokeTv = (TextView) view.findViewById(R.id.pokeTv);
TextView genderTv = (TextView) view.findViewById(R.id.genderTv);
RadioButton hpRb = (RadioButton) view.findViewById(R.id.hpRb);
RadioButton attRb = (RadioButton) view.findViewById(R.id.attRb);
RadioButton defRb = (RadioButton) view.findViewById(R.id.defRb);
RadioButton spAttRb = (RadioButton) view.findViewById(R.id.spAttRb);
RadioButton spDefRb = (RadioButton) view.findViewById(R.id.spDefRb);
RadioButton speedRb = (RadioButton) view.findViewById(R.id.speedRb);
ImageView pokeSprite = (ImageView) view
.findViewById(R.id.pokeSpriteIV);
pokeTv.setText(cur.getString(cur.getColumnIndex("name")));
genderTv.setText(cur.getString(cur.getColumnIndex("gender")));
hpRb.setChecked((cur.getInt(cur.getColumnIndex("hp")) == 0 ? false
: true));
attRb.setChecked((cur.getInt(cur.getColumnIndex("att")) == 0 ? false
: true));
defRb.setChecked((cur.getInt(cur.getColumnIndex("def")) == 0 ? false
: true));
spAttRb.setChecked((cur.getInt(cur.getColumnIndex("sp_att")) == 0 ? false
: true));
spDefRb.setChecked((cur.getInt(cur.getColumnIndex("sp_def")) == 0 ? false
: true));
speedRb.setChecked((cur.getInt(cur.getColumnIndex("speed")) == 0 ? false
: true));
AssetManager assetManager = getAssets();
int imageIndex = cur.getColumnIndex("nat_dex");
try {
InputStream ims = assetManager.open("pokes/"
+ cur.getString(imageIndex) + ".gif");
Drawable d = Drawable.createFromStream(ims, null);
pokeSprite.setImageDrawable(d);
} catch (IOException ex) {
return;
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.poke_menu_remove, menu);
delete = menu.findItem(R.id.deletepokes);
delete.setEnabled(false);
delete.setVisible(false);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.canceldeleting:
finish();
return true;
case R.id.deletepokes:
// create a new AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(
DeleteMultiPoke.this);
builder.setTitle(R.string.confirmTitle); // title bar string
builder.setMessage(R.string.confirmMessage); // message to display
// provide an OK button that simply dismisses the dialog
builder.setPositiveButton(R.string.button_delete,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int button) {
final DatabaseConnector databaseConnector = new DatabaseConnector(
DeleteMultiPoke.this);
ListView listView = (ListView) findViewById(android.R.id.list);
SparseBooleanArray checked = listView
.getCheckedItemPositions();
for (int i = checked.size() - 1; i >= 0; i--) {
final int position = checked.keyAt(i);
if (checked.valueAt(i)) {
AsyncTask<Long, Object, Object> deleteTask = new AsyncTask<Long, Object, Object>() {
#Override
protected Object doInBackground(
Long... params) {
databaseConnector.deletePoke(mListAdapter
.getItemId(position));
return null;
}
#Override
protected void onPostExecute(
Object result) {
finish(); // return to the
// BookLibrary Activity
}
};
// delete the AsyncTask to delete book at
// rowID
deleteTask.execute(new Long[] { rowID });
}
}
} // end method onClick
});
builder.setNegativeButton(R.string.button_cancel, null);
builder.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onResume() {
super.onResume();
mListAdapter.getCursor().requery();
}
#Override
protected void onStop() {
super.onStop();
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selected = "";
int cntChoice = l.getCount();
SparseBooleanArray sparseBooleanArray = l.getCheckedItemPositions();
for (int i = 0; i < cntChoice; i++) {
if (sparseBooleanArray.get(i)) {
selected += l.getItemAtPosition(i).toString() + "\n";
}
}
Toast.makeText(DeleteMultiPoke.this,
selected,
Toast.LENGTH_LONG).show();
if (l.getCheckedItemCount() == 0) {
delete.setEnabled(false);
delete.setVisible(false);
} else {
delete.setEnabled(true);
delete.setVisible(true);
}
}
Ty in advance,
Christiano Bolla
You should notify the list that the underlying data source has changed by calling notifyDataSetChanged() on your list adapter after deleting any item.

Maintain filtered listview after rotation in android

I'm using customized ListView in android, and using search with customized EditText. When I search for a name, I will get the result using getFilter().filter(cs);. But, when I rotate, the ListView shows the full list (without filtering), then after a delay, it shows the filtered list.
When I rotate without filtering, it maintains the listview position.
So I have to maintain the filtered ListView when I rotate the screen.
Please help. Thanks in advance...
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.content.Loader;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
#SuppressLint("HandlerLeak")
public class ContactsListActivity
extends Activity
implements LoaderManager.LoaderCallbacks<ArrayList<ListViewItem>> {
private LetterScrollListView mLSListView;
private ProgressBar mProgressBar;
private EditText mSearchText;
private ProgressBar mSearchProgressBar;
private LinearLayout mViewsLl;
private ContactAdapter mFullContactAdapter = null;
private ArrayList<ListViewItem> mFullContactsArrayList = null;
public boolean isSoftKeyOn = false;
public boolean isPortrait;
private long mLastTimeTextChanged = 0L;
private long mCurrentTime = 0L;
private Filter mFilter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts_list);
RelativeLayout windowRl = (RelativeLayout) findViewById(R.id.window_RelativeLayout);
LinearLayout progressBarLl = (LinearLayout) windowRl.findViewById(R.id.progressBar_LinearLayout);
mViewsLl = (LinearLayout) windowRl.findViewById(R.id.views_LinearLayout);
mSearchText = (EditText) mViewsLl.findViewById(R.id.contact_SearchEditText);
mSearchProgressBar = (ProgressBar) mViewsLl.findViewById(R.id.search_ProgressBar);
mLSListView = (LetterScrollListView) mViewsLl.findViewById(R.id.contacts_LetterScrollListView);
mLSListView.setSearchText(mSearchText);
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
isPortrait = true;
} else {
isPortrait = false;
}
final View activityRootView = windowRl;
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView()
.getHeight() - activityRootView.getHeight();
// if heightDiff > 100 pixels, its probably a keyboard...
if (heightDiff > 100) {
// Toast.makeText(getApplicationContext(), "Key ON",
// Toast.LENGTH_SHORT).show();
isSoftKeyOn = true;
} else {
// Toast.makeText(getApplicationContext(), "Key OFF",
// Toast.LENGTH_SHORT).show();
isSoftKeyOn = false;
mSearchText.clearFocus();
}
}
});
mSearchText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
mCurrentTime = System.currentTimeMillis();
if( (mCurrentTime - mLastTimeTextChanged) <= USER_TYPE_DELAY ) {
mSearchInputHandler.removeMessages(WHAT_SEARCH);
}
// preparing message
Bundle bundle = new Bundle();
bundle.putCharSequence(USER_INPUT_KEY, cs);
Message message = mSearchInputHandler.obtainMessage(WHAT_SEARCH);
message.setData(bundle);
mSearchInputHandler.sendMessageDelayed(message, USER_TYPE_DELAY);
mLastTimeTextChanged = mCurrentTime;
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
// Create a progress bar to display while the list loads
mProgressBar = new ProgressBar(this);
mProgressBar.setIndeterminate(true);
progressBarLl.addView(mProgressBar);
getLoaderManager().initLoader(0, null, this).forceLoad();
}
#SuppressLint("HandlerLeak")
private final Handler mSearchInputHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
CharSequence cs = msg.getData().getCharSequence(USER_INPUT_KEY);
int what = msg.what;
switch(what) {
case WHAT_SEARCH:
// When user changed the Text after a delay
mSearchProgressBar.setVisibility(View.VISIBLE);
mFilter.filter(cs);
break;
}
}
};
public void removeSearchProgressBar() {
mSearchProgressBar.setVisibility(View.INVISIBLE);
}
/**
* Called when a new Loader needs to be created
*/
public Loader<ArrayList<ListViewItem>> onCreateLoader(int id, Bundle args) {
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
boolean showInvisible = false;
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(showInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return new ContactArrayListLoader(this, uri, projection, selection, selectionArgs, sortOrder);
}
/**
* Called when a previously created loader has finished loading
*/
public void onLoadFinished(Loader<ArrayList<ListViewItem>> loader, ArrayList<ListViewItem> data) {
mProgressBar.setVisibility(View.INVISIBLE);
if(mLSListView.getAdapter() == null) {
mFullContactsArrayList = data;
mFullContactAdapter = new ContactAdapter(this, R.layout.contact_tuple, mFullContactsArrayList);
mFilter = mFullContactAdapter.getFilter();
mLSListView.setAdapter(mFullContactAdapter);
}
}
/**
* Called when a previously created loader is reset, making the data unavailable
*/
public void onLoaderReset(Loader<ArrayList<ListViewItem>> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
//////mListView.setAdapter(new ContactAdapter(this, R.layout.contact_tuple, null));
mProgressBar.setVisibility(View.VISIBLE);
}
}
SOLVED
The problem is on the method
public void onLoadFinished(Loader<ArrayList<ListViewItem>> loader, ArrayList<ListViewItem> data) {....}
On rotation/starting the app, the parameter data in this method is always the complete list and not the filtered list. So to get the filtered list on rotation, just create a static member
private static ArrayList<ListViewItem> sCurrentContactsArrayList = null;
and save before screen rotation like this:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if( TextUtils.isEmpty(mSearchText.getText()) ) {
sCurrentContactsArrayList = null;
} else {
sCurrentContactsArrayList = mFullContactAdapter.getCurrentItems();
}
}
also modify onLoadFinished():
public void onLoadFinished(Loader<ArrayList<ListViewItem>> loader, ArrayList<ListViewItem> data) {
mProgressBar.setVisibility(View.INVISIBLE);
if(mLSListView.getAdapter() == null) {
ArrayList<ListViewItem> contactsAL = null;
if(sCurrentContactsArrayList == null) {
contactsAL = data;
} else {
contactsAL = sCurrentContactsArrayList;
}
mFullContactAdapter = new ContactAdapter(this, R.layout.contact_tuple, contactsAL, data);
if(mFilter == null) mFilter = mFullContactAdapter.getFilter();
mLSListView.setAdapter(mFullContactAdapter);
}
mSearchText.setVisibility(View.VISIBLE);
}

Open Multiple Listview Item Click to One Class

Hope you guys can help.
I have a activity which handles all the 10 image button clicks and list view intents. What i am looking to do is have 1 layout for all the list view button clicks. And in this layout call different data to it. When i started this project i had many activitys until a great stack overflow user pointed out that i can make it simpler which i did and made it a lot clear.
package com.example.testtest;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Listviewact extends Activity {
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.listview_layout);
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/AlexBrush-Regular-OTF.otf");
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setTypeface(tf);
}
public void onResume() {
super.onResume();
int buttonId = getIntent().getIntExtra("buttonId", 0);
int buttonIdx = getButtonIdx(buttonId);
// find and set image according to buttonId
int imageId = IMAGE_IDS[buttonIdx]; // image to show for given button
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setImageResource(imageId);
// find and set listview imtes according to buttonId
String[] items = LISTVIEW_DATA[buttonIdx]; // listview items to show for given button
ListView listView = (ListView)findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
}
private void setListAdapter(ArrayAdapter adapter) {
// TODO Auto-generated method stub
}
// a little helper to map ids to array indices
// to be able to fetch the correct image and listview data later
private final static int[] BUTTON_IDS = new int[] {
R.id.imageButton1,
R.id.imageButton2,
R.id.imageButton3,
R.id.imageButton4,
R.id.imageButton5,
R.id.imageButton6
};
// 6 images
private final static int[] IMAGE_IDS = new int[] {
R.drawable.bmw,
R.drawable.ford,
R.drawable.honda,
R.drawable.toy,
R.drawable.vok2,
R.drawable.ic_launcher
};
// 6 different sets of strings for the listviews
private final static String[][] LISTVIEW_DATA = new String[][] {
{"First A", "First B", "First C", "First D","First E","First F"},
{"Second A", "Second B", "Second C"},
{"Third A", "Third B", "Third C"},
{"Forth A", "Forth B", "Forth C"},
{"Fifth A", "Fifth B", "Fifth C"},
{"Sixth A", "Sixth B", "Sixth C"},
};
// map button id to array index
static private int getButtonIdx(int id) {
for(int i = 0; i<BUTTON_IDS.length; i++) {
if (BUTTON_IDS[i] == id) return i;
}
return 0; // should not happen
}
}
It would be great if someone can show me how to make a class which i can call all the item clicks from all list views too from my code here.
package com.example.testtest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
public class MainActivity extends Activity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_of_button);
ImageButton btn1 = (ImageButton)findViewById(R.id.imageButton1);
ImageButton btn2 = (ImageButton)findViewById(R.id.imageButton2);
ImageButton btn3 = (ImageButton)findViewById(R.id.imageButton3);
ImageButton btn4 = (ImageButton)findViewById(R.id.imageButton4);
ImageButton btn5 = (ImageButton)findViewById(R.id.imageButton5);
ImageButton btn6 = (ImageButton)findViewById(R.id.imageButton6);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
// if one of the image buttons is pressed...
case R.id.imageButton1:
case R.id.imageButton2:
case R.id.imageButton3:
case R.id.imageButton4:
case R.id.imageButton5:
case R.id.imageButton6:
Intent intent = new Intent(this, Listviewact.class);
// pass ID of pressed button to listview-activity
intent.putExtra("buttonId", v.getId());
startActivity(intent);
break;
// here you could place handling of other clicks if necessary...
}
}
private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
// TODO Auto-generated method stub
}
private ListView getListView() {
// TODO Auto-generated method stub
return null;
}
}
CHEERS.
http://img40.imageshack.us/img40/705/f6h9.png
If I understand what you want, you could create a class with something like a static Arraylist to be appended each time an item is clicked. So create a class something like
public class Data class
{
static ArrayList<String> dataArray = new ArrayList<String>();;
public Data()
{
// empty constructor but could be used if needed
}
Then you can add different getters/setters or whatever you need here. When you click on an item you just call something like
Data.dataArray.add("stuff");
then retrieve items in here in your next Activity.
If that is too complicated or more than you need then you can just pass an ArrayList or whatever object you need through the Intent
Intents
Also, just a preference but since all of your Buttons do the same thing, you can do away with initializing them and setting listeners on all of them. In xml just add
`android:onClick="someFunctionName"`
to each Button then use that function name
public void someFunctionName(View v) {
switch(v.getId()) {
// if one of the image buttons is pressed...
Intent intent = new Intent(this, Listviewact.class);
// pass ID of pressed button to listview-activity
intent.putExtra("buttonId", v.getId());
startActivity(intent);
break;
// here you could place handling of other clicks if necessary...
}
There is also no need for the case statements since they all do the same thing and you don't need implements OnClickListener in the Activity declaration
You're using the ListView but not using any of it's callbacks? Here, this is my code that I use for my ListView. I'm putting activities in my array, but you could put anything. Modifying the R.layout.mfd_view allows you to put whatever you want for each list item. A Button if that's what you need. Hope this helps. I'm still learning myself.
import android.app.Activity;
import android.app.ListFragment;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MyListFragment extends ListFragment {
String fragmentBackStack;
MyMapHandler handler;
OnViewSelectedListener mListener;
/**
* An array of POJOs used to hold the info about the fragments we'll be
* swapping between This should be inserted into an array adapter of some
* sort before being passed onto ListAdapter
*/
private static final ViewDetails[] ACTIVITY_DETAILS = {
new ViewDetails(R.string.action_largeTach,
R.string.largeTach_description, LargeTachActivity.class),
new ViewDetails(R.string.action_map, R.string.map_description,
MyMapHandler.class),
new ViewDetails(R.string.action_navigation,
R.string.navigation_description, NavigationActivity.class),
new ViewDetails(R.string.action_raceMode,
R.string.raceMode_description, RaceModeActivity.class),
new ViewDetails(R.string.action_settings,
R.string.settings_description, SettingsFragment.class),
new ViewDetails(R.string.action_extraInfo,
R.string.extraInfo_description, ExtraInfoActivity.class) };
/**
* #author PyleC1
*
* A POJO that holds a class object and it's resource info
*/
public static class ViewDetails {
private final Class<? extends Activity> viewActivity;
private int titleId;
private int descriptionId;
/**
* #param titleId
* The resource ID of the string for the title
* #param descriptionId
* The resource ID of the string for the description
* #param activityClass
* The fragment's class associated with this list position
*/
ViewDetails(int titleId, int descriptionId,
Class<? extends Activity> viewActivity) {
super();
this.titleId = titleId;
this.descriptionId = descriptionId;
this.viewActivity = viewActivity;
}
public Class<? extends Activity> getViewActivity() {
return viewActivity;
}
}
/**
* #author PyleC1
*
* Extends the ArrayAdapter class to support our custom array that
* we'll insert into the ListAdapter so the user can pick between
* MFD screens at boot time.
*/
private static class CustomArrayAdapter extends ArrayAdapter<ViewDetails> {
public CustomArrayAdapter(Context context, ViewDetails[] activities) {
super(context, R.layout.mfd_view, R.id.mfdTitle, activities);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MFDView mfdView;
if (convertView instanceof MFDView) {
mfdView = (MFDView) convertView;
} else {
mfdView = new MFDView(getContext());
}
ViewDetails details = getItem(position);
mfdView.setTitleId(details.titleId);
mfdView.setDescriptionId(details.descriptionId);
return mfdView;
}
}
public void onAttach(Activity activity) {
super.onAttach(activity);
ListAdapter listAdapter = new CustomArrayAdapter(getActivity(),
ACTIVITY_DETAILS);
setListAdapter(listAdapter);
try {
mListener = (OnViewSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnViewSelectedListener!");
}
}
#Override
public void onResume() {
super.onResume();
}
public interface OnViewSelectedListener {
public void onViewSelected(Class<? extends Activity> activityClass);
}
public void onListItemClick(ListView l, View v, int position, long id) {
ViewDetails details = (ViewDetails) getListAdapter().getItem(position);
mListener.onViewSelected(details.viewActivity);
}
}
Note that whatever activity calls this fragment must implement the OnViewSelectedListener interface. If you added this to your main activity as a subclass, this wouldn't be required. The public void onListItemClick(arg0, arg1, arg2, arg3) callback is fine. You just swap fragments or call activities from inside there.

Android loaders, the way to go?

I am used to building lists in android using adapters. If I need some long-to-get data, I use an asynctask, or a simple runnable, to update the data structure on which the adapter rely, and call notifyDataChanged on the adapter.
Although it is not straightforward, I finally find this is a simple model and it allows a good separation of logic presentation (in the asynctask, update a data structure) and the view (an adapter acting as a view factory, mostly).
Nevertheless, I read recently about loaders introduced in HoneyComb and included in the backward compatibility support-library, I tried them and find the introduce a lot of complexity. They are difficult to handle and add some kind of magic to this whole process through loader managers, add a lot of code and don't decrease the number of classes or collaborating items but I may be wrong and would like to hear some good points on loaders.
What are they advantages of loaders in terms of lines of code, clarity and effort ?
What are they advantages of loaders in terms of role separation during data loading, or more broadly, in terms of design ?
Are they the way to go, should I replace all my list data loading to implement them through loaders ?
Ok, this is a developers' forum, so here is an example. Please, make it better with loaders :
package com.sof.test.loader;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/** The activity. */
public class LoaderTestActivity extends ListActivity {
private DataSourceOrDomainModel dataSourceOrDomainModel = new DataSourceOrDomainModel();
private List<Person> listPerson;
private PersonListAdapter personListAdapter;
private TextView emptyView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listPerson = new ArrayList<Person>();
personListAdapter = new PersonListAdapter( listPerson );
setListAdapter( personListAdapter );
setUpEmptyView();
new PersonLoaderThread().execute();
}
public void setUpEmptyView() {
emptyView = new TextView( this );
emptyView.setLayoutParams( new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ) );
emptyView.setVisibility(View.GONE);
((ViewGroup)getListView().getParent()).addView(emptyView);
getListView().setEmptyView(emptyView);
}
/** Simulate a long task to get data. */
private class PersonLoaderThread extends AsyncTask<Void, Integer, List<Person>> {
#Override
protected List<Person> doInBackground(Void... params) {
return dataSourceOrDomainModel.getListPerson( new ProgressHandler());
}
#Override
protected void onProgressUpdate(Integer... values) {
emptyView.setText( "Loading data :" + String.valueOf( values[ 0 ] ) +" %" );
}
#Override
protected void onPostExecute(List<Person> result) {
listPerson.clear();
listPerson.addAll( result );
personListAdapter.notifyDataSetChanged();
}
private class ProgressHandler implements ProgressListener {
#Override
public void personLoaded(int count, int total) {
publishProgress( 100*count / total );
}
}
}
/** List item view factory : the adapter. */
private class PersonListAdapter extends ArrayAdapter<Person> {
public PersonListAdapter( List<Person> listPerson ) {
super(LoaderTestActivity.this, 0, listPerson );
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if( convertView == null ) {
convertView = new PersonView( getContext() );
}
PersonView personView = (PersonView) convertView;
personView.setPerson( (Person) getItem(position) );
return personView;
}
}
}
A small callback interface for progress
package com.sof.test.loader;
/** Callback handler during data load progress. */
public interface ProgressListener {
public void personLoaded(int count, int total );
}
A list item widget
package com.sof.test.loader;
import com.sof.test.loader.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
/** List Item View, display a person */
public class PersonView extends LinearLayout {
private TextView personNameView;
private TextView personFirstNameView;
public PersonView(Context context) {
super(context);
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate( R.layout.person_view,this );
personNameView = (TextView) findViewById( R.id.person_name );
personFirstNameView = (TextView) findViewById( R.id.person_firstname );
}
public void setPerson( Person person ) {
personNameView.setText( person.getName() );
personFirstNameView.setText( person.getFirstName() );
}
}
It's xml : res/person_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/person_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/person_firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/person_name" />
</RelativeLayout>
The data source or model, providing data (slowly)
package com.sof.test.loader;
import java.util.ArrayList;
import java.util.List;
/** A source of data, can be a database, a WEB service or a model. */
public class DataSourceOrDomainModel {
private static final int PERSON_COUNT = 100;
public List<Person> getListPerson( ProgressListener listener ) {
List<Person> listPerson = new ArrayList<Person>();
for( int i=0; i < PERSON_COUNT ; i ++ ) {
listPerson.add( new Person( "person", "" + i ) );
//kids, never do that at home !
pause();
if( listener != null ) {
listener.personLoaded(i,PERSON_COUNT);
}//if
}
return listPerson;
}//met
private void pause() {
try {
Thread.sleep( 100 );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
The POJO representing a person :
package com.sof.test.loader;
/** A simple POJO to be displayed in a list, can be manipualted as a domain object. */
public class Person {
private String name;
private String firstName;
public Person(String name, String firstName) {
this.name = name;
this.firstName = firstName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}//class
In case someone is looking for the loader version of my previous example : here it is :
package com.sof.test.loader;
import java.util.ArrayList;
import android.app.LoaderManager;
import java.util.List;
import android.app.ListActivity;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.content.Loader;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/** The activity. */
public class LoaderTestActivity2 extends ListActivity implements
LoaderManager.LoaderCallbacks<List<Person>> {
private DataSourceOrDomainModel dataSourceOrDomainModel = new DataSourceOrDomainModel();
private List<Person> listPerson;
private PersonListAdapter personListAdapter;
private TextView emptyView;
private Loader<List<Person>> personLoader;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listPerson = new ArrayList<Person>();
personListAdapter = new PersonListAdapter(listPerson);
setListAdapter(personListAdapter);
personLoader = new PersonLoader(this, dataSourceOrDomainModel, new ProgressHandler() );
setUpEmptyView();
getLoaderManager().initLoader(0, null, this);
personLoader.forceLoad();
}
public void setUpEmptyView() {
emptyView = new TextView(this);
emptyView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
emptyView.setVisibility(View.GONE);
((ViewGroup) getListView().getParent()).addView(emptyView);
getListView().setEmptyView(emptyView);
}
public void publishProgress(int progress) {
emptyView.setText("Loading data :" + String.valueOf(progress) + " %");
}
#Override
public Loader<List<Person>> onCreateLoader(int arg0, Bundle arg1) {
return personLoader;
}
#Override
public void onLoadFinished(Loader<List<Person>> personLoader, List<Person> result) {
listPerson.clear();
listPerson.addAll(result);
personListAdapter.notifyDataSetChanged();
}
#Override
public void onLoaderReset(Loader<List<Person>> arg0) {
listPerson.clear();
personListAdapter.notifyDataSetChanged();
}
/** List item view factory : the adapter. */
private class PersonListAdapter extends ArrayAdapter<Person> {
public PersonListAdapter(List<Person> listPerson) {
super(LoaderTestActivity2.this, 0, listPerson);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new PersonView(getContext());
}
PersonView personView = (PersonView) convertView;
personView.setPerson((Person) getItem(position));
return personView;
}
}
private class ProgressHandler implements ProgressListener {
#Override
public void personLoaded(final int count, final int total) {
runOnUiThread( new Runnable() {
#Override
public void run() {
publishProgress(100 * count / total);
}
});
}
}
}
class PersonLoader extends AsyncTaskLoader<List<Person>> {
private DataSourceOrDomainModel dataSourceOrDomainModel;
private ProgressListener progressHandler;
public PersonLoader(Context context, DataSourceOrDomainModel dataSourceOrDomainModel, ProgressListener progressHandler ) {
super(context);
this.dataSourceOrDomainModel = dataSourceOrDomainModel;
this.progressHandler = progressHandler;
}
#Override
public List<Person> loadInBackground() {
return dataSourceOrDomainModel.getListPerson( progressHandler );
}
}
It would be more difficult to add support (support librairy) to this example as there is no equivalent of ListAcitivity in the support librairy. I would have either to create a ListFragment or create an FragmentActivity and give it a layout including a list.
One problem your code has which loaders aim to fix is what happens if your activity is restarted (say due to device rotation or config change) while your async task is still in progress? in your case your restarted activity will start a 2nd instance of the task and throw away the results from the first one. When the first one completes you can end up with crashes due to the fact your async task has a reference is what is now a finished activity.
And yes using loaders often makes for more/more complex code, particularly if you can't use one of the provided loaders.

Categories

Resources