Listview with spinner in Android - java

Hey guys I am trying to create a menu using a listview with the code I have shown below. The code I have shown will show Item1, Item2 and Item3 in a list. Now I want to be able to add a spinner to item1 and item2. Is that possible? If so how would i go about doing it.
public class MyList extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] MyList = new String[] { "Item1","Item2","Item3"};
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, MyList));
}

Yes, It is possible. Try the following code. You will get it.
String[] MyList = new String[] { "Item1","Item2","Item3"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, MyList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

I think you need to create custom list view for doing that you can start by following this particular tutorial :
Tutorial
This link may be helpful to you :
Link

Related

How to set a DIFF background color of EACH item clicked on my List- can only get it to send 1 color for each item selected using setBackgroundColor()

public class SecondActivity extends AppCompatActivity {
private String [] myList = new String[]{"Blue", "Green", "Red"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ListView listView = (ListView)findViewById(R.id.ListView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listView.setAdapter(adapter);
}
}
Don't use an ArrayAdapter. Use a custom adapter and set the background of each item.
Better yet- don't use ListView. It's been deprecated for most of a decade. Use a RecyclerView, and set the background color in onBindViewHolder.

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)

Giving three letters in autocompletetextview that a word can contain and add them in a listview

Here i have a code for autocompletetextview. With help of this code i can get all the words that start with first letter that a user type in textfield. But i dont know how to customize this code so the user have to give 3 letters of any word that Contain those letters and it should give me the result and i also want to add that word that a user choose to a listview.
public class tweede extends AppCompatActivity {
private static final String[] producten = new String[] { "Yoghurt",
"Cream", "Cacao", "Cola", "Yummy", "Chocolate" };
AutoCompleteTextView ACTV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tweede);
ACTV=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView2);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, producten);
ACTV.setAdapter(adapter);
ACTV.setThreshold(1);
}
}
you can customize up to two letter. You can check this solution.
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.(here use your layout name),R.id.(autoCompleteTextViewId), producten);
as an example....
ArrayAdapter aa = new ArrayAdapter(this,R.layout.activity_main,R.id.autoCmp,producten);

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);

Sort ListView in ListActivity

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.

Categories