Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Listview自定义ListItem模板_Java_Android_Xml_Android Listview_Adt - Fatal编程技术网

Java Listview自定义ListItem模板

Java Listview自定义ListItem模板,java,android,xml,android-listview,adt,Java,Android,Xml,Android Listview,Adt,我是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="h

我是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>
<?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

<?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>
<?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 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);
    }

}
 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;
    }
}
公共类产品扩展活动{
私有ArrayList产品=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.products);
Products=新的ArrayList();
产品。添加(新产品(“Ahmed”);
((列表视图)findViewById(R.id.Products))
.setAdapter(新产品适配器(getApplicationContext()),
R.layout.product,R.id.Name,Products));
}
公共类ProductAdapter扩展了ArrayAdapter{
私人上市产品;
公共产品适配器(上下文、int资源、,
int textViewResourceId,列出对象){
超级(上下文、资源、textViewResourceId、对象);
产品=对象;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回产品。大小();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
产品持有人;
if(convertView==null){
convertView=GetLayoutFlater()。充气(R.layout.product,
无效);
Holder=新产品Holder();
Holder.Name=(TextView)convertView.findViewById(R.id.Name);
Holder.Picture=(图像视图)convertView
.findviewbyd(R.id.Picture);
convertView.setTag(支架);
}否则{
Holder=(ProductHolder)convertView.getTag();
}
产品=产品。获取(位置);
如果(产品!=null){
Holder.Name.setText(product.getName());
}
返回视图;
}
}
公共静态类产品持有者{
公共文本视图名称;
公众图片;
}
}

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>


问题:


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

您应该扩展
ListActivity
类。不是
活动

我想,这一行您错了,您在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>
尝试更改:

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


让你将活动添加到清单文件是的,清单已添加到我!我是一个android新手,谢谢:)如果你想写这篇文章,那么包含LISTVIEW的R.layout.XML文件无法解析。@Dixit
products.XML
文件就是包含LISTVIEW的文件。检查OP;它已经发布了…如果你指的是包含listview的布局,那么products布局有一个!Dixit Patel有一点,您在
setContentView()
中使用相同的布局,并且每行的布局…@Sam-Huh?他正在对内容视图使用
R.layout.products
,对行使用
R.layout.product
,然后将您的ListView id更改为“@android:id/list”,他不需要扩展ListActivity。@AhmedGhoneim您是否尝试过将id更改为“@android:id/list”并将活动更改为ListActivity?您还必须将findviewbyd(R.id.Products)更改为findviewbyd(android.R.id.list)