android - Android 中的自定义 ListView,图标和文本不起作用

标签 android list listview text icons

我不是 Java/Android 方面的专家,但我仍在努力。

我只是想知道你如何制作带有图标和文本的自定义 ListView。

list (listView1) 是我想要图标的 ListView...

这是我的代码:

list = (ListView) findViewById(R.id.listView1);
    adapter = new ArrayAdapter<String>(this,
            R.layout.custom,
            R.id.app_name,
            listItems);
    list.setAdapter(adapter);

    list2 = (ListView) findViewById(R.id.listView2); adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems2); list2.setAdapter(adapter2);

    Directory directory = new Directory(Environment.getExternalStorageDirectory()); File[] files = directory.getFiles("*.apk", true);
    for (int i = 0; i < files.length; i++) {
        String apkPath = files[i].getPath() + files[i].getName();
        listItems2.add(apkPath); adapter.notifyDataSetChanged();
        PackageManager pm = getPackageManager();
        PackageInfo    pi = pm.getPackageArchiveInfo(apkPath, 0);
        pi.applicationInfo.sourceDir       = apkPath;
        pi.applicationInfo.publicSourceDir = apkPath;
        String AppName = (String)pi.applicationInfo.loadLabel(pm);
        //Drawable APKicon = pi.applicationInfo.loadIcon(pm);

        //<Here i get error when i try to change icon.>
        ImageView icon = (ImageView) findViewById(R.id.app_icon);
        icon.setImageResource(R.drawable.about);
        //<>

        listItems.add(AppName);
    }

这是我的“custom.xml”代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" >

<ImageView android:id="@+id/app_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" />
<TextView android:id="@+id/app_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/app_icon" android:text="" android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

最佳答案

好的,为了更好地理解如何实现自定义 ListView ,我建议您查看此 tutorial .现在,寻找您的问题。第一个问题出现是因为您正在使用 ArrayAdapter 制作动态列表。查看从教程中检索到的解释。

Input for ArrayAdapter The ArrayAdapter class can handle any Java object as input. It maps the data of this input to a TextView in the layout. You can define one in the constructor otherwise the android.R.id.text1 ID will be used.

ArrayAdapter uses the toString() method of the data input object to determine the String which should be displayed.

Add and remove data The ArrayAdapter class allows to remove all elements in its underlying data structure with the clear() method call. You can then add new elements via the add() method or a Collection via the addAll() method.

You can also directly modify the underlying data structure and call the notifyDataSetChanged() method on the adapter to notify him about the changes in data.

Warning If you want to change the data in your adapter the underlying data structure must support this operation. This is for example the case for the ArrayList class but not for arrays.

因此,您的第一个问题是,在将阵列设置到适配器上后,您正在更改/填充阵列。你应该改变这个。

关于在每一行放一个图标。为此,您必须编写自定义适配器。例如,您可以扩展自定义适配器来执行此操作。

同样,从教程中获取。

Developing a custom Adapter To control the data assignment and to support this assignment to several Views, you create your own Adapter implementation. For this you would extend an existing adapter implementations or by sub-classing the BaseAdapter class directly.

ListView calls the getView() method on the adapter for each data element. In this method the adapter determines the layout of the row and how the data is mapped to the Views in this layout.

This root of the layout is typically a ViewGroup (LayoutManager) and contains several other Views, e.g. an ImageView and a TextView.

所以,下面的代码实现了一个自定义的 listView。在每一行我们有两个 TextView 和图标。看看(你可以看到完整的例子 here(Example CustomListView) ):

结果:

Final Result

列表布局.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <ListView android:id="@android:id/list" android:layout_width="match_parent"
 android:layout_height="match_parent" android:background="#EEEEEE"/>

 <TextView android:id="@android:id/empty" android:layout_width="match_parent"
 android:layout_height="wrap_content" android:background="#0000FF"
 android:text="@string/list_is_empty"/>  

</LinearLayout>

two_line_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ImageView android:id="@+id/icon" android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

 <LinearLayout 
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
  >

    <TextView android:id="@+id/text1" android:layout_width="match_parent"
  android:layout_height="wrap_content" android:text="Texto 1"/>

<TextView android:id="@+id/text2" android:layout_width="match_parent"
  android:layout_height="wrap_content" android:text="Descricao"/>

 </LinearLayout>

</LinearLayout>

ListWithIcon.java

public class ListWithIcon extends ListActivity {

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

    String[] dados = {"Item 1", "Item 2", "Item 3",
            "Item 4", "Item 5", "Item 6", "Item 7"};

    String[] dados2 = {"desc 1", "desc 2", "desc 3",
            "desc 4", "desc 5", "desc 6", "desc 7"};

    MyAdapter myAdapter = new MyAdapter(this, dados, dados2); 

    setListAdapter(myAdapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Toast.makeText(this, l.getItemAtPosition(position).toString(),
            Toast.LENGTH_SHORT).show();
}

我的适配器.java

public class MyAdapter extends BaseAdapter {

private String[] data;
private String[] data2;
private Context context;

public MyAdapter(Context context, String[] data1, String[] data2) {
    super();
    this.data = data1;
    this.data2 = data2;
    this.context = context;
}

@Override
public int getCount() {
    return data.length;
}

@Override
public Object getItem(int position) {
    return data[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = LayoutInflater.from(context).
            inflate(R.layout.two_line_icon, parent, false);

    TextView text1 = (TextView) rowView.findViewById(R.id.text1);
    TextView text2 = (TextView) rowView.findViewById(R.id.text2);
    ImageView icon = (ImageView) rowView.findViewById(R.id.icon);

    text1.setText(data[position]);
    text2.setText(data2[position]);
    icon.setImageResource(R.drawable.ic_launcher);

    return rowView;
}

}

关于android - Android 中的自定义 ListView,图标和文本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16777078/

相关文章:

android - 谷歌地图 API 位置更新

python - 您可以将选择器列表写入 SQL 中的一列吗?

android - 在android中更改 ListView 内文本的颜色

R:如何将由行索引列表表示的稀疏二进制矩阵转换为列索引列表

python - 字典列表中的单个列表

安卓 : What's the best way to display an article with images?

android - 使用自定义 ArrayAdapter 在 ListView 中删除带有按钮的项目

android - 将 wgs 84 转换为纬度/经度

android - 在 Android 中与 WebView 交互

Android Retrofit 2 等待多个请求