2013-01-16 60 views
0

我是新來的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; 
    } 
} 

AndroidMani巨星

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

問題:

就呈現出白空的活動!

+0

具有u添加活動納入清單文件 –

+0

是,艙單加入到質疑 –

+0

下投票我!我是Android新手,謝謝:) –

回答

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 
+0

如果你的意思是寫這個,那麼R.layout.XML_FILE_WHICH_CONTAIN_LISTVIEW無法解析。 –

+0

@Dixit'products.xml'文件是包含'ListView'的文件。檢查OP;它已經發布... – Eric

+0

如果你的意思是包含一個listview的佈局,那麼產品佈局就有一個! –

0

您應該擴展ListActivity類。不是Activity

+0

試過,結果相同 –

+0

然後將你的ListView id改爲「@android:id/list」 –

+0

試過,結果相同:( –

0

嘗試改變:

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