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.
Related
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.
I've been trying to build a page that take the the file name and then input it in a listview and then this problem occurred first I don't know how to build a
layout page and then there is a problem with this line Saved.add(FileName); on add particularly?
I've tried by removing the final infront list view but it ended up with an error on Saved instead
this is the code in question:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ImageButton Add = (ImageButton)findViewById(R.id.imageButton);
final ListView Saved = (ListView)findViewById(R.id.Saved_list);
final ArrayAdapter<String> Arr1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
Saved.setAdapter(Arr1);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Save");
View mView = getLayoutInflater().inflate(R.layout.SaveFile_dialog_layout, null);
final EditText SavedText = (EditText) findViewById(R.id.SavedText);
Button Save = (Button)mView.findViewById(R.id.Save);
Button Later = (Button)mView.findViewById(R.id.Later);
Button Cancel = (Button)mView.findViewById(R.id.Cancel);
builder.setView(mView);
final AlertDialog SavingsDialogue =builder.create();
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!SavedText.getText().toString().isEmpty()){
String FileName = SavedText.getText().toString();
Saved.add(FileName);
SavedText.getText().clear();
Toast.makeText(MainActivity.this,"file name added",Toast.LENGTH_LONG).show();
Intent Transition = new Intent(MainActivity.this,Main2Activity.class);
startActivity(Transition);
// Here I need to add the file organizayion system to start adding the file
}
I have a class which I use a camera to point at an object and get all the strings it can see on the object. I then pass the list into this intent so the user can choose a specific string which I will then use to send to another activity.
However, I want to create a new textview for each string and then put an OnClickListener on each of the textviews, but I am unsure on how I can put an OnClickListener on a textview that I won't know exist as it will be created depending on the list.
public class SelectProductName extends AppCompatActivity {
ArrayList<String> getString = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_product_name);
getString = getIntent().getStringArrayListExtra("stringList");
LinearLayout ll = findViewById(R.id.layout_select_product_name);
for (int i = 0; i < getString.size();i++){
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText(getString.get(i));
System.out.println(getString.get(i));
ll.addView(text);
}
}
}
So for example: getString is {"Chicken","Pie"}
I want a textview to be created for each string.
I'm unsure on how I would get the name of the textview to add the OnClickListener to it.
You can add the listener just like you would do with any view:
LinearLayout ll = findViewById(R.id.layout);
for (int i = 0; i < getString.size();i++){
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText(getString.get(i));
System.out.println(getString.get(i));
ll.addView(text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your code goes here
}
});
}
public class SelectProductName extends AppCompatActivity implements OnClickListener{
ArrayList<String> getString = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_product_name);
getString = getIntent().getStringArrayListExtra("stringList");
LinearLayout ll = findViewById(R.id.layout_select_product_name);
for (int i = 0; i < getString.size();i++){
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText(getString.get(i));
System.out.println(getString.get(i));
ll.addView(text);
text.setOnClickListener(this);
}
}
public void onClick(View view) {
TextView tv = (TextView)view;
Log.i("clicked", tv.getText().toString());
}
}
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
This question already has answers here:
The method getText() is undefined for the type String error
(3 answers)
Closed 8 years ago.
Here is my code. I'm receiving The method getText() is undefined for the type String and cannot solve it by my own.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
EditText ETtitle = (EditText) findViewById(R.id.ETtitle);
EditText ETcontent = (EditText) findViewById(R.id.ETcontent);
button.setOnClickListener(new buttonOnClickListener());
}
public class buttonOnClickListener implements View.OnClickListener {
public void onClick(View v) {
String ETtitle = ETtitle.getText().toString();
}
}
}
you are redefining ETtitle locally
change
String ETtitle = ETtitle.getText().toString();
into
String editTextContent = ETtitle.getText().toString();
change
String ETtitle = ETtitle.getText().toString();
to
String ETtitleStr = ETtitle.getText().toString();
because ETtitle already defined as edittext
and define EditText ETtitle as class member.
i.e. simply rewrite your code as
public class MainActivity extends Activity {
private EditText ETtitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
ETtitle = (EditText) findViewById(R.id.ETtitle);
EditText ETcontent = (EditText) findViewById(R.id.ETcontent);
button.setOnClickListener(new buttonOnClickListener());
}
public class buttonOnClickListener implements View.OnClickListener {
public void onClick(View v) {
String ETtitleStr = ETtitle.getText().toString();
}
}
}