how can i create an arrayadapter in asynctask. onPostExecute - java

protected void onPostExecute(String s) {
veriAdaptoru=new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, android.R.id.text1, a);
liste=(ListView)findViewById(R.id.listView1);
liste.setAdapter(veriAdaptoru);
}
i have to write this code but in this part i get error
(veriAdaptoru=new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, android.R.id.text1, a);)

The problem is in new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, android.R.id.text1, a);
you must load context from Activity, but if you write this in onPostExecute it is another. Make in you Activity class some variable
Context context = MyActivity.this;
if your AsyncTask is in another class - create constructor and pass context from Activiy in it.
and then you can do
(context, android.R.layout.simple_list_item_1, android.R.id.text1, a);`

Related

Android listview contains buttons that shouldn't be there

I have an issue with my ListView where for some unknown reason each element has a button that shouldn't have. How could i go about removing these buttons?
Android emulator showing the buttons in the listview
The issue seems to be with my onPostExecute method for an async task.
#Override
protected void onPostExecute(String pResult) {
//Populate the list view?
ListView listView = (ListView) findViewById(R.id.DownloadablePuzzlesList);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
R.layout.activity_download, R.id.DownloadablePuzzlesText, PuzzleNames);
listView.setAdapter(arrayAdapter);
}
After hours of trying to figure this issue out i'm unable to find the solution to my issue any help would be appreciated.
Your R.layout.activity_download must be having button.
Instead of:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.activity_download, R.id.DownloadablePuzzlesText, PuzzleNames);
Do:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, PuzzleNames)

Call view method from an activity in non-activity class

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 .

Cannot resolve constructor ArrayAdapter (Listview , fragment )

I want to load a listview in my fragment and the problem with the adapter is the error - Cannot resolve constructor ArrayAdapter.
This is the onCreateView method in my class :
public class AndroidGUIModifier implements IMyComponentGUIModifier, IFragmentEvents {
#Override
public void onCreateView() {
tv1 = (TextView) fragment.findViewById("xtv1");
tv2 = (TextView) fragment.findViewById("xtv2");
lv=(ListView) fragment.findViewById("xdevices") ;
mydevices = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AndroidGUIModifier.this,android.R.layout.simple_list_item_1,mydevices);// the problem here : Cannot resolve constructor ArrayAdapter
lv.setAdapter(adapter);
}
}
Obtain the Context from the fragment and just pass it to the ArrayAdapter :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(fragment.getContext(), android.R.layout.simple_list_item_1, mydevices);
If it is inside a fragment, you need to pass context for the first argument usually like this I think?
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mydevices);
If you are using AndroidGUIModifier as your fragment, can you try extending the Fragment class?
public class AndroidGUIModifier extends Fragment implements IMyComponentGUIModifier, IFragmentEvents {

"this" gives me an error in fragment

Why does this give me an error in the below fragment?
This is my code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
Try this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, spinnerArray);
getContext() was added only on API 23 and should work only with few devices (since Android M still does not have a huge market share).
getActivity() was added on API 11 and it is NOT deprecated.. So, you can use it.
Replace this with this.getContext() or even simply just getContext()
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item, spinnerArray);
The reason being is that the constructor for an ArrayAdapter object looks like this,
From https://developer.android.com/reference/android/widget/ArrayAdapter.html,
ArrayAdapter(Context context, int resource, T[] objects)
or
ArrayAdapter(Context context, int resource, List<T> objects)
What was probably happening before is that the class that the line of code was in was most likely not of type Context; therefore, you can't use this alone as a parameter in the construction of an ArrayAdapter. If getContext() doesn't work, then I'd suggest trying getActivity() or getApplicationContext() as well.
Use getActivity() which return the context of the activity that the fragment is associated with.
So your code becomes
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, spinnerArray);`]
You can try getActivity().getApplicationContext() instead of using just this in fragment.

How to get list in alert dialog?

I want to set an alert dialog with list of titles. I have database and table of time table in that.
Time table has column of title. I want to get the list of titles from time table.
I tried some code but it's not working. I have created query in TimeTableHelper to get list of table. But I don't know how to put it in alert dialog.
Can anyone help please?
selectTable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TimeTableHelper th = new TimeTableHelper(getApplicationContext());
List<String> tables = new ArrayList<String>(th.getTitle());
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddEventActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.alertlistrow, null);
alertDialog.setView(convertView);
ListView lv = (ListView) convertView.findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tables);
lv.setAdapter(adapter);
alertDialog.show();
}
});
I am getting error can not resolve constructor ArrayAdapter if I try to add list in this.
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, tables);
How t do this?
You need to pass activity context but inside onClick method this means the onClickListener interface.
Change
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, tables);
to
// Replace MyActivity with the activity you are currently in or getActivity() if inside a fragment
ArrayAdapter adapter = new ArrayAdapter(MyActivity.this, android.R.layout.simple_list_item_1, tables);

Categories