【问题标题】:Listview custom ListItem templateListview 自定义 ListItem 模板
【发布时间】:2013-01-16 04:41:30
【问题描述】:

我是 Android 编程新手,

产品:

XML

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

    <ImageView
        android:id="@+id/Picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

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

        <TextView
            android:id="@+id/Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</LinearLayout>

JAVA

public class Product {
    private String Name;
    private byte[] Picture;

    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public byte[] getPicture() {
        return Picture;
    }
    public void setPicture(byte[] picture) {
        Picture = picture;
    }

    public Product(String Name) {
        this.setName(Name);
    }

}

产品:

XML

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

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

</LinearLayout>

JAVA

 public class Products extends Activity {
    private ArrayList<Product> Products = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.products);
        Products = new ArrayList<Product>();
        Products.add(new Product("Ahmed"));
        ((ListView) findViewById(R.id.Products))
                .setAdapter(new ProductAdapter(getApplicationContext(),
                        R.layout.product, R.id.Name, Products));

    }

    public class ProductAdapter extends ArrayAdapter<Product> {

        private List<Product> Products;

        public ProductAdapter(Context context, int resource,
                int textViewResourceId, List<Product> objects) {
            super(context, resource, textViewResourceId, objects);
            this.Products = objects;

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return Products.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ProductHolder Holder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.product,
                        null);
                Holder = new ProductHolder();
                Holder.Name = (TextView) convertView.findViewById(R.id.Name);
                Holder.Picture = (ImageView) convertView
                        .findViewById(R.id.Picture);
                convertView.setTag(Holder);
            } else {
                Holder = (ProductHolder) convertView.getTag();
            }
            Product product = Products.get(position);
            if (product != null) {
                Holder.Name.setText(product.getName());
            }
            return convertView;
        }
    }

    public static class ProductHolder {
        public TextView Name;
        public ImageView Picture;
    }
}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.Ghoneim.Dealer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="info.Ghoneim.Dealer.Products"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

问题:

只显示一个白色的空活动!

【问题讨论】:

  • 让你在清单文件中添加活动
  • 是的,清单已添加到问题中
  • 否决我!我是安卓新手,谢谢:)

标签: java android xml android-listview adt


【解决方案1】:

我认为,你在这一行错了,你在 setContentView() 和每一行的布局中使用了相同的布局

  setContentView(R.layout.products);

这样用

 setContentView(R.layout.XML_FILE_WHICH_CONTAIN_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" >

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

</LinearLayout>

这个xml名字在这里像

R.layout.products

【讨论】:

  • 如果你的意思是写这个,那么 R.layout.XML_FILE_WHICH_CONTAIN_LISTVIEW 无法解析。
  • @Dixit products.xml 文件是包含ListView 的文件。检查OP;它已经发布了...
  • 如果你的意思是包含一个列表视图的布局,那么产品布局就有一个!
  • Dixit Patel 有一个观点,您在 setContentView() 中使用相同的布局以及每一行的布局...
  • @Sam 嗯?他将R.layout.products 用于内容视图,将R.layout.product 用于行...
【解决方案2】:

您应该扩展ListActivity 类。不是Activity

【讨论】:

  • 然后将您的 ListView id 更改为“@android:id/list”
  • 他不需要扩展ListActivity。
  • @AhmedGhoneim 你是否尝试将你的 id 更改为“@android:id/list”并将活动更改为 ListActivity?您还必须将 findViewById(R.id.Products) 更改为 findViewById(android.R.id.list)
【解决方案3】:

尝试改变:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    相关资源
    最近更新 更多