I am facing problem while putting Spinner in layout. My acctual requirement is to place Spinner once, at the top of layout.
This output I am getting:
I have Relative layout
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/string_array"
android:prompt="#string/spinner_msg"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/notesTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
</TextView>`
MyActivity class is extended by ListActivity here's onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=getListView();
listView.setOnItemClickListener(listener);
spinner1=(Spinner)findViewById(R.id.spinner1);
getDetailsCursor();
String[] from = new String[] {"db_column"};
int[] to = new int[] { R.id.notesTextView};
curAdapter=new SimpleCursorAdapter(MyActivity.this, R.layout.mylist, null, from, to);
setListAdapter(curAdapter);
registerForContextMenu(listView);
}`
Here is the code in which spinner inflates on the top of listview :
listView= (ListView) findViewById(R.id.list);
LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.spin, null);
authorityView.addHeaderView(mTop);
R.layout.spin is that layout which contains only spinner.
And inside the List you have to inflate textView only. as you are doing in mylist.xml ,
just remove spinner from it and made seprate xml for spinner.
so spinner is once, at the top of layout (ListView).
I have some difficulty making out what your actual question is, some layout.xml code would help here, too. I think, that you are placing the spinner inside the listitem.xml instead of the main.xml, so that it gets replicated for each item in your listview. Please share some code.
Since you declare both the TextView and the Spinner in mylist.xml, you get both those elements in each Item of your List.
If you only want one spinner, you shouldn't use a ListActivity, but instead create a normal Activity with a Spinner and a ListView in the Layout. Then define another layout to use for the list items (e.g. with only the TextView).
Related
I'm trying to add some items in a ListView. When I swith the Layout (startActivity), I call the class Listing.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listing);
ListView lv = (ListView) findViewById(R.id.listViewLIST);
String[] items = {"Item1", "Item2", "Item3", "Item4"};
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(items));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.activity_listing, R.id.listViewLIST, arrayList);
lv.setAdapter(adapter);
}
I read some tutorials an followed every step but the app stops working always in the last line of code
lv.setAdapter(adapter);
What am I missing?
Edit 27.12.2015
The activity_listing.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="cigarkings.cigarking.Listing">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewLIST"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="51dp" />
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/listView"
android:layout_alignEnd="#+id/listView" />
<include layout="#layout/content_listing" />
</android.support.design.widget.CoordinatorLayout>
Assuming missing statement android:layout_width="match_parent" for your Coordinator Layout as a typographical error,the major error is in setting up array adapter.
In the statement : ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.activity_listing, R.id.listViewLIST, arrayList); the constructor parameters (2nd parameter:R.layout.activity_listing and 3rd parameter:R.id.listViewLIST) are incorrect.Array Adapter takes resource id of a textview as 3rd argument and not of a listview, and the layout resource id of the layout containing the textview is passed as 2nd argument.
So, one solution for this is to create a separate xml layout containing a textview and passing the layout resource id as 2nd argument and textview id as 3rd argument to the constructor of Array Adapter
First of all activity listing should not be in this pattern.
If you are at beginning stage then First try this Tutorial...
The best explanation ever for beginners.
Then you have to implement custom listView using BaseAdaptor class and custom XML.
I have a ListFragment that might have no items in the listview. I've added the empty view in the xml:
<TextView
android:id="#+id/empty"
android:text="#string/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"/>
Usually, when using a ListActivity, one would do:
#Override
public void onContentChanged() {
super.onContentChanged();
View empty = findViewById(R.id.empty);
ListView list = (ListView) findViewById(R.id.list);
list.setEmptyView(empty);
}
This will hide/show the empty view depending if the listview has items or not.
What is the equivalent of a ListActivity.onContentChanged for a ListFragment?
This does the trick, without the need to override methods or create observers:
final View emptyView = view.findViewById(R.id.empty);
getListView().setEmptyView(emptyView);
I have a fragment with a listview. The adapter only has one field: the item id.
When I add an item to the list using the adapter, the list shows the item id.
But if I put a simple item id check before (a for-cycle that checks if the item id is equals to one specific id, if so then adds the item id), the list shows an empty row (like it added something) but without showing the item id.
What could be? I'm sure that the "check part" works.
EDIT: I forgot to specify that the item id is a String.
EDIT 2: I added the check part of the code. It loops through list that contains the item ids that I want to add to the other fragment list. item is the item that I'm checking. blueF is the fragment.
for(int i=0; i<list.getCount();i++) {
if(list.getItem(i).equals(item.getId())) {
//send the item to the listview of the fragment
blueF.getArrayAdapter().addItem(item);
break;
}
}
EDIT 3: After some testing, I discovered that the listview inside the fragment have inside the item (I used getCount and also printed the item id), but it isn't showing the item id! My custom adapter have a notifyDataSetChanged() in the "add" method and I've also recalled the notifyDataSetChanged after the call to the add method in the activity...but nothing.
EDIT 4: Probably is some problem with the layout?
I have this layout for the list row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inventory_item_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/inventory_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:textSize="17sp"
android:typeface="monospace" >
</TextView>
<TextView
android:id="#+id/inventory_item_counter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="17sp"
android:typeface="monospace" />
</LinearLayout>
the fragment layout with the listview:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/listBlueTagView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
Your filter code should be done before setting the Data of the Adapter, so the Adapter have only the items that it will show.
From your question, I understood this,
Try this in your main Activity, these are just Pseudo codes there may be some typos.
public class Main extends Activity{
EditText etID; //Your Item ID field.
ListView listView; // Listview in your XML.
int check_ID; // ID which you want to compare with Edit Texts' value.
List<String> arrayList= new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_XML);
etID = (EditText) findViewById(R.id.editTextID); //ITEM_ID editText id in XML
listView = (ListView) findViewById(R.id.your_ListView_ID);
check_ID = 1; // suppose you want to add into your list if user enters 1 in editText as Item ID
if(check_ID = Integer.parseInt(etID.getText().toString())) //get value from EditText then check it with your desired value and on being equal add item to list
// add item here and set it to listView
arrayList.add("Mango");
ArrayAdapter<String> array_Adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
arrayList);
listView .setAdapter(array_Adapter );
} else{
Toast.makeText(getApplicationContext(),"Wrong ITEM ID",Toast.LENGTH_SHORT).show();
}
}
I've resolved the problem.
It was a graphic problem generated in the getView of the adapter. Basically, some elements overwrote the color text of the ID, rendering it invisible.
I set the text color to Color.BLACK and now I can see the ID of the item.
Beginner question here I wonder if someone can point me in the right direction. I need a listview of 'accounts' where each item will contain a textview, and a spinner to choose from a preset amount, so each item will be like so:
________________________
|TextView---------Spinner|
|________________________|
Here's what I have so far:
activity_topup.xml This is the main xml for the activity
<ListView
android:id="#+id/topup_accounts_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/stq_grey"
android:divider="#android:color/transparent"
android:dividerHeight="20.0sp"/>
And then in my java file TopUpActivity.java
String[] listAccounts = { "Acc1", "Acc2", "Acc3"};
accountListAdapter = new ArrayAdapter<String>(this, R.layout.listview_item_accounts, R.id.accounts_list_tv, listAccounts);
listView = (ListView)findViewById(R.id.topup_accounts_listview);
listView.setAdapter(accountListAdapter);
listview_item_accounts.xml contains the content of each item (a text view and spinner)
<TextView
android:id="#+id/accounts_list_tv"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/accounts_list_amt_spinner"
android:background="#drawable/list_item_selector_grey"
style="#style/ListText" />
<Spinner
android:id="#id/accounts_list_amt_spinner"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:prompt="#string/topup_amt_prompt"
android:background="#drawable/list_item_selector_blue"
style="#style/ListText" />
Am I on the right track?! The result is kind of correct but I can't quite figure out how to populate the spinners, all I can populate is the text views using this method I think. I added this to the java file in an attempt to populate the spinners too, but the spinners remain empty:
//Inflate the listview items
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.listview_item_accounts, null);
//Get the spinner
amt = (Spinner)vi.findViewById(R.id.accounts_list_amt_spinner);
//Add the values to the spinner by setting this adapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.topup_amts, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
amt.setAdapter(adapter);
Here is the result (The spinners have the blue background):
And when I click on one of the spinners they are empty, I get this
You have to find your sub views in it's parent.For example in getView of your adapter,you iflate your item(from listview_item_accounts.xml) as GroupView with name "item",then you have to find spinner in it:
Spinner amt = (Spinner)item.findViewById(R.id.accounts_list_amt_spinner);
If you use
Spinner amt = (Spinner)findViewById(R.id.accounts_list_amt_spinner);
you tried to find a view that it's id is equal to "R.id.accounts_list_amt_spinner" in content view,not in each item.
I have used this code for the row layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/bckimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
and this is for the class that builds the list:
public class ListTestActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
String[] myArray = new String[] {...};
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.listlayout,myArray));
ListView listView = getListView();
listView.setCacheColorHint(0);
listView.setBackgroundResource(R.drawable.bckimage);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
//. . .
});
}
}
This shows an image for each row, not for the whole background. How can I add a single image for background? I saw several methods in here also but nothing worked.
Set the image as the background for the ListView element instead:
getListView().setBackgroundResource(R.drawable.bckimage);
Your list rows probably hide the background image. Also add:
getListView().setCacheColorHint(android.R.color.transparent);
As you stated, set background to your list and not to the row layout.
You can set background to your list in following way:
((ListView)findViewById(android.R.id.list)).setBackgroundResource(R.drawable.bckimage);
Whenever you are using ListActivity, it have ListView with id "list".