Here is my code, when i use
List<String> array = new ArrayList<String>();
array.add("kk");
array.add("bb");
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
listView1.setAdapter(adapter);
This will work fine in my onCreate method, but the problem is I can't us 'this' in an anonynmous class. replacing 'this' with getApplicationContext() does not work for some reason, either inside or outside the onCreate.
like this
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,array);
Can anyone help me find the issue I have searched a lot but can not find an answer on this anywhere
Thanks
I would pass the this-pointer via parameter in the constructor of the inner class and save it in a inner class field, like this ->
public class outerclass{
#Override
protected void onCreate(Bundle savedInstanceState){
//.. other stuff
innerclass ic = new innerclass(this);
//.. other stuff
}
public class innerclass{
private Context c;
public innerclass(Context c){
this.c = c;
}
}
}
When you use an adapter to add something into a list or an array, you need to alert the list, in this case array, that there was a change. After you call the add function, you need to call adapter.notifyDataSetChanged()
Also, when declaring the adapter, declare it outside of onCreate() then assign your values inside onCreate()
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
See below:
public class MainActivity extends AppCompatActivity {
// List of array strings that will represent list items
public static ArrayList<String> listItems = new ArrayList<String>();
// Array string will handle the information stored in the listview
public static ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lView = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
lView.setAdapter(adapter);
}
}
Then you can call:
listItems.add(<value>);
adapter.notifyDataSetChanged();
Then if need be you also have access to:
listItems.clear(); and adapter.clear();
Hope this helps!
I assume your anonymous class is inside your MainActivity. If this is the case you can simply do:
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, array);
When you use "this" inside your anonymous class, it refers to the anonymous inner class instance and not the instance of the outer class.
Related
This is my method in CheckActivity.java
public void check5(View view) {
lv = (ListView) findViewById(R.id.listView1);
modelItems[5] = new Model("Item", 1);
CustomAdapter adapter = new CustomAdapter(this, modelItems);
lv.setAdapter(adapter);
}
This how I call method in non-activity class
CheckActivity mActivity = new CheckActivity();
mActivity.check5();
But this is the error I got.
I alt-entered and android studio create new method in CheckActivity.java
public void check5()
{
lv = (ListView) findViewById(R.id.listView1);
modelItems[5] = new Model("Item", 1);
CustomAdapter adapter = new CustomAdapter(this, modelItems);
lv.setAdapter(adapter);
}
But my code will not work with (View view) how do I fix that?
Edit - Some told me that this is bad idea to do. So if this is impossible how do I copy my method into non-activity class?
You need to pass Context of CheckActivity Activity in your non Activity Class and then you call Activity method from Non Activity Class by casting Context in Activity name.
((CheckActivity)context).check5(view);
pass view with just like you have called member function , so you can access variables of class CheckActvity declared as public .
Move your adapter initialization inside onCreate. You need to set the adapter to the ListView using setAdapter. Call noftifyDataSetChanged after populating the dataset arr.
Try this code,
private MyAdapter ma;
onCreate(){
...
setContentView(R.layout.activty_main2);
lv = (ListView) findViewById(R.id.lv_1);
ma = new MyAdapter(this, arr);
lv.setAdapter(ma);
initData();
}
initData(){
for(;;){
...
arr.add(n);
}
ma.noftifyDataSetChanged();
}
You not having any blank constructor in MyAdapter,
you have to pass context and ArrayList to the adapter like,
MyAdapter ma = new MyAdapter(Main2Activity.this,arr);
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
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);
I am trying to add few items in the spinner list. But somehow everytime, I always get the below exception on this-
Cannot use this in a static context
Below is my code
public static void initSpinnerView(Context context, Spinner spinnerView, String prefix, int numItems, int layout) {
//spinnerView = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerView.setAdapter(dataAdapter);
}
}
Can anyone tell me what wrong I am doing here?
this
This is a Java keyword indicateing the current object. If I am not wrong, this method is placed in an class that extends Activity. Hence, this refers to the Activity object( which is used as Context in this case). And since it is a static method, you cannot used the keyword this or non-static variables.
In order to fix it,change to this line:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, list);
In java you can not use this in static methods (static context).
Static methods do not point to any instance of the enclosing class.
You are using this in static method which is not legal in java.