In my application I am experiencing the same sort of error as found here or here however the suggested solution of cleaning the Eclipse project isn't working for me. Specifically I have a custom ListView adapter that is attempting to load two TextViews from its layout file, the first TextView is found no problem but the second throws the ResourceNotFound exception and crashes the application.
Layout File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/roomNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:text="Room name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/roomNumText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/roomNameText"
android:layout_below="#+id/roomNameText"
android:text="Room number"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
m = new ViewHolder();
convertView = mInflater.inflate(R.layout.room_row, null);
convertView.setTag(m);
} else {
m = (ViewHolder) convertView.getTag();
}
m.name = (TextView) convertView.findViewById(R.id.roomNameText);
m.name.setText(rooms.get(position).getName());
m.roomNumber = (TextView) convertView.findViewById(R.id.roomNumText);
m.roomNumber.setText(rooms.get(position).getRoomNumber());
return convertView;
}
I have also tried editing R.java just to force Eclipse to regenerate the file but that also doesn't work. Really not sure what else to try since I have another adapter that is basically the same but for different data that works perfectly. Also ViewHolder is a static class that is just a container class for any views being accessed (have to use it according to coding style) in case anyone was wondering.
change
m.roomNumber.setText(rooms.get(position).getRoomNumber());
with
m.roomNumber.setText(String.valueOf(rooms.get(position).getRoomNumber()));
in your code you are using the setText(int stringId) which looks up inside R.string to retrive the stringId parameter . Also I would change your getView this way:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
m = new ViewHolder();
convertView = mInflater.inflate(R.layout.room_row, null);
m.name = (TextView) convertView.findViewById(R.id.roomNameText);
m.roomNumber = (TextView) convertView.findViewById(R.id.roomNumText);
convertView.setTag(m);
} else {
m = (ViewHolder) convertView.getTag();
}
m.name.setText(rooms.get(position).getName());
m.roomNumber.setText(String.valueOf(rooms.get(position).getRoomNumber()));
return convertView;
}
Related
Please help me.
How can i add favourite (CheckBox/ToggleButton/ImageView) to my ListView? ... I have never added a favourite button before so I would like to add one in listView with CheckBox or ToggleButton or ImageView .
Hope everyone understand my question
My code :
This is my ListAdapter
static class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Context ctx;
public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
ctx = context;
}
public int getCount() {
return RecipeName.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.txtRecipeName = (TextView) convertView.findViewById(R.id.txtRecipeName);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtRecipeName.setText(RecipeName[position]);
return convertView;
}
static class ViewHolder {
TextView txtRecipeName;
}
}
This is my row xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1"
android:gravity="center_horizontal"
android:background="#drawable/row">
<TextView
android:id="#+id/txtRecipeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/text"
android:textSize="16sp"
android:textStyle="bold"
android:layout_weight="0.04"
android:typeface="normal"
android:background="#drawable/button2"/>
</LinearLayout>
For adding image u can use ImageView same as TextView in the .xml file..
Just add the src of the image after it "android:src="#drawable/beach" like this.
Here "#drawable/beach" is the path of image.
`<ImageView
android:id="#+id/photo_image_view" //for id
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" //this will display image in center
android:src="#drawable/beach" /> //path of image`
For Toggle Button..use the following code in .xml file
<Switch
android:id="#+id/toggle_switch" //give id so can use this in java code later
android:layout_width="wrap_content" //set width
android:layout_height="wrap_content" //set height
android:text="Toggle Button" // string to be displayed
android:textAppearance="?android:textAppearanceSmall" />
How it will look
Toggle Button
I have a class LanguagesListAdapter and I want to use condition for rows to show an image in a specific row. TextViews values are correct but ImageView for each row make visible. My code is here:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = mcontex.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.language_list, null);
holder = new ViewHolder();
holder.langTextView = (TextView) convertView.findViewById(R.id.languageTextView);
holder.langTextView.setGravity(Gravity.LEFT);
holder.check = (ImageView) convertView.findViewById(R.id.check_lang);
holder.check.setVisibility(View.GONE);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.langTextView.setText(languages[position]);
if (session.getLanguage().equals("fa") && languages[position].equals("farsi")) {
holder.check.setVisibility(View.VISIBLE);
} else if (session.getLanguage().equals("en") && languages[position].equals("English")) {
holder.check.setVisibility(View.VISIBLE);
}
return convertView;
}
and my list XML is here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="#+id/languageTextView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<ImageView
android:id="#+id/check_lang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
app:srcCompat="#drawable/ic_check_dark"/>
</RelativeLayout>
How can I fix it to make ImageView visible just in a specific row?
If view is recycled (which means convertView is not null), ImageView will not gone. So add else statement to make ImageView gone.
if (session.getLanguage().equals("fa") && languages[position].equals("farsi")) {
holder.check.setVisibility(View.VISIBLE);
} else if (session.getLanguage().equals("en") && languages[position].equals("English")) {
holder.check.setVisibility(View.VISIBLE);
} else {
holder.check.setVisibility(View.GONE);
}
Im super confused between what is passed to the methods initially.
In ArrayAdapter getview method, What is the View and ViewGroup passed
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
....
}
List_item xml is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:fontFamily="serif"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
....
</LinearLayout>
What is the ViewGroup passed in RecyclerView adapter here.
#Override
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
NumberViewHolder viewHolder = new NumberViewHolder(view);
return viewHolder;
}
Number_list_item is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/tv_item_number"
style="#style/TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:fontFamily="monospace"
android:textSize="42sp"
tools:text="#42" />
</FrameLayout>
What does the below line do exacty besides inflating the layout.
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately
View view = inflater.inflate(layoutIdForListItem, viewGroup,
shouldAttachToParentImmediately);
When you want to show any view on UI it must have some height and width. In a inflate method,
First parameter is a resource id of a view, which should inflate. In a case of Adapter, it should be item view which is going to be recycled.
Second parameter is a ViewGroup, generally parent view. Parent view must be a ViewGroup because ViewGroup contains child views not vice versa. In a case of ListView sometimes size(width) of an inflated view not adopted automatically depends on its parent.
For an example - If a ListView have a width match_parent then there is a possibility width of your inflated view less than its parent if you skipped this parameter.
Third parameter is used to decide whether you want to add your inflated view inside parent ViewGroup(2nd parameter). In a case of ListView, if you make it true then you will get RunTimeException (View already have a parent), this parameter should be true if you want to add new view inside parent view.
For an example - If I want to add View in my UI only after sudden action, then I will inflate a 'View' and add in a ViewGroup.
I know how to customize the spinner. In my case it is a little bit different. I cannot modify my code more than I have now. Normally, I am able to make spinner that is reachable from all activities in my app.
I am trying to put icon along the textview in the spinner drop down.
Here the code I have;
Spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<Spinner
android:id="#+id/spinner"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:spinnerMode="dialog"
android:prompt="#string/language_prompt" />
spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:addStatesFromChildren="true"
android:gravity="center_horizontal"
android:padding="5dp"
android:textColor="#FFFFFF"
android:textSize="17sp" >
</TextView>
<!-- Here I cannot use Relative layout. If I use it gives error like [java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView]. Without layout I cannot implement ImageView. I am stuck here.
My activity class;
public class Base_Activity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//int flags = R.array.flags;
final Spinner spinner = (Spinner) menu.getItem(0).getActionView()
.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.languages, R.layout.spinner_row);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
What I want is to make is custom Adapter so that I can put image along the textview in spinner list. I am not able to make normal way that I checked in many tutorials. I think it's because I am creating spinner in onCreateOptionsMenu() constructor.
u can take a look at this tutorial on AndroidHive
http://www.androidhive.info/2013/11/android-working-with-action-bar/
code snippet
// Spinner title navigation data
navSpinner = new ArrayList<SpinnerNavItem>();
navSpinner.add(new SpinnerNavItem("Local", R.drawable.ic_location));
navSpinner
.add(new SpinnerNavItem("My Places", R.drawable.ic_my_places));
navSpinner.add(new SpinnerNavItem("Checkins", R.drawable.ic_checkin));
navSpinner.add(new SpinnerNavItem("Latitude", R.drawable.ic_latitude));
// title drop down adapter
adapter = new TitleNavigationAdapter(getApplicationContext(),
navSpinner);
getView() and getDropDownView() from TitleNavigationAdapter
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
imgIcon.setImageResource(spinnerNavItem.get(position).getIcon());
imgIcon.setVisibility(View.GONE);
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
imgIcon.setImageResource(spinnerNavItem.get(position).getIcon());
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
Download that code and u r good to go
I'm in the process of making a music app and I'm currently working on the library functionality. I'm having some problems, however, in working with a list view (In particular, the cells). I'm trying to move from a simple textview layout in each cell that's created within java to one that uses an XML file for layout (Hence keeping the Java file mostly semantic)
This is my original code for the cell layout:
public View getView(int position, View convertView, ViewGroup parent) {
String id = null;
TextView tv = new TextView(mContext.getApplicationContext());
if (convertView == null) {
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id += "\n" + musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM);
musiccursor.moveToPosition(position);
id += "\n" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
And my new version:
public View getView(int position, View convertView, ViewGroup parent) {
View cellLayout = findViewById(R.id.albums_list_cell);
ImageView album_art = (ImageView) findViewById(R.id.album_cover);
TextView album_title = (TextView) findViewById(R.id.album_title);
TextView artist_title = (TextView) findViewById(R.id.artist_title);
if (convertView == null) {
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM);
musiccursor.moveToPosition(position);
album_title.setText(musiccursor.getString(music_column_index));
//music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
//musiccursor.moveToPosition(position);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
musiccursor.moveToPosition(position);
artist_title.setText(musiccursor.getString(music_column_index));
} else{
cellLayout = (TextView) convertView;
}
return cellLayout;
}
The initialisation (done in the on create file):
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(this));
musiclist.setOnItemClickListener(musicgridlistener);
And the respective XML files:
(main)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/PhoneMusicList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1.0"
android:text="#string/no_list_data"
/>
</LinearLayout>
(albums_list_cell)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/albums_list_cell"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/album_cover"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_width="50dip"
android:layout_height="50dip"
/>
<TextView
android:id="#+id/album_title"
android:layout_toRightOf="#+id/album_cover"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/artist_title"
android:layout_toRightOf="#+id/album_cover"
android:layout_below="#+id/album_title"
android:layout_width="wrap_content"
android:layout_height="15dip"
/>
</RelativeLayout>
In theory (based on the tiny bit of Android I've done so far) this should work..it doesn't though. Logcat gives me a null pointer exception at line 96 of the faulty code, which is the album_title.setText line. It could be a problem with my casting but Google tells me this is ok :D
Thanks for any help and let me know if you need more info!
EDIT: Just to point out, the code this section is based on is from, most of is the same as mine for now
http://androidsamples.blogspot.com/2009/06/displaying-list-of-video-files-stored.html
When you want to use a custom layout to represent each item, in your adapter you need to inflate your custom layout in your getView method, using a LayoutInflater.
So i would change the following line:
View cellLayout = findViewById(R.id.albums_list_cell);
to:
View cellLayout = LayoutInflater.from(mContext).inflate(R.layout.albums_list_cell,null);
You need to move your albums_list_cell.xml into the layout folder aswell.
You would then also edit how you retrieve the relevant widgets defined in your layout as follows:
ImageView album_art = (ImageView) cellLayout.findViewById(R.id.album_cover);
TextView album_title = (TextView) cellLayout.findViewById(R.id.album_title);
TextView artist_title = (TextView) cellLayout.findViewById(R.id.artist_title);
Hope this helps!
Solved the above problem...note the (TextView) cast on the following lines. This should be a relativelayout cast of course
} else{
cellLayout = (TextView) convertView;
}