我是新來的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>
問題:
就呈現出白空的活動!
具有u添加活動納入清單文件 –
是,艙單加入到質疑 –
下投票我!我是Android新手,謝謝:) –