Android listview contains buttons that shouldn't be there - java

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)

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.

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.

Spinner in Dialog not showing items?

I'm currently developping a small app and therefore want to add a spinner to a dialog.
I looked for several tutorials on the web on how to add items to that spinner.
So this is what I have so far:
#SuppressWarnings({ "rawtypes", "unchecked" })
public void prepareNewClubSpinner(){
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
ArrayList<String> rest = db.getRest();
List<String> list = new ArrayList<String>();
for(String entry : rest){
list.add(entry);
}
ArrayAdapter dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinClub.setAdapter(dataAdapter);
}
I've checked the list with the debugger and it does contain all the entries, so the loop is filling the list correctly but the items do not show up in my spinner whatsoever.
I seem to miss something, but can't figure out what!
Anyone got an idea?

Listview with spinner in Android

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

Categories