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);
Related
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)
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 {
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);
How my onClickListener should look like if I need to open layout with different text for each ListView item and I have a lot of ListView items (about 50)? Do I need to create new activity or layout file for each new item? Is it possible to use one activity for all items?
This is my MainActivity.java:
public class MainActivity extends Activity {
// ListView
private ListView listView;
// Adapter
ArrayAdapter<String> adapter;
String items [];
// ArrayList
ArrayList<HashMap<String, String>> productList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//ListView data in res/values/arrays.xml
items =getResources().getStringArray(R.array.items);
listView = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.listItem, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
/*
I know that I can put here I can put something like this:
case 0 :Intent appInfo = new Intent(About.this, Activity1.class);
startActivity(appInfo);
break;
case 1 :Intent appInfo = new Intent(About.this, Activity2.class);
startActivity(appInfo);
break;
case 2 :Intent appInfo = new Intent(About.this, Activity3.class);
startActivity(appInfo);
break;
BUT DO I NEED REPEAT THIS MORE THAN 20 TIMES?!
*/
}
});
}
}
Some system apps like Settings has a lot of ListView and layouts and I don't believe that it has new activity for each layout.
Create a new single activity and pass the text to that activity.
Something like:
Intent intent = new Intent(About.this, <NEW_ACTIVITY>.class);
intent.putExtra("text_key", items[position]);
startActivity(intent);
on the other activity, retrieve the text like this (can be on the onCreate method):
String text = getIntent().getExtras().getString("text_key");
I want to make spinner inside my Google Map class so i can change the view of googlemaps like HYBRID, SATELLITE so on.. I make a this spinner on fragment inside of fragment.
when i declare arrayaddapter it keep show "Unfortunately, --- has stopped"
public class GMaps extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.popup_map, null, false);
sview = (Spinner)getActivity().findViewById(R.id.spinner1);
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.getActivity(), android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sview.setAdapter(dataAdapter);
// ..
I think it is because the parameter context = this.getActivity()... I have try this.getView(), getActivity(), v.getContext, nothing works.
First of all you should change this
sview = (Spinner)getActivity().findViewById(R.id.spinner1);
to
sview = (Spinner)v.findViewById(R.id.spinner1);
it's becoz your Spinner view belong to you inflated layout view.
then try to set adapter like:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sview.setAdapter(dataAdapter);
//your spinner is in popup_map.xml so you need to do
sview = (Spinner)getActivity().findViewById(R.id.spinner1);
instead of getActivity() you need to use View v
sview = (Spinner)v.findViewById(R.id.spinner1);