Android: difficulties referencing resources

96 Views Asked by At

I'm still a newbie to android programming, so i tried a tutorial that was to help create a music app. At a stage in the tutorial i was asked to create a new class that extends base Adapter and at a point in this new class i was asked to use Layout Inflater to to inflate a secondary layout, which i created. But the problem is that from this new class which i created when i try inflating or referencing any resource element at all, i get errors. so my question is : 1. Is it impossible to refer to resources from classes that i create ? 2. Is there an error in my IDE(eclipse) or its just a java thing. here's the secondary layout XML file and the class i created. the java lines with * is where i have the errors. when inflating "R.layout.song","R.id.song_artist" and "R.id.song_title"

import android.R;
import android.R.*;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;

public class SongAdapter extends BaseAdapter {

private ArrayList<Song> songs;
private LayoutInflater songInf;

public SongAdapter(Context c, ArrayList<Song>theSongs){
    songs = theSongs;
    songInf = LayoutInflater.from(c);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return songs.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout songLay = (LinearLayout) songInf.inflate(R.layout.song,parent,false);*
    TextView songView = (TextView)songLay.findViewById(R.id.song_title);*
    TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);*
    Song currSong = songs.get(position);
    songView.setText(currSong.getTitle());
    artistView.setText(currSong.getArtist());
    songLay.setTag(position);
    return songLay;
}

}

the XML file

<?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"
android:orientation="vertical"
android:padding="5dp"
android:onClick="songPicked" >

<TextView
    android:id="@+id/song_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF99"
    android:textSize="20sp"
    android:textStyle="bold"/>

<TextView
    android:id="@+id/song_artist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF99"
    android:textSize="18sp"
    android:textStyle="italic"/>


</LinearLayout>

the error description reads "Description Resource Path Location Type song_artist cannot be resolved or is not a field song cannot be resolved or is not a field
line 46 Java Problem song_title cannot be resolved or is not a field

"

thanks in advance.

2

There are 2 best solutions below

2
On BEST ANSWER

What is R pointing at in your code?

It should point at something like your.package.name.R. If it is pointing to Android.R, it won't work. Remove import Android.R and import Android.R.*, then it should be OK.

1
On

You can hit Ctrl+Shift+O to organize your imports and if you still have a problem change Android.R to packageName.R and this should solve the problem.