I found a code from here
it is like
public class MainActivity extends ListActivity implements
SwipeActionAdapter.SwipeActionListener
{
protected SwipeActionAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] content = new String[20];
for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(
this,
R.layout.row_bg,
R.id.text,
new ArrayList<String>(Arrays.asList(content))
);
mAdapter = new SwipeActionAdapter(stringAdapter);
mAdapter.setSwipeActionListener(this)
.setListView(getListView());
setListAdapter(mAdapter);
mAdapter.addBackground(SwipeDirections.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)
.addBackground(SwipeDirections.DIRECTION_NORMAL_LEFT,R.layout.row_bg_left)
.addBackground(SwipeDirections.DIRECTION_FAR_RIGHT,R.layout.row_bg_right_far)
.addBackground(SwipeDirections.DIRECTION_NORMAL_RIGHT,R.layout.row_bg_right);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onListItemClick(ListView listView, View view, int position, long id){
Toast.makeText(
this,
"Clicked "+mAdapter.getItem(position),
Toast.LENGTH_SHORT
).show();
}
#Override
public boolean hasActions(int position){
return true;
}
#Override
public boolean shouldDismiss(int position, int direction){
return direction == SwipeDirections.DIRECTION_NORMAL_LEFT;
}
#Override
public void onSwipe(int[] positionList, int[] directionList){
for(int i=0;i<positionList.length;i++) {
int direction = directionList[i];
int position = positionList[i];
String dir = "";
switch (direction) {
case SwipeDirections.DIRECTION_FAR_LEFT:
dir = "Far left";
break;
case SwipeDirections.DIRECTION_NORMAL_LEFT:
dir = "Left";
break;
case SwipeDirections.DIRECTION_FAR_RIGHT:
dir = "Far right";
break;
case SwipeDirections.DIRECTION_NORMAL_RIGHT:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Test Dialog").setMessage("You swiped right").create().show();
dir = "Right";
break;
}
Toast.makeText(
this,
dir + " swipe Action triggered on " + mAdapter.getItem(position),
Toast.LENGTH_SHORT
).show();
mAdapter.notifyDataSetChanged();
}
}
}
I want to remove the string section and want to display apps.
I want to use SwipeActionAdapter to replace listview from my app which i used earlier to show app packages but unable to understand how to do that with this..
Go a bit easy on me if this is a very noob-ish question. I m a begginer.
I think you are misunderstanding the purpose of the Adapter.
The Adapter is the component that feeds the data into your ListView.
The SwipeActionAdapter is meant to wrap around your existing implementation of Adapter and provide callbacks that you can use to perform actions when someone swipes an item in your ListView.
You should probably go through a bit more tutorials. I can greatly recommend the Busy Coders guide to Android development by Commonsware.
I'm certain you'll find what you need there.
Related
I am trying to make a spinner that will display a different text view each time an item from the list has been selected. When I run my code, I am able to switch between the different items, but the text is not updating based on the selection. I have looked at a variety of similar questions but none of their solutions have done what I am looking for.
Here is my code from the main activity:
Spinner spinner;
TextView example;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
example = (TextView) findViewById(R.id.example);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this, R.array.medication_array,android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{
case 0:
example.setText("Depression");
break;
case 1:
example.setText(R.string.ssriexample);
break;
case 2:
example.setText(R.string.snriexample);
break;
case 3:
example.setText(R.string.tcaexample);
break;
case 4:
example.setText(R.string.moiexample);
break;
case 5:
example.setText(R.string.otherexample);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Any tips would be apprieated as this is my first time coding in android studios and it's taking a little bit of time to get used to.
From the way your code is posted, it looks like your listeners are not attached to the spinner. Call spinner.setOnItemSelectedListener(new listener) or spinner.setOnItemSelectedListener(this)
I have just implemented the "android.support.v7.widget.SearchView" in my Actionbar to search for some pdf files.
When i insert multiple characters for search, the list of suggestions is shown perferctly. But when i'm only entering one single character, there are no proposals shown, although the function to populate the suggestions is called.
Do you have an idea how to change that behavior, to get the proposals shown when entering only one character?
This is where i simply define the searchview inside a menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/pdf_menu_search_item"
android:icon="#mipmap/ic_search_white_24dp"
android:title="Suche"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/></menu>
In my Activity, the following code is implemented:
private static final String[] SUGGESTIONS = {
"Amsterdam","Berlin","Rome"
};
private SimpleCursorAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_contextual_actionbar, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
final SearchView mSearchView;
mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.pdf_menu_search_item));
final String[] from = new String[] {"cityName"};
final int[] to = new int[] {android.R.id.text1};
mAdapter = new SimpleCursorAdapter(Startseite.this,
android.R.layout.simple_list_item_1,
null,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
mSearchView.setSuggestionsAdapter(mAdapter);
mSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
#Override
public boolean onSuggestionClick(int position) {
return true;
}
#Override
public boolean onSuggestionSelect(int position) {
return true;
}
});
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
populateAdapter(newText);
return true;
}
});
return super.onPrepareOptionsMenu(menu);
}
// You must implements your logic to get data using OrmLite
private void populateAdapter(String query) {
final MatrixCursor c = new MatrixCursor(new String[]{ BaseColumns._ID, "cityName" });
for (int i=0; i<SUGGESTIONS.length; i++) {
if (SUGGESTIONS[i].toLowerCase().startsWith(query.toLowerCase()))
{ c.addRow(new Object[] {i, SUGGESTIONS[i]}); }
}
mAdapter.changeCursor(c);
mAdapter.notifyDataSetChanged();
}
Working with more than one characters:
Not working with only one character:
Please has anyone an idea how to fix that?
I have just found the answer.
If you are using AppCompatActivity (like me), you can set the searchview threshold like that:
AutoCompleteTextView searchAutoCompleteTextView = (AutoCompleteTextView) mSearchView.findViewById(getResources().getIdentifier("search_src_text", "id", getPackageName()));
searchAutoCompleteTextView.setThreshold(1);
Now the search-suggestions will be shown from the first character on.
Maybe it is helpful for others :)
Hi everyone I am currently developing an android app, I am having some confusions in the code, I have a main activity in which I store some array values.
I have two xml files(one mainactivity.xml and other the other one is listview.xml)
In main activity.xml there are four switches, when I click any particular switch it should take me to the listview.xml, with a corresponding array displayed on list view by list view adapter. The code is as follows
public class MainActivity extends ActionBarActivity {
ListView l;
Button chem = (Button) findViewById(R.id.button4);
public String[] contentc = {
"Abundance",
"Anxiety",
"Bruxism",
"Discipline",
"Drug Addiction"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l= (ListView) findViewById(R.id.listview);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_element, contentp);
chem.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//DO SOMETHING! {RUN SOME FUNCTION ... DO CHECKS... ETC}
setContentView(R.layout.list);
l.setAdapter(adapter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);
In activity1, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message")
I have an activity which has an edit text which becomes visible when a button is clicked. I fill the edit text up and click another button. On clicking this button the edit text content must be sent to another activity.The first activity takes the edit text and queries a list of data from my Parse database and shows it in a ListView in the Second Activity.But whenever i click the first button(after entering the string) the app crashes.This is the first activity
public class MainActivity extends ActionBarActivity {
String name;
EditText search;
Button g;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpSpinners();
Parse.initialize(this, "AAh5US7zhbYyFBexsv07cjo34ZZiB7KNe9SuTv7e",
"eKUG1pYaV50hVyDC9d4qZc4qf1dCtOTqnX92eGJV");
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation();
search = (EditText) findViewById(R.id.search);
g = (Button) findViewById(R.id.Go);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void byName(View v) {
search.setVisibility(View.VISIBLE);
search.requestFocus();
g.setVisibility(View.VISIBLE);
}
public void Go(View v) {
name = search.getText().toString();
final Intent i;
i = new Intent(MainActivity.this, ResterauntList1.class);
i.putExtra("restrauntName", name);
startActivity(i);
}
}
In the above byName is the onClick for making the EditText visible, and Go is the onClick for getting my EditText string and passing it to the next activity. The second activity is below
public class ResterauntList1 extends Activity {
String rValue;
ArrayAdapter<String> adapter;
ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resteraunt_list1);
Bundle bdl = getIntent().getExtras();
rValue = bdl.getString("restrauntName");
setContentView(R.layout.activity_resteraunt_list);
populateList(rValue, "name");
}
private void populateList(final String Value, final String Key) {
ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {
#Override
#SuppressWarnings({ "unchecked", "rawtypes" })
public ParseQuery create() {
ParseQuery query = new ParseQuery("resdb");
query.whereEqualTo(Key, Value);
return query;
}
};
ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(
this, factory);
adapter.setTextKey("name");
adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
#Override
public void onLoading() {
mProgressDialog = new ProgressDialog(ResterauntList1.this);
mProgressDialog.setTitle("Searching for " + Value);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
public void onLoaded(List<ParseObject> objects, Exception e) {
mProgressDialog.dismiss();
}
});
final ListView listView = (ListView) findViewById(R.id.restListView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ParseObject object = (ParseObject) listView
.getItemAtPosition(position);
String Id = object.getObjectId();
Intent i = new Intent(getApplicationContext(),
SingleRestraunt.class);
i.putExtra("restId", Id);
startActivity(i);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.resteraunt_list1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The error as stated above occurs when I click the Go button.The error is
09-02 14:58:46.443: E/AndroidRuntime(3061): Process: com.example.gastronomaapp, PID: 3061
09-02 14:58:46.443: E/AndroidRuntime(3061): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gastronomaapp/com.example.gastronomaapp.ResterauntList1}: java.lang.NullPointerException
Any idea where I am making a mistake? The funniest thing almost the same code has worked in another part of my app. absolutely clueless whats wrong.
Bundle bdl = getIntent().getExtras();
rValue = bdl.getString("restrauntName");
change to
rValue = getIntent().getStringExtra("restrauntName");
You put the string directly on the intent, not packaged in a bundle.
I'm trying to change the layout of my activity according to my selection inside the spinner.
But after the first selection, the spinner become white and I'm not able to decide another selection.
The code I'm using is the following:
public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner spinner;
String[] options = { "Modulo1", "Modulo2" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, options);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter_state);
spinner.setOnItemSelectedListener(this);
}
int check = 0;
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
check = check + 1;
if (check > 1) {
int selState = spinner.getSelectedItemPosition();
switch (selState) {
case 0:
setContentView(R.layout.activity_main);
break;
case 1:
setContentView(R.layout.activity2_main);
break;
}
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
//
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Any suggestion?
Thanks
The spinner you are referring to is in your first activity. After you have replaced the content, the spinner isn't there anymore. If you have another spinner in the second layout, you have to reconnect it and set the listener again.
Basically you have to run your onCreate stuff after every setContentView...
As a side note, whatever you are trying to do, this is probably not the way to go. To show another full layout, better use another activity.