I have a navigation drawer which uses a ListView defined in the main activity layout:
<!-- Navigation drawer (left) -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/darker_gray"
android:dividerHeight="1dp"
android:background="#e5e5e5"/>
and I populate it like this:
navMenuItems = getResources().getStringArray(R.array.NavMenuItems);
navDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
navDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.nav_list_item, navMenuItems));
As the ArrayAdapter needs a TextView resource ID to be passed to it, I use a separate layout file with only a TextView in it for menu items:
nav_list_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nav_menu_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="right"
android:gravity="right"
android:layoutDirection="rtl"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingRight="8dp"
android:textColor="#android:color/white"
android:textSize="50sp"
android:textIsSelectable="true"
android:textAlignment="textStart"
android:textDirection="anyRtl" >
</TextView>
BUT, the TextView has no effect whatsoever and the menu items are shown the same no matter what attribute is changed!
Why is this happening? What am I doing wrong here?
In Resource, you should give layout instead of id of text view
new ArrayAdapter<String>(this, R.layout.'yourlayoutname', navMenuItems)
You should add a param to ensure that the adapter knows what view to map the contents of the navitem.
As the 3rd param, add the id of the textview's id. Like this:
new ArrayAdapter<String>(this, R.layout.nav_list_item, R.id.nav_menu_text, navMenuItems)
instead of
new ArrayAdapter<String>(this, R.layout.nav_list_item, navMenuItems)
Im not sure about this, but if you don't want to use the textview's id, it might work naming the id field to "text01" which is the id of the android.R.layout.simple_list_item_1's textview.
try this:
new ArrayAdapter<String> (Context context, R.layout.nav_list_item, R.id.nav_menu_text, navMenuItems);
Hope this helps you...
I found a solution:
I made a custom adapter for navigation list and in its getView() (#override) used my textView:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
Log.d("Log", "Null v");
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.nav_list_item, null);
}
String s = values.get(position);
TextView textView = (TextView) v.findViewById(R.id.nav_menu_text);
textView.setText(s);
return v;
}
Now the attributes in textView take effect.
Related
I connent an addevent.java with addevent.xml is content a listview, the listview take its element from another XML file "item.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:drawablePadding="0dip"
android:drawableRight="#drawable/icon_name" />
<EditText
android:id="#+id/en"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/d" >
</EditText>
</LinearLayout>
now I need to findviewbyId not from basic xml file but form another "item.xml"
I tried LayoutInflater as this, code but it isn't work:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addevent);
myList = (ListView) findViewById(R.id.MyList);
myList.setItemsCanFocus(true);
myAdapter = new MyAdapter();
myList.setAdapter(myAdapter);
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.item, null);
tvDisplayDate = (EditText)textEntryView.findViewById(R.id.editd);
btnChangeDate=(Button)textEntryView.findViewById(R.id.d);
any help please
Try this View view = LayoutInflater.from(getApplication()).inflate(R.layout.item, null);
How you can see, static from() method takes 1 argument(Context) and you obtains LayoutInflater from given Context, in your case it will be your Activity where you want to inflate View.
Hope it helps!
From insinde your MyAdapter class you can override the getView method (that's the one that produces the item renderers on demand).
After recycling / inflating the proper layout for the current item (say convertView), you can access all of its children using the findViewById method called on the convertView:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
// initialize, inflate convertView from your layout if needed;
tvDisplayDate = (EditText)convertView.findViewById(R.id.editd);
// populate convertView with the item's details
}
I have a ListView (with an Adapter of my own) where I'd like to add a delete button by swaping an item on the ListView (like on iPhones).
I really don't know how to do it and where to start ...
Could you please give me some hints ?
Thanks
What you want to implement is a custom ListView. You need a layout for your row, here's an example
res/layout/row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent">
<TextView android:id="#+id/Browse_DateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
<ImageButton android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
Then you need to overide the getView() method of your adapter, a little like this:
setListAdapter(new ArrayAdapter<Object>(this, R.layout.row, R.id.Browse_DateTime, ourRows) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)BrowseActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
TextView time = (TextView) v.findViewById (R.id.Browse_DateTime);
time.setText(ourRows[position].dateTime);
ImageButton delete = (ImageButton) v.findViewById(R.id.delete);
delete.setFocusable(false);
delete.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.deletebutton));
delete.setOnClickListener(BrowseActivity.this);
delete.setId(position);
return v;
}
};)
Best regards.
PS: this is cut&paste from my code, BrowseActivity is just the name of the activity this code resides in, R.layout.row is my row.xml file, you name it any which way, just put it in /res/layout/, and if your delete button is an imagebutton, you DO need the delete.setFocusable(false); (try it without and see why).
I am not quite sure my question really has something to do with listview.There is an app named Gleeo Time Tracker ,and here has a screenview of it.When you press the symbol "+",a new item will be created,and you can delete one item by pressing the "-".More is that when I click the record button on the left of the item,the background will change.
My question is,what is it in the end,a listview? How can I achieve such thing?Thank you all!
What you want to do, is build a custom ListView.
This is done by providing a layout file, for example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent">
<ImageView android:id="#+id/Plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
<TextView android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
</LinearLayout>
This layout is then applied to each row of your ListView by overriding its Adapter's getView() method, for example something like this:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
ImageView plus = (ImageView) v.findViewById(R.id.plus);
icon.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.plus));
TextView title = (TextView) v.findViewById (R.id.Browse_FileName);
// add a listener to your button and you're done
return v;
}
read about list view in developer docs and here is an example
I am developing an android application which contains a ListAtivity class and get it is data as follow :
ArrayAdapter<Item> ara=new MyArrayAdapter(this,_items);
setListAdapter(ara);
And I defined MyArrayAdapter :
....
public MyArrayAdapter(Activity context, List<BirthdayContact> list) {
super(context,R.layout.list_row,0,list);
//super(context, R.layout.birthday_list, list);
this.context = context;
this.list = list;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.birthday_list_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder._cName= (TextView) view.findViewById(R.id.contact_name);
viewHolder._cImage = (ImageView) view.findViewById(R.id.contact_image);
viewHolder._cbirthDay=(TextView)view.findViewById(R.id.contact_birthday_remained);
view.setTag(viewHolder);
}
......
But the problem with this way is that you cant only assign each row`s template and you cant have other widget on list view which are not part of list of data. I mean I want to have a say a TextView which shows messages to user, and below that I show the list of rows.
Can you help me please?
If you want a different layout than a simple ListView you have the option of setting the content view to a layout file like this:
setContentView(R.layout.layout_with_diferrent_views); // call this on the onCreate() method
where R.layout.layout_with_different_views could be a xml layout like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Because you extends ListActivity you must have in the layout a ListView element with the id #android:id/list. Of course you can have a more complex layout than the one above as long as you have a ListView element with the id #android:id/list
You should consider using a listHeader : lv.addHeaderView(findViewById(R.id.header));
This has to be done in your onCreate method in your activity, and you must provide a widget with the id header.
If you want other components in activity.Then better consider Normal Activity instead ListActivity.
In my application, I have a ListActivity and a custom layout for it. In the custom layout, I have an ImageView and a TextView.
I want to be able to change the ImageView resource based on some conditional code in my app. However, when I try to image.setImageResource(resource);, I get javaNullPointerException. I have tried placing the code snippet in different places, like after the adapter. Does anyone have any ideas? Thanks in advance.
Also, I did declare the image view before referring to it with ImageView image = (ImageView) findViewById(R.id.image);
EDIT:
In my activity:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, DlistArray));
// Here I want to change the image
ImageView celebIcon = (ImageView) findViewById(R.id.icon);
celebIcon.setImageResource(R.drawable.phil);
ListView celebList = getListView();
registerForContextMenu(getListView());
celebList.setTextFilterEnabled(true);
In my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:src="#drawable/alan" />
<TextView
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_vertical"
android:text="#+id/label"
android:textColor="#ffffff"
android:textSize="20dp" />
</LinearLayout>
Use a customized adapter:
setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, R.id.label, yourItems){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if( convertView == null ){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
ImageView image = (ImageView) findViewById(R.id.icon);
image.setImageResource(R.drawable.phil);
return convertView;
}
});
(You can also just create your own class extending arrayadapter if you want to reuse it)
To set the text view, just add the code for that in the getView() method, just like the image-setting-code.
In your case I think you need to write a custom adapter (take a look here).
The code:
(ImageView) findViewById(R.id.icon);
is looking for the ImageView within your main layout, that's why it does not find it.