无法弄清楚如何在Listview中显示图像

时间:2013-08-15 10:00:25

标签: java listview imageview android-studio

我看过视频,搜索堆栈溢出并试图将我学到的东西实现到我的代码中,但我无法让Listview在右边显示图像。

这是我的page1.xml(包含listview):

<?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="match_parent"
    android:orientation="vertical"
    android:background="#ffd9d9d9">


    <ListView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_gravity="center"
            android:dividerHeight="8dip"
            android:paddingTop="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingBottom="10dp"
            android:scrollbars="none"
            android:clipToPadding="false"
            />

  </LinearLayout>

这是我的自定义row.xml(运行正常)

<?xml version="1.0" encoding="utf-8"?>


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/rowtext"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:fontFamily="sans-serif-light"
          android:gravity="center_vertical"
          android:textColor="#ff858585"
          android:textSize="20sp"
          android:paddingLeft="15dp"
          android:paddingRight="15dp"
          android:minHeight="80dp"
          android:background="@drawable/card"/>

这是我的Backups.java:

package com.YvesB.tobin;


import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.File;


public class Backups extends Activity {

    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

    @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setTheme(R.style.RedTheme);
      setContentView(R.layout.page1);

          ActionBar ab = getActionBar();
          ab.setHomeButtonEnabled(true);
          ab.setDisplayHomeAsUpEnabled(true);

        ListView listView1 = (ListView) findViewById(R.id.listView);

        File directory = new File(extStorageDirectory
                + "/Android/data/com.tobin.redirect");

        String[] filenames = directory.list();



        if (filenames == null){

        }

        else{

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                   R.layout.row, filenames);

            listView1.setAdapter(adapter);

        }

  }

}

感谢您的帮助,我想起诉的png叫做drive.png!

另一方面,你们都很棒,不顾一切地帮助别人!

1 个答案:

答案 0 :(得分:0)

要做的第一件事是更改row.xml。它应该包含一个显示drive.png的ImageView。像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rowtext"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fontFamily="sans-serif-light"
        android:gravity="center_vertical"
        android:textColor="#ff858585"
        android:textSize="20sp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:minHeight="80dp"
        android:background="@drawable/card"/>

    <ImageView
        android:id="@+id/drive"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_marginLeft="5px"
        android:layout_marginRight="20px"
        android:layout_marginTop="5px"
        android:src="@drawable/drive" >
    </ImageView>

</LinearLayout>

然后你需要编写一个自定义的ArrayAdapter,类似于:

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

    public MyArrayAdapter(Context context, String[] values) {
        super(context, R.layout.row, 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.row, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.rowtext);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.drive);
        textView.setText(values[position]);

        // Change icon based on name or position
        // ...

        return rowView;
    }
}

然后您必须使用此自定义适配器而不是标准ArrayAdapter<String>

本教程中的示例(并且有点自定义):
http://www.mkyong.com/android/android-listview-example/

希望它有所帮助并做到你的想法。