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.
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 use an adapter to load a list view from a content provider. Each item in the list uses the below layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id ="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckedTextView
android:id="#+id/txt_chat"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
</LinearLayout>
</LinearLayout>
I have set the list item to allow multiple choice mode :
onCreate(){
...
lv_chatMessages = (ListView) findViewById(R.id.listview_chat);
lv_chatMessages.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv_chatMessages.setItemsCanFocus(false);
...
}
But at run time, i am not able to check any of the items in the list view. If i touch the box, the item just gets highlighted momentarily, but no check mark appears.
I can set a default for all the items if i add an attribute to the CheckedTextView :
android:checked="true"
How do i let the user change the check status of each item at runtime?
refer this link...
CheckedTextView
this may help you..
[Edit by Faizal] :
Using the toggle() inside the onItemClickListener did the trick :
lv_chatMessages.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
CheckedTextView ctv = (CheckedTextView) (((ViewGroup) arg1).findViewById(R.id.txt_chat));
ctv.toggle();
}
});
can you add this into list items
android:descendantFocusability="blocksDescendants"
android:focusableInTouchMode="false"
Since, you are using a custom adapter, you'll have to maintain the status of the checkbox in the getView() method of the custom adapter.
What you could probably do is to add another Boolean field to your ArrayList of Objects which you use to populate the adapter and check that value in the getView() method. to Something like :
if(status) {
checkBox.setChecked(true);
}
Also, update the status variable using OnCheckedChangeListener().
This method will let you toggle status and also ensure that the checkbox status is maintained even when a list item goes out of the view.
Edit 1 : When not using a custom adapter. This is how I did it.
ListView rendererList;
ArrayList<String> friendlyNames;
friendlyNames = new ArrayList<String>();
rendererList = (ListView) dialog.findViewById(R.id.rendererList);
ArrayAdapter rendererSelectionAdapter = new ArrayAdapter<String>(activity,R.layout.custom_list_layout, friendlyNames);
rendererList.setAdapter(rendererSelectionAdapter);
rendererList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
custom_list_layout was the same layout which android provides, however, I had to make slight modifications so I copied the default file and edited.
here is the custom_list_layout :
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:background="#color/white"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_vertical"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:textSize="#dimen/dialog_box_button_textsize" />
Currently, I have a
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="#+id/listview1"></ListView>
<ListView android:id="#+id/listview2"></ListView>
</LinearLayout>
and on 2 of my button clicks events I publish data to the corresponding ListViews:
Button1 click:
List result = GetSomeDate();
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, result);
listview1.setAdapter(customAdapter);
Button2 click:
List result = GetSomeOtherDate();
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, result);
listview2.setAdapter(customAdapter);
I wonder, can I leave only one ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="#+id/universalListView"></ListView>
</LinearLayout>
and append List to it on my events, if the List type is the same?
The only rule is, that List from Button1 click should be always on top, no matter, if Button1 is clicked before or after Button2.
Thanks in advance!
create a method like that in your custom adapter : HERE data is your list in adapter.
public void addAll(List<mtItems> items)
{
if(item!=null && item.size()>0){
data.addAll(items);
notifyDataSetChanged()
}
}
call this method of adapter whenever you want to add some items in your already populated list. like
customAdapter.addAll(myItems);
thats all .
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).
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.