I am curious how I would go about adding an an editText variable to a list in another activity. I have created an arrayList that names are stored into as they are entered in the editText line. These are stored in the nameList variable in the class StartingActivity. How would i go about adding and showing these names into a ListView in another activity and class called ListActivity as shown. Thank you. Also, how do i refractor the names of classes and files without it just basically not making my program runable.
Code:
Class StartingActivity:
public void onClick_btnConfirm {
EditText editName = (EditText) findViewById(R.id.editName);
TextView textOutput = (TextView) findViewById(R.id.textOutput);
Intent intentList = new Intent(StartingActivity.this, ListActivity.class);
if(editName.length()!=0) {
nameList.add(editName.getText().toString());
editName.setText("");
}
//intentList.putExtra("Name", editName.toString());
startActivity(intentList);
}
Class ListActivity:
public class ListActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
ListView listView = (ListView) findViewById(R.id.listAthletes);
final StartingActivity nameList = new StartingActivity();
nameList.getNameList();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, nameList.getNameList());
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
You cannot run findviewbyid() for an id in another Activity.This answer might be of help to you. If that is not what you really want to go with, you can do two things:
Send Intents with the string.
Use a sharedPreference and write the string to a sharedPreference. SharedPreferences also have an OnChangeListener, so you will be able know if the preference has changed. In the onResume() of that activity , you can just read from the sharedPreference, and update the UI
Also take a look at AsyncTask here
As mentioned , you can also use the SQLite Database
Related
public class Main2Activity extends AppCompatActivity {
ListView simpleList;
String[] thoughtList = {" "};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String newThought = intent.getStringExtra(MainActivity.EXTRA_TEXT);
thoughtList.add(newThought); //error comes herer hich wont allow add
simpleList = (ListView)findViewById(R.id.simpleListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
simpleList.setAdapter(arrayAdapter);
}
}
that is my code on second page it brings over varible (saved as newThought) from the previous activity which i then want to add to thoughtList to display in ListView but won't let me add the new varible to the list. does my code need tweaking or should i start again to achieve varible brougth over from previous activity to add into list view on activity 2
Have you tried using an ArrayList instead of an string array?
String array has a fixed size when array list does not.
That may be your problem.
Hello I'm currently trying to make my first app on android and I've gotten some problems. I'm trying to make a todo-list app so want some kind of input to be transformed into checkboxes. I've made it work with radio buttons using radioGroup. But when using Checkboxes with ListView it just doesn't work.
Here's my code:
public class MainActivity extends AppCompatActivity {
EditText t;
ListView listView;
ArrayList<CheckBox> checkList = new ArrayList<>();
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View view){
t = findViewById(R.id.input);
String s = t.getText().toString();
CheckBox check = new CheckBox(this);
checkList.add(check);
listView = findViewById(R.id.list);
checkList.get(i).setText(s);
listView.addView(checkList.get(i));
i++;
t.setText("");
}}
The app crashes saying something about adapterView
You're unfortunately doing something wrong here. You are trying to add your view to Listviews on
listView.addView(checkList.get(i));
This is not the correct way to use ListViews. You have to use an adapter. The adapter would need to provide data for the listview to load.
Please have a look at this tutorial for how to use listviews.
Below is a summary of the steps to use a listView correctly.
Create a new layout file (say custom_cell.xml) inside your layouts folder.
Insert a checkbox inside your custom_cell.xml and place an Id which you can use later to identify the checkbox
Create an adapter for your listview
Override the getView method
Inside the getView method, inflate a new cell using the custom_cell.xml or reuse an existing cell if provided
Reference the Checkbox by using the Id you provided in the custom_cell.xml file
I replaced an EditText with an AutoCompleteTextView in my app to make things a little more user friendly, but I am having an error.
In the app, the user types in the name of a plant, and then clicks a button to be taken to a new activity where some information about the plant is displayed.
The error: the app crashes after I press the button to be taken to the next activity after the user types in the name of the plant. This didnt happen when I still had an EditText. Perhaps I am using the AutoCompleteTextView wrong?
Heres the relevant code:
public class Main2Activity extends AppCompatActivity {
AutoCompleteTextView edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//this is the AutoComplete
AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);
//this is the list of suggestions for the AutoComplete
String[] items = getResources().getStringArray(R.array.items_array);
java.util.Arrays.sort(items);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
edit.setAdapter(adapter);
//this is the method that is called when the button is pressed
public void find(View view) {
String name = edit.getText().toString();
//basically, whatever is typed into the AutoComplete is turned into a string, and
//if the string matches one of the existing plants, the user is taken to
//the next activity
if(name.equalsIgnoreCase("Sunflower")){
Intent intent = new Intent(this, Sunflower.class);
startActivity(intent);
}
else if(name.equalsIgnoreCase("Cactus")){
Intent intent = new Intent(this, Cactus.class);
startActivity(intent);
}
Can anyone see why this does not work?
Change below:
AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);
To:
edit = (AutoCompleteTextView) findViewById(R.id.et_item);
Your autocomplete textview scope is limited to onCreate() and
edit.getText().toString();
You are trying to get text from it which is not initialized yet. So it will get null pointer exception.
I've created a ListActivity (yeah, the one without an XML layout) which displays the values got from an ArrayList<String>, via an adapter.
The fact is that the last String added to the ArrayList, is the latest to appear on the ListView aswell, instead I want to sort the ListView so that the last item added will be the first one on top in the ListView, how to do it?
Look, this is a snippet of my application, about the onCreate() method of the ListActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
ArrayList<String> history = intent.getStringArrayListExtra("HistoryList");
ListView historyList = getListView();
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, history);
historyList.setAdapter(listAdapter);
}
Nothing complicated. You just needed to reverse the list history.
Collections.reverse(history);
You should read a little about the theory of Java and learn how to look for. Otherwise, nothing good is not over. The code would look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
ArrayList<String> history = intent.getStringArrayListExtra("HistoryList");
Collections.reverse(history);
ListView historyList = getListView();
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, history);
historyList.setAdapter(listAdapter);
}
how to do it?
Re-order history to be in the order that you want, such as via Collections.reverse().
Try to write a Comparator to sort the string.
I have made a simple contact apps and now I wish to add tab view to this app. I am following the tutorial here. Below is part of the source code for my MainActivity.jave:
public class MainActivity extends ListActivity {
private ListView contactListView;
private CursorAdapter contactListViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contactListView = getListView(); // get ListView id
contactListView.setOnItemClickListener(viewContactListListener);
String[] from = new String[] { "familyName" }; // built an String array
// named "from"
int[] to = new int[] { R.id.contactTextView };
contactListViewAdapter = new SimpleCursorAdapter(MainActivity.this,
R.layout.contact_list_item, null, from, to);
setListAdapter(contactListViewAdapter); // set adapter
}
I tried to change "public class MainActivity extends ListActivity" to "public class MainActivity extends TabActivity implements OnTabChangeListener{". However, I get the error "The method getListView() is undefined for the type MainActivity" and other similar errors. I need suggestions on how to fix this. Thanks for any help.
ListActivity provides some help methods to manage ListView so if you want to change your parent activity from ListActivity to TabActivity you need to handle ListActivity behaviour yourself.
Basically you need to get and store somewhere the ListView object. Something like mListView = (ListView) findViewById(android.R.id.list) on your onCreate method and then implement missing method
ListView getListView()
{
return mListView;
}
Also setting your adapter will be a bit different. Instead of calling setListAdapter(contactListViewAdapter); // set adapter there should be contactListView.setAdapter(contactListViewAdapter);