Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>ListView &amp; GridView are both descendants of AbsListView. So, in your code, refer to an instance of AbsListView. Then, as someone else suggested, use the specific layout folders to define your layouts. You will also define the specific instance of AbsListView within these layouts (ListView or GridView).</p> <p>If you define the layouts correctly with all the same element names, your code won't need to change.</p> <p><strong>EDIT: I'm not sure why you would ever write code to do something the SDK/OS will do for you. So, for others who stumble upon this, I wanted to give a full example of how to do this without having to put a hack in your code:</strong></p> <p>The full, very basic project can be found on my gitHub here: <a href="https://github.com/sberg413/abslistview-example" rel="noreferrer">https://github.com/sberg413/abslistview-example</a></p> <p>The MainActivity.java :</p> <pre><code>package com.example.abslistviewexample; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { AbsListView absListView; static String[] listItems = { "First Item", "Second Item", "Third Item", "Fourth Item", "Fifth Item", "Sixth Item", "Seventh Item", "Eight Item", "Ninth Item", "Tenth Item" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); absListView = (AbsListView) findViewById(R.id.listView1); absListView.setAdapter( new MyArrayAdapter(this, R.layout.row, listItems)); } private class MyArrayAdapter extends ArrayAdapter&lt;String&gt;{ public MyArrayAdapter(Context context, int resource, String[] values) { super(context, resource, values); } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.row, parent, false); TextView textView = (TextView) view.findViewById(R.id.textView1); ImageView imageView = (ImageView) view.findViewById(R.id.imageView1); textView.setText( getItem(position)); return view; } } } </code></pre> <p>The layout/activity_main.xml :</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" &gt; &lt;ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" &gt; &lt;/ListView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>The layout/row.xml :</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:src="@drawable/ic_launcher" /&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/imageView1" android:layout_marginLeft="50dp" android:layout_toRightOf="@+id/imageView1" android:text="TextView" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>The layout-xlarge/activity_main.xml :</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" &gt; &lt;GridView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:numColumns="3" &gt; &lt;/GridView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>The layout-xlarge/row.xml :</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" &gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:src="@drawable/ic_launcher" /&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:text="TextView" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>This is obviously a very trivial example, but you'll get the idea. Notice how the MainActivity uses an AbsListView. Within the layout xmls you specify which child class is used.</p> <p>I hope this helps someone.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload