문제

I'm a beginner to Android development, but not to software development. I've been looking for tutorials on how to create dynamic ListViews with images and have had some trouble.

The tutorial I followed most closely was #2 on this link (the Custom ArrayAdapter example): http://www.mkyong.com/android/android-listview-example/

This works fine if I remove the header and footer from my layout xml file, but I really need those to be included. Below I'll post my source from my Activity java file, my custom array adapter java file, and my layout xml file as well as the output. I think there may be something I'm missing or not understanding about how the layout inflater works.

Thanks for your time.

MainActivity.java

public class MainActivity extends ListActivity {

static final String[] MAIN_MENU = 
        new String[] { "ICD-10", "EDUCATION", "NEWS", "SERVICES", "CONTACT" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(new ImageAdapter(this, MAIN_MENU));

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    //get selected items
    String selectedValue = (String) getListAdapter().getItem(position);

    // to contact
    if(selectedValue.equals(MAIN_MENU[4])) {
        contactPressed(v);
    }
    // to icd-10
    else if(selectedValue.equals(MAIN_MENU[0])) {
        startActivity(new Intent(MainActivity.this, ICDActivity.class));
    }
    // to services
    else if(selectedValue.equals(MAIN_MENU[3])) {
        startActivity(new Intent(MainActivity.this, ServicesActivity.class));
    }

}

public void  contactPressed(View v) {       
    startActivity(new Intent(MainActivity.this, ContactActivity.class));
    overridePendingTransition(R.anim.slide_in_up, R.anim.stay);
}

}

ImageAdapter.java

  public class ImageAdapter extends ArrayAdapter<String>
{
private final Context context;
private final String[] values;

public ImageAdapter(Context context, String[] values) {
    super(context, R.layout.activity_main, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.activity_main, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.lvText);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.lvImage);
    textView.setText(values[position]);

    // Change icon based on name
    String s = values[position];

    if (s.equals("ICD-10"))
    {
        imageView.setImageResource(R.drawable.icon_icd10);
    }
    else if(s.equals("EDUCATION"))
    { 
        imageView.setImageResource(R.drawable.icon_education);
    }
    else if(s.equals("NEWS"))
    {
        imageView.setImageResource(R.drawable.icon_news);
    }
    else if(s.equals("SERVICES"))
    {
        imageView.setImageResource(R.drawable.icon_services);
    }
    else if(s.equals("CONTACT"))
    {
        imageView.setImageResource(R.drawable.icon_contact);
    }

    return rowView;
}
 }

activity_main.xml

<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" >

<ImageView
    android:id="@+id/custom_background"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY"
    android:src="@drawable/gray">
</ImageView>

<RelativeLayout
    android:id="@+id/headerLayout"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_alignParentTop="true" >

    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:scaleType="fitXY"
        android:src="@drawable/logo_header" />
</RelativeLayout>

<!-- Footer aligned to bottom -->
<RelativeLayout 
    android:id="@+id/footerLayout"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="30dp" >

    <ImageView
        android:id="@+id/footer"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:scaleType="center"
        android:src="@drawable/footer" />

    <TextView
        android:id="@+id/footerText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="14dp"
        android:onClick="contactPressed"
        android:layout_marginBottom="5dp"
        android:text="@string/footerText" />

</RelativeLayout>


<ScrollView
    android:id="@+id/scrollableContents"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/footerLayout"
    android:layout_below="@id/headerLayout" >


    <LinearLayout
        android:id="@+id/linearList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/footerLayout"
        android:layout_below="@id/headerLayout"
        android:padding="5dp"
        tools:context=".MainActivity" >

        <!-- Making a dynamic ListView with images with the two attributes below -->

        <ImageView
            android:id="@+id/lvImage"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/icon_icd10" >
        </ImageView>

        <TextView
            android:id="@+id/lvText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id/lvText"
            android:textSize="25sp" >
        </TextView>

    </LinearLayout>
</ScrollView>

</RelativeLayout>

The output I get for this is: enter image description here

If I comment out the header and footer code in the xml I get: enter image description here

The goal is for it to look similar to this but with images on the right side of each line: enter image description here

Thanks for your time.

도움이 되었습니까?

해결책 2

So I found the solution to my problem while searching for another tutorial. I finally ended up using this tutorial: http://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html

My problem was that I tried to replace my ListView with a TextView and ImageView in the same layout.xml file when I should have created my TextView and ImageView in a different layout.xml file. Below is the final code and the correct output I was looking for. Thanks!

list_single.xml (below)

<?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="match_parent" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="9dp"
        android:layout_alignParentRight="true" />

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="17dp"
        android:layout_marginLeft="5dp"
        android:textSize="23sp" />

</RelativeLayout>

activity_main.xml (below)

<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" >

    <ImageView
        android:id="@+id/custom_background"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/gray">
    </ImageView>

    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:scaleType="fitXY"
        android:src="@drawable/logo_header" />

    <!-- Footer aligned to bottom -->
    <ImageView
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:scaleType="center"
        android:src="@drawable/footer" />

    <TextView
        android:id="@+id/footerText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="14dp"
        android:onClick="contactPressed"
        android:text="@string/footerText" />

    <ListView
        android:id="@+id/mainMenuList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/footer"
        android:layout_below="@id/header"
        android:divider="#000000"
        android:dividerHeight="0.1dp" />

</RelativeLayout>

ImageAdapter.java (below)

public class ImageAdapter extends ArrayAdapter<String>
{
    private final Context context;
    private final String[] values;

    public ImageAdapter(Context context, String[] values) {
        super(context, R.layout.list_single, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_single, null, true);
        TextView textView = (TextView) rowView.findViewById(R.id.txt);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        textView.setText(values[position]);

        // Change icon based on name
        String s = values[position];

        if (s.equals("ICD-10"))
        {
            imageView.setImageResource(R.drawable.icon_icd10);
        }
        else if(s.equals("EDUCATION"))
        { 
            imageView.setImageResource(R.drawable.icon_education);
        }
        else if(s.equals("NEWS"))
        {
            imageView.setImageResource(R.drawable.icon_news);
        }
        else if(s.equals("SERVICES"))
        {
            imageView.setImageResource(R.drawable.icon_services);
        }
        else if(s.equals("CONTACT"))
        {
            imageView.setImageResource(R.drawable.icon_contact);
        }

        return rowView;
    }
}

MainActivity.java (below)

public class MainActivity extends Activity {

    ListView list;
    static final String[] MAIN_MENU = 
            new String[] { "ICD-10", "EDUCATION", "NEWS", "SERVICES", "CONTACT" };

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

        list = (ListView)findViewById(R.id.mainMenuList);
        list.setAdapter(new ImageAdapter(MainActivity.this, MAIN_MENU));

        // register onClickListener to handle click events on each item
        list.setOnItemClickListener(new OnItemClickListener() {
            // argument position gives the index of item which is clicked
            public void onItemClick(AdapterView<?> arg0, View v, int position,
                    long arg3) {

                String selectedValue = MAIN_MENU[position];

                // to contact
                if(selectedValue.equals(MAIN_MENU[4])) {
                    contactPressed(v);
                }
                // to icd-10
                else if(selectedValue.equals(MAIN_MENU[0])) {
                    startActivity(new Intent(MainActivity.this, ICDActivity.class));
                }
                // to services
                else if(selectedValue.equals(MAIN_MENU[3])) {
                    startActivity(new Intent(MainActivity.this, ServicesActivity.class));
                }
            }
        });

    }

    public void  contactPressed(View v) {       
        startActivity(new Intent(MainActivity.this, ContactActivity.class));
        overridePendingTransition(R.anim.slide_in_up, R.anim.stay);
    }

}

And the final output (below) enter image description here

다른 팁

First looks like you have

android:layout_above="@id/footerLayout"
android:layout_below="@id/headerLayout"

on your @+id/scrollableContents, which is good. But you also have it on the view nested inside it, @+id/linearList which is not right. There may be other layout problems but it is hard to tell. In general this layout looks very complex for what you are trying to achieve.

But I'm not sure really what you mean by "dynamic ListView" and it looks like you are trying to re-create the functionality of a listview using a ScrollView. I don't think you should take that approach. The ListView is very flexible and you can use a custom layout for your items.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top