I have a button, an editText, and a listView on my project. The function I want to realize is that as I enter some words in editText and click the button, the words I typed need to be in the listView.
Here is my code. After debugging, it shows error: incompatible types: String cannot be converted to String[]
public class MainActivity extends AppCompatActivity {
private String[] str = new String[10];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view); //build listview
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, str);
listView.setAdapter(adapter);
}
public void sendMessage(View view){
EditText editText = (EditText) findViewById(R.id.editText);
str = editText.getText().toString();
}
}
Below is working code for what you want.
step 1 -> Add data in edit text
step 2 -> click on submit button
step 3 -> Added data will display in listview
public class MainActivity extends AppCompatActivity {
private List<String> str = new ArrayList<>();
private ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.list_view); //build listview
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, str);
listView.setAdapter(adapter);
}
public void sendMessage(View view) {
EditText editText = findViewById(R.id.editText);
str.add(editText.getText().toString());
adapter.notifyDataSetChanged();
editText.setText("");
}
}
Try it this way
public class MainActivity extends AppCompatActivity {
private List<String> str = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view); //build listview
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, str);
listView.setAdapter(adapter);
}
public void sendMessage(View view){
EditText editText = (EditText) findViewById(R.id.editText);
String str1 = editText.getText().toString();
str.add(str1);
}
}
error error: incompatible types: String cannot be converted to String[]
is because of str = editText.getText().toString();
here you are trying to assign string to array of string incorrectly
what you have to do is use ArrayList insted of String[] as it is dynamic and efficient than String[]
and
insted of str = editText.getText().toString(); do
arrayList.add(ditText.getText().toString());
In your case you are using String[] array with size 10.
now if you want to add string from EditText in ListView then you have to add like below.
public class MainActivity extends AppCompatActivity {
private String[] str = new String[10];
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view); //build listview
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, str);
listView.setAdapter(adapter);
}
public void sendMessage(View view){
EditText editText = (EditText) findViewById(R.id.editText);
if(count < str.length){
str[count] = editText.getText().toString();
count++;
}
}
}
Why you geeting error?
You get the error because you set the String array as a string. But if you want to add string in string array then you have to specify a position for array.
Related
I have three spinners in my activity. So I have to open specific activity based on the input of three spinners; if I have entered "1" in my first spinner "cse" in my second spinner and "b" in my third spinner I have to open the respected activity and so on for different combinations.
I have tried using if statements and the app is crashing every time since.
Please beware of the comments in the code those are the codes which I have tried and didn't get any result.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button btn;
/*TextView selection1;
TextView selection2;
TextView selection3;*/
Spinner spin1;
Spinner spin2;
Spinner spin3;
String[] years = {"1","2","3","4"};
String[] branches = {"CSE","ECE","EEE","CIVIL","IT","MECH"};
String[] sections = {"A","B","C"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin1 = (Spinner) findViewById(R.id.spinner1);
spin1.setOnItemSelectedListener(this);
/*List<String> branches = new ArrayList<String>();
branches.add("CSE");
branches.add("ECE");
branches.add("IT");
branches.add("MECH");
branches.add("CIVIL");
branches.add("EEE");*/
ArrayAdapter<String> dataAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,years);
//Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//attaching dataAdapter to spinner
spin1.setAdapter(dataAdapter);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
spin2.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,branches);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(dataAdapter2);
Spinner spin3 = (Spinner) findViewById(R.id.spinner3);
spin3.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sections);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin3.setAdapter(dataAdapter3);
Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck(){
/*Spinner s1 = (Spinner)findViewById(R.id.spinner1);
Spinner s2 = (Spinner)findViewById(R.id.spinner2);
Spinner s3 = (Spinner)findViewById(R.id.spinner3);
String sp1 = s1.getPrompt().toString();
String sp2 = s2.getPrompt().toString();
String sp3 = s3.getPrompt().toString();
if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){*/
Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show();
Intent it = new Intent(this,M2.class);
startActivity(it);
//}
/*else{
Toast.makeText(this, "Invalid input", Toast.LENGTH_LONG).show();
}*/
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//on selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
/*if (spin1.getSelectedItem().toString().equals("3")) {
if (spin2.getSelectedItem().toString().equals("CSE")) {
if (spin3.getSelectedItem().toString().equals("B")) {*/
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
/*Intent it = new Intent(this, M2.class);
startActivity(it);*/
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
/*Button btn =(Button)findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
displaycheck();
}
}
);
}
public void displaycheck(){
Spinner s1 =(Spinner) findViewById(R.id.spinner1);
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
Spinner s3 = (Spinner) findViewById(R.id.spinner3);
String ye = y.getText().toString();
String br = b.getText().toString();
String se = s.getText().toString();
if (ye.equals("3")&&br.equals("cse")&&se.equals("b")) {
Toast.makeText(this, "ra ra ", Toast.LENGTH_LONG).show();
Intent it = new Intent(this, M2.class);
startActivity(it);
}
else{
Toast.makeText(this, "po po", Toast.LENGTH_LONG).show();
}
}*/
}
You are not giving value to variable which are global - Activity Scope - spin1, spin2, spin3.
Instead your are giving value to onCreate or Method specific scope; when you're fetching value from it it's never initialized.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button btn;
Spinner spin1;
Spinner spin2;
Spinner spin3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get spinner in variable
spin1 = (Spinner) findViewById(R.id.spinner1);
...
// Your adapter, ItemListener, DropDownViewResource
spin2 = (Spinner) findViewById(R.id.spinner2);
... // Your adapter, ItemListener, DropDownViewResource
spin3 = (Spinner) findViewById(R.id.spinner3);
... // Your adapter, ItemListener, DropDownViewResource
Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck(){
String sp1 = spin1.getSelectedItem().toString();
String sp2 = spin2.getSelectedItem().toString();
String sp3 = spin3.getSelectedItem().toString();
// Your combinations check
if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){
// based on combination - redirect user
}
}
}
To get selected item from spinner you use this
String value1 = s1.getSelectedItem().toString();
String value2 = s2.getSelectedItem().toString();
String value3 = s3.getSelectedItem().toString();
Now you can check the values with any logic for example:
if(value1.equals("a") && value2.equals("cse") && value3.equals("b"))
openActivityCodeHere();
I am trying to bring all the items from the ArrayList getting saved in EditActivity to the MainActivity using SharedPreferences. But only the last entered data gets displayed in the ListView on the first MainActivity. How can I make all the text entered into the EditText get displayed as a different item in the ListView and not replace the earlier item.
Here is my Code:
MainActivity
public class MainActivity extends AppCompatActivity {
private ListView list;
private Button nextButton;
ArrayList<String> arrayList = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
nextButton = (Button) findViewById(R.id.nextButton);
loadData();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
list.setAdapter(adapter);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextActivity();
}
});
}
public void nextActivity(){
Intent intent = new Intent(this, EditActivity.class);
startActivity(intent);
}
public void loadData(){
SharedPreferences sp = getSharedPreferences("Data", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sp.getString("list", null);
Type type = new TypeToken<ArrayList<String>>() {}.getType();
arrayList = gson.fromJson(json, type);
if(arrayList == null){
arrayList = new ArrayList<String>();
}
}}
EditActivity
public class EditActivity extends AppCompatActivity {
private EditText input;
private Button addButton;
public ArrayList<String> list = new ArrayList<String>();
public ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
input = (EditText) findViewById(R.id.input);
addButton = (Button) findViewById(R.id.addButton);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getInput = input.getText().toString();
adapter.addAll(getInput);
saveData();
input.setText(""); //clear the value in the edit text
}
});
}
public void saveData(){
SharedPreferences sp = getSharedPreferences("Data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
editor.putString("list", json);
editor.apply();
}}
I think you forget to add item to list before save and thus the result is you are saving only the last item.
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getInput = input.getText().toString();
list.add(getInput); //add this line
adapter.addAll(getInput);
saveData();
input.setText(""); //clear the value in the edit text
}
});
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
EditText myeditText1;
EditText myeditText2;
Button myButton;
Double myfirstET1;
Double myfirstET2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
myeditText1 = (EditText) findViewById(R.id.editText1);
myeditText2 = (EditText) findViewById(R.id.editText2);
myButton = (Button) findViewById(R.id.buttonID);
final TextView myTextView = (TextView) findViewById(R.id.textView2);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myfirstET1 = Double.parseDouble(myeditText1.getText().toString());
myfirstET2 = Double.parseDouble(myeditText2.getText().toString());
final ListView mListView = (ListView) findViewById(R.id.mList);
if ((myeditText1.getText().length() > 0) && (myeditText2.getText().length() > 0)) {
double oper1 = Double.parseDouble(myeditText1.getText().toString());
double oper2 = Double.parseDouble(myeditText2.getText().toString());
double result = oper1 / oper2;
myTextView.setText(Double.toString(result));
Double myyList[] = {oper1, oper2 + 1, oper2 + 1};
ListAdapter myAdapter = new ArrayAdapter<Double>(this, android.R.layout.simple_list_item_1, myyList);
mListView.setAdapter(myAdapter);
}
}
});
Upon creating the ListView and setting adapter to it, I'm getting error in ListAdapter.. How do I fix it OR whats the easy alternative for it?
I hope my moto is clear with the code I've written.
All suggestions are heartly appreciated.
You need to use MainActivity.this instead of this in anonymous class to refer to this actual Activity context as
ListAdapter myAdapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, myyList);
// ^^^^^^^^^^^^^^^^^^
and also mention the type and use ArrayAdapter not ListAdapter
ArrayAdapter<Double> myAdapter = new ArrayAdapter<>(MainActivity.this...);
// ^^^^^^^
you should use something like this
ArrayAdapter<Double> myAdapter = new ArrayAdapter<Double>(this, android.R.layout.simple_list_item_1, myyList);
You can either use
ListAdapter myAdapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, myyList);
mListView.setAdapter(myAdapter);
or
ListAdapter myAdapter = new ArrayAdapter<Double>(MainActivity.this,
android.R.layout.simple_list_item_1, myyList);
mListView.setAdapter(myAdapter);
But make sure instead of this use MainActivity.this
[Here is a screenshot of my output][1]
I am trying to sort the elements of an array.
I have stored the elements to the array from EditText1(ed) and I want to sort them and display them in EditText2.
I am done with storing them and displaying them, i wanted to sort them using Collections.sort(array); but it shows me that there is something wrong.
This is my code so far:
public class MainActivity extends AppCompatActivity {
List<EditText> allEds = new ArrayList<EditText>();
EditText ed,ed2;
RelativeLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed= (EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
final Button b=(Button)findViewById(R.id.button);
container = (RelativeLayout)findViewById(R.id.rl);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
allEds.add(ed);
String[] strings = new String[allEds.size()];
for(int i=0; i < allEds.size(); i++){
strings[i] = allEds.get(i).getText().toString();
Log.e("My data", strings[i]);
ed2.setText(strings[i]);
}
}
});
[1]: http://i.stack.imgur.com/dowdE.jpg
I think this will probably work (replace the code in the onClick with this):
String[] strings = ed.getText().toString().split("\\r?\\n");
Arrays.sort(strings);
String output = TextUtils.join("\\n",strings);
ed2.setText(output);
may be something like this?
public class MainActivity extends AppCompatActivity {
List<String> allEds = new ArrayList<String>();
EditText ed,ed2;
RelativeLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed= (EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
final Button b=(Button)findViewById(R.id.button);
container = (RelativeLayout)findViewById(R.id.rl);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str = ed.getText().toString();
String lines[] = str.split("\\r?\\n");
Arrays.sort(lines);
ed2.setText(TextUtils.join("\n", lines));
}
});
this is my code:
public class EnterActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.enter);
Button search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] answers = {
"das",
"das",
"dsa"
};
EditText eproduct = (EditText) findViewById(R.id.product);
EditText efactory = (EditText) findViewById(R.id.factory);
GridView grid = (GridView) findViewById(R.id.grid);
String product = eproduct.getText().toString();
String factory = efactory.getText().toString();
FrenchList french = new FrenchList();
int frenchResult = french.getCosher(product, factory);
//error is here
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, answers);
}
});
}}
the error is: "The constructor ArrayAdapter(new View.OnClickListener(){}, int, String[]) is undefined"
I did some searching on similar problems and I'm preety sure the problem is that i don't use the context properly, but to my understanding, as long as "this" appears in a class that extends activity I should be alright.
Why is this error showing? how can I fix it?
this refers to the View.OnClickListener class, which is an anonymous inner class in this example. You can use EnterActivity.this.