我是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>
问题:
只是显示一个白色的空活动!
答案 0 :(得分: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
答案 1 :(得分:0)
您应该扩展ListActivity
类。不是Activity
类
答案 2 :(得分:0)
尝试改变:
<ListView
android:id="@+id/Products"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>