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
Related
i am runing the Code below but not happens. How can i doit right?
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
List<String> memorablePlaces;
ListView listView;
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.memorablePlaces);
memorablePlaces = new ArrayList<>();
memorablePlaces.add("Add a place");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1);
listView.setAdapter(arrayAdapter);
}
Pass your list to adapter:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, memorablePlaces);
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 am unable to get my ArrayList out when it's on a different page. I would like to get the ArrayList out Android Studio.
This is CartActivity class:
public class CartActivity extends AppCompatActivity {
private ArrayList<CartItem> cartList;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
listView = (ListView) findViewById(R.id.listView);
CartAdapter adapter = null;
cartList = new ArrayList<>();
adapter = new CartAdapter(this, R.layout.adapter_cart_item, cartList);
listView.setAdapter(adapter);
}
}
This is ProductActivity class. It's where the data is being stored into the ArrayList:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product);
myDB = new DatabaseHelper(ProductActivity.this);
textViewPrice = (TextView) findViewById(R.id.textViewPrice);
textViewInfo = (TextView) findViewById(R.id.textViewInfo);
textViewName = (TextView) findViewById(R.id.textViewName);
ivProduct = (ImageView) findViewById(R.id.ivProduct);
btnAddToCart = (Button) findViewById(R.id.btnAddToCart);
spinnerQuantity = (Spinner) findViewById(R.id.spinnerQuantity);
Intent in = getIntent();
Bundle b = in.getExtras();
userID = b.getString("userID");
productID = b.getInt("productID");
Cursor results = myDB.checkProduct();
if (results.getCount() == 0) {
Toast.makeText(getApplicationContext(), "Error: no data found!",
Toast.LENGTH_SHORT).show();
return;
} else {
StringBuffer buffer = new StringBuffer();
while (results.moveToNext()) {
id = results.getInt(0);
name = results.getString(1);
price = results.getString(2);
info = results.getString(4);
quantity = Integer.parseInt(results.getString(5));
image = results.getBlob(6);
if (id == productID) {
textViewName.setText(name);
textViewInfo.setText(info);
textViewPrice.setText("S$" + price);
int[] quantityValues = new int[quantity + 1];
int counter = 0;
for (int i = 0; i < quantityValues.length; i++) {
quantityValues[i] = counter;
counter++;
quantityList.add(Integer.toString(quantityValues[i]));
}
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
ivProduct.setImageBitmap(bitmap);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, quantityList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerQuantity.setAdapter(adapter);
}
}
}
}
public void addCart(View v) {
cartList.add(new CartItem(name, price,spinnerQuantity.getSelectedItem().toString(), image, id));
Intent productListIntent = new Intent(this, CartActivity.class);
startActivity(productListIntent);
}
You should impliment Serializable in CartItem class and then pass from ProductActivity to CartActivity . Like this :
ProductActivity class.
Intent productListIntent= new Intent(this, CartActivity .class);
productListIntent.putExtra("cartlist", ArrayList<CartItem>mcartItem);
startActivity(productListIntent);
and in CartActivity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
ArrayList<CartItem> mycart= new ArrayList<CartItem>();
mycart= (ArrayList<CartItem>)getIntent().getSerializableExtra("cartlist");
listView = (ListView) findViewById(R.id.listView);
CartAdapter adapter = null;
adapter = new CartAdapter(this, R.layout.adapter_cart_item, mycart);
listView.setAdapter(adapter);
}
It's not the best practice, but you can make your arrayList
public static ArrayList list;
Then you can access it from ather activity using {class_name}.list
You have to pass your list of your data via intent(as Intent is a message passing Object) From ! activity to another activity. For that you have to make that class Parcable or Serializable . How to make a class Parcable in andriod studio read this answer. Then put thah class into the intent that you are passing to startActivity() method like
intent.putParcelableArrayListExtra("key",your_list);
or
intent.putExtra("key",your_class)// incase of single object
then get it to the onCreate() method of second activity like
getIntent().getParcelableArrayListExtra("your_key")
or
getIntent().getParcelableExtra()// incase of single object
Hope it will help your
I have a listview and I want to add a search in listview using the algorithm KMP
How to Implement on my listView.
#Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView =(ListView) findViewById(R.id.list_istilah);
autoTv = (AutoCompleteTextView) findViewById(R.id.AutoTV);
db = DatabaseHelper.getInstance(this);
setData();
autoTv.addTextChangedListener(this);
listView.setOnItemClickListener(this);
}
private void setData()
{
listKamus = db.getAllKamus();
adapter = new ArrayAdapter<Kamus>(this,
android.R.layout.simple_expandable_list_item_1, listKamus);
listView.setAdapter(adapter);
}
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.