J.erome
J.erome

Reputation: 788

How to link an other xml file to the main_activity.xml in android and to access it?

I'm new to android and following the android documentation I tried to link my android_version_layout.xml to my main activity to access the things inside android_version_layout.xml.

To do that here is my main activity file:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <include layout="@layout/android_version_layout"/>

    <ListView
        android:id="@+id/customListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/listSimple"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>

and here is my android_version_layout.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="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dip"
        android:src="@drawable/list_icon" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/titleT"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="I'm a title"
            />
        <TextView
            android:id="@+id/description"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
</LinearLayout>

Now I have my Java file where I try to change the value of the textview with the id titleT:

TextView title = (TextView) view.findViewById(R.id.titleT);

but for some reason I have a null pointer. When I use the debugger I can see that he finds R.id.titleT because there is a number like 2331165325 but title stays null.

Someone could help me ?

Here is my AndroidAdapter.java

public class AndroidAdapter extends ArrayAdapter<AndroidVersion> {
    private ArrayList<AndroidVersion> androidVersionList;
    private Context context;
    private int viewRes;
    private Resources res;

    public AndroidAdapter(Context context, int viewResourceId, ArrayList<AndroidVersion> versions){
        super(context,viewResourceId,versions);
        this.androidVersionList = versions;
        this.context = context;
        this.viewRes = viewResourceId;
        this.res = context.getResources();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View view = convertView;
        if(view == null){
            LayoutInflater layoutInflater = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(viewRes, parent, false);
        }

        final AndroidVersion androidVersion = androidVersionList.get(position);

        if(androidVersion != null){
            final TextView title = (TextView) view.findViewById(R.id.titleT);
            final TextView description = (TextView) view.findViewById(R.id.description);
            final String versionName = "TEST"; //String.format(res.getString(R.string.list_title), androidVersion.getVersionName());
            title.setText(versionName);

            final String versionNumber = "versionTest"; //String.format(res.getString(R.string.list_desc), androidVersion.getVersionNumber());
            description.setText(versionNumber);
        }
        return view;
    }
}

Here is the main_activity.java

public class MainActivity extends AppCompatActivity {

    ListView listsimple;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listsimple = findViewById(R.id.listSimple);
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("item1");
        arrayList.add("item2");
        arrayList.add("item3");
        arrayList.add("Jérôme & Dylan I-2a");

        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,arrayList);
        listsimple.setAdapter(arrayAdapter);

//       custom list -----------------------------------------------------------
        ArrayList<AndroidVersion> androidList = new ArrayList<AndroidVersion>();
        initList(androidList);
        AndroidAdapter adapter = new AndroidAdapter(this, android.R.layout.simple_list_item_1, androidList);
        final ListView list = (ListView) findViewById(R.id.customListView);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AndroidVersion selectedItem = (AndroidVersion) parent.getItemAtPosition(position);
                Log.v("CustomAdapterExemple","Selected element: " + selectedItem.getVersionName());
            }
        });
    }

Upvotes: 0

Views: 2084

Answers (1)

Ivo
Ivo

Reputation: 23277

when calling view.findViewById from a view it searches only in the subviews of that particular view. When called directly from an Activity it looks in the subviews of whatever setContentView you did before.

I assume you are initiating your adapter with the wrong viewResourceId. You should give it R.layout.android_version_layout, not simple_list_item_1.

I also think you really don't want to use that include in your xml because you want to use that layout to populate ListView and not as a single view, but that's just my guess.

Upvotes: 1

Related Questions