i am new in Android development.
Im trying to transfer selected data from multiple spinners (Search activity) to another activity (JSON Search result activity)
and at the end i have button that open the search result
Search activity:
i have the java spinner
ArrayAdapter<CharSequence> whatlist = ArrayAdapter.createFromResource(this,
R.array.whatlist, android.R.layout.simple_spinner_item);
whatlist.setDropDownViewResource(R.layout.spinner_style);
spwhat = (Spinner) findViewById(R.id.spWhat);
spwhat.setAdapter(whatlist);
spwhat.setOnItemSelectedListener(new MyOnItemSelectedListener());
and the MyOnItemSelectedListener
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strs = new Bundle();
Intent i = new Intent(SearchActivity.this, SearchResult.class);
strs.putString("setwhat", parent.getItemAtPosition(pos).toString());
i.putExtras(strs);
}
public void onNothingSelected(AdapterView<?> arg0) {}
}
This is the button
btnsearch = (Button)findViewById(R.id.btnSearch);
btnsearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent ia = new Intent(SearchActivity.this, SearchResult.class);
SearchActivity.this.startActivity(ia);
}
});
This is in the search result
Bundle extras = getIntent().getExtras();
if(extras!=null){
Integer item = extras.getInt("setwhat");
//Use a switch(item) here to switch to different links based on selection
TextView tv = (TextView) findViewById(R.id.tvtv);
tv.setText("Another Activity, Item is :" + item.toString());
the text wont change.
i have tried any tutorial on the web and searching here for a solution for hours..
anyone can help?
You don't even need a listener for your spinner. Just change your button's onclick to this:
btnsearch = (Button)findViewById(R.id.btnSearch);
btnsearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Spinner spwhat = (Spinner) findViewById(R.id.spWhat);
Intent ia = new Intent(SearchActivity.this, SearchResult.class);
ia.putExtra("setwhat", spwhat.getSelectedItem().toString());
startActivity(ia);
}
});
Related
Been trying to add a favorites system to this notes app where I can tap and hold an item in the list view to add it to another activity with a list view. Here is the activity with the first list.
Items are added via the MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.savebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText editTextHeading = (EditText) findViewById(R.id.editTextTextPersonName);
EditText editTextContent = (EditText) findViewById(R.id.contentfield);
String heading = editTextHeading.getText().toString().trim();
String content = editTextContent.getText().toString().trim();
if (!heading.isEmpty()) {
if(!content.isEmpty()) {
try {
FileOutputStream fileOutputStream = openFileOutput(heading + ".txt", Context.MODE_PRIVATE); //heading will be the filename
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else {
editTextContent.setError("Content can't be empty!");
}
}else{
editTextHeading.setError("Heading can't be empty!");
}
editTextContent.setText("");
editTextHeading.setText("");
}
});
Button button2 = (Button) findViewById(R.id.btn_gotosaved);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, saved.class));
}
});
Button button3 = (Button) findViewById(R.id.btn_faves);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, favorites.class));
}
});
}
}
Items added will be viewed here
public class saved extends MainActivity {
public static final String EXTRA_MESSAGE = "com.example.notes.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saved);
File files = getFilesDir();
String[] array = files.list();
ArrayList<String> arrayList = new ArrayList<>();
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
for (String filename : array) {
filename = filename.replace(".txt", "");
System.out.println(filename);
adapter.add(filename);
}
final ListView listView = (ListView) findViewById(R.id.lv_saved);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = listView.getItemAtPosition(position).toString();
Intent intent = new Intent(getApplicationContext(), Note.class);
intent.putExtra(EXTRA_MESSAGE, item);
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
String item = listView.getItemAtPosition(position).toString();
}
});
}
}
And tapping and holding an item from there should "favorite" it and copy it to this new activity with another listview
public class favorites extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorites);
ListView listView = (ListView) findViewById(R.id.lv_favorites);
}
}
How should I approach this?
With your implementation of creating an individual .txt file in the default directory for each note, this is how you could implement:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
String item = listView.getItemAtPosition(position).toString();
Boolean isItemFavorite = item.contains("_favorite");
if (!isItemFavorite){
File itemFile = new File(item + ".txt");
File favoriteItemFile = new File(item + "_favorite.txt");
itemFile.renameTo(favoriteItemFile);
}
}
});
Then in your "favorites" activity you could access all of your note .txt file the same as you do in your "saved" activity - just filtering out any items that don't contain "_favorite" in your "String[] array = files.list();"
Also, some tips: follow naming convention with your activities. "saved" should at least start with an uppercase letter and really should be named something like "SavedNotesListActivity". Also, you should use a room database to keep track of your notes. You should have a favorites table in your room database to keep track of all of your favorites.
As I am very new to java pls help me on this. I have a custom list view in my main activity and a Custom adapter with it. In my every list item there is a delete button that should delete that item when it clicked. I can not remove data from my arraylist when i am inside my custom adapter. Pls helm me in coding this delete button.
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText getItem;
Button AddButton;
Button DellButton;
public static ArrayList<String> myData = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView)
findViewById(R.id.listView);
getItem = (EditText) findViewById(R.id.newItem);
AddButton = (Button) findViewById(R.id.AddButton);
MyAdapter adapter = new MyAdapter(this, myData);
list.setAdapter(adapter);
AddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String result = getItem.getText().toString();
myData.add(result);
adapter.notifyDataSetChanged();
}
});
}
MyAdapter.java
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, ArrayList<String> records) {
super(context, 0, records);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String item = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_custom, parent, false);
}
final TextView lst_txt = (TextView) convertView.findViewById(R.id.list_Txt2);
Button plusbut = (Button) convertView.findViewById(R.id.plusbut);
Button minusbut = (Button) convertView.findViewById(R.id.minusbut);
final TextView sum = (TextView) convertView.findViewById(R.id.sum);
Button cal = (Button) convertView.findViewById(R.id.calButton);
Button delete = (Button) convertView.findViewById(R.id.btnDel);
lst_txt.setText(item);
minusbut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int sumAll = Integer.parseInt(sum.getText().toString());
int sum1 = sumAll - 1;
sum.setText(String.valueOf(sum1));
}
});
plusbut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int sumAll = Integer.parseInt(sum.getText().toString());
int sum1 = sumAll + 1;
sum.setText(String.valueOf(sum1));
}
});
cal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s = sum.getText().toString();
Intent intent = new Intent(getContext(), calll.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("sumFL", s);
getContext().startActivity(intent);
}
});
return convertView;
}
}
Please first try to remove object from list by using the position of item with check the validation with list size and then call notifyItemadapter to update the list view.
Use ViewHolder class for all view like textview, button etc. And initialize them inside the condition
if(convert view==null){
Initialize holder object here and
Inflate your layout and
Initialize button like
holder.deletebutton = convert view.findviewbyid from xml
settag(holder)
}
Again get the holdet using the gettag in
else{
//Here
}
Put All click event and text update etc. Outside of above condition
holder.deletbutton.setonclicklistener{
int pos = view.getag
list.remove(pos)
Notifyadapter here
}
holder.deletebutton.settag(position)
I am trying to pass some data from one activity to another and I have made a toast in the other activity to see if data is being passed.When the toast shows up it is blank.I have done everything right and tried many different times with different methods I have also created another app but the problem still stays.It gives me null.
My java code in the first activity
public class titleandcuisine extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static final String TITLE_GET = "title";
public static final String CUISINE_GET = "cuisine";
ImageView imageView;
Button button;
Spinner spinner;
EditText dish;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titleandcuisine);
dish = findViewById(R.id.editText);
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.cuisines,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
button = findViewById(R.id.next);
imageView = findViewById(R.id.close);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), homepage.class));
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = dish.getText().toString();
String text2 = spinner.getSelectedItem().toString();
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(TITLE_GET,text);
data.putExtra(CUISINE_GET,text2);
startActivity(data);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Second activity java code
public class reviewactivity extends AppCompatActivity {
TextView cuisine,title;
ImageView displayimage,back;
Uri myUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviewactivity);
Intent intent = getIntent();
String titleget = intent.getStringExtra(titleandcuisine.TITLE_GET);
String cuisineget = intent.getStringExtra(titleandcuisine.CUISINE_GET);
Toast.makeText(this, titleget + cuisineget, Toast.LENGTH_SHORT).show();
cuisine = findViewById(R.id.reviewcuisine);
title = findViewById(R.id.reviewtitle);
back = findViewById(R.id.backtopostvideo);
displayimage = findViewById(R.id.reviewdisplaimage);
// cuisine.setText(cuisineget);
// title.setText(titleget);
// myUri = Uri.parse(uri);
displayimage.setImageURI(myUri);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),videopost.class));
}
});
}
}
You need to read the text from the EditText when the button is clicked. Something like:
title = dish.getText().toString(); // Add this
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(Intent.EXTRA_TEXT, title);
...
One more thing: You are trying to read the Intent.EXTRA_TEXT at the reviewactivity activity. However, nothing is starting that activity (at least within the piece of code that you shared).
I have 4 activity with spinner inside every activity and this spinner include 3
string data (drop-down selection), when I pass from activity to another one I must pass this selected data inside spinner like if I have chosen data x from the list in the spinner and click in button the selected data must be in second activity spinner as x too.
I read several solutions without any solving. I hope to solve it here and this is my code for spinner and where to put the intent code
public class Page1 extends AppCompatActivity {
Spinner spinner;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this,R.array.film_type,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
switch (position)
{case 0:
btn[0] = (FloatingTextButton) findViewById(R.id.btn);
btn[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
double thick = Double.valueOf(editText1.getText().toString());
double width = Double.valueOf(editText2.getText().toString());
}
});
break;
case 1:
btn[0] = (FloatingTextButton) findViewById(R.id.btn);
btn[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
} catch (NumberFormatException e) {
//not a double
}
}
});
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
First save the position of selected data from spinner in a String variable,
int positionOfSelectedDataFromSpinner;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
positionOfSelectedDataFromSpinner= position;
}
Then on button click send intent to Another activity with putExtra
Intent i = new Intent (this, activity2.class);
i.putExtra("position", positionOfSelectedDataFromSpinner);
startActivity(i);
get int from getIntent in another activity
Intent intent = getIntent();
int positionToShowToSpinner = intent.getStringExtra("position");
then set the position to spinner
spinner.setSelection(positionToShowToSpinner);
I think this my solve your problem.
this is the most painless way i can think of:
make a new class or a static member of an existing class but the second solution makes the code less understandable.
public class SpinnerPosHolder{
public static int poition;
}
then in all 4 of them:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
SpinnerPosHolder.position = spinner.getSelectedItemPosition();
For using it:
spinner.setSelection(SpinnerPosHolder.position)
You can do it in this way
String selectedItem=spinner1.getSelectedItem().toString();
And that String you can pass using Intent
Hello to the community,
got a problem with the implementation of my project, which I hope someone can give me because some clarity.
Short description of my project:
I want to change from the Activity_A means of a button onclick events the default position of a spinner in the Activity_B. In my sample code I've tried using an Intent, Value Activity_A to the spinner in the Activity_B in an if - to give statement which then starts the spinner in the corresponding position.
Here is my code:
Activity_A:
public class Start extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
Button button = (Button) findViewById(R.id.button_1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Start.this, Spinnerwert.class);
intent.putExtra("position", "3");
startActivity(intent);
}
});
}
}
Activity_B:
public class Spinnerwert extends Activity {
private TextView beschreibung_1;
private TextView beschreibung_2;
private Spinner s1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_wert);
// TextView issue of Spinner
beschreibung_1 =(TextView)findViewById(R.id.tV_spinner_1);
// TextView output Intent worth
beschreibung_2 =(TextView)findViewById(R.id.tV_spinner_2);
// Intent data receiving
Intent i = getIntent();
String selected = i.getStringExtra("position");
beschreibung_2.setText(selected);
s1 = (Spinner) findViewById(R.id.spinner_1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.auswahl, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Spinner position - Response to Intent worth handover --------
s1.setSelection(position);
if (position == 0) {
s1.setSelection(0);
} else if (position == 1) {
s1.setSelection(1);
} else if (position == 2) {
s1.setSelection(2);
}
beschreibung_1.setText((CharSequence) parent.getItemAtPosition(position));
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}
It seems as though your intent is passing a String extra, and only changing a text view within the second activity. If I am correct at understanding your question, you are wanting to place the spinner in a certain position based on the intent passed from the first activity.
Using the calls similar to those in your IF statement, simply parse the selected String to an int and call s1.setSelection on it.
It should look something like this:
s1 = (Spinner) findViewById(R.id.spinner_1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.auswahl, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setSelection(Integer.parseInt(selection));
I hope this helped!
It looks like you're never setting the spinner to the selected index. Your if statements in onItemSelected aren't doing anything since "position" is a value that gets passed in from the spinner and not the previous intent.
Try putting this
s1.setSelection(Integer.parseInt(selected));
after you set your adapter.
Spinner variant with equals value
public class Spinnerwert_2 extends Activity {
private TextView beschreibung_3;
private TextView beschreibung_4;
private Spinner s2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_wert_2);
beschreibung_3 =(TextView)findViewById(R.id.tV_spinner_3);
beschreibung_4 =(TextView)findViewById(R.id.tV_spinner_4);
Intent intent_spinner_2 = getIntent();
String spinner_2_auswahl = intent_spinner_2.getStringExtra("position_spinner_2");
beschreibung_4.setText(spinner_2_auswahl);
// #2 Spinner + Funktion(Auswahl aus Resource values/array.xml)
s2 = (Spinner) findViewById(R.id.spinner_2);
ArrayAdapter<CharSequence> adapter_2 = ArrayAdapter.createFromResource(this, R.array.auswahl, android.R.layout.simple_spinner_item);
adapter_2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter_2);
// #2 SPINNER-ITEM POSITIONSEINSTELLUNG / ZUWEISUNG
s2.setSelection(adapter_2.getPosition(spinner_2_auswahl));
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (s2.getSelectedItem().toString().equals("Mercury")){
Toast.makeText(getApplicationContext(), "Auswahl Mercury", Toast.LENGTH_SHORT).show();
} else if (s2.getSelectedItem().toString().equals("Venus")){
Toast.makeText(getApplicationContext(), "Auswahl Venus", Toast.LENGTH_SHORT).show();
} else if (s2.getSelectedItem().toString().equals("Earth")){
Toast.makeText(getApplicationContext(), "Auswahl Earth", Toast.LENGTH_SHORT).show();
}
beschreibung_3.setText((CharSequence) parent.getItemAtPosition(position));
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}