0

创建自定义列表视图作为一个库,我们要创建一个使用适配器列表视图,我们要设置适配器外的列表项的属性。在Android电子

例如,列表视图包含200行和14列,然后我们需要使用适配器创建列表项。这里适配器中的对象是基于屏幕中显示的项目创建的。

创建适配器后,我们想要把外部适配器放在getter中,setter对于项目198意味着。如果最初在设备中最多10个项目被diplayed意味着然后10个项目对象在适配器中创建,但其余的创建时用户向下滚动

所以空指针execption被arised该项目198


我想创建一个ListView为自定义component.In用户可以添加任意数量的行,任意列数,等
任何项目作为列表视图等。


我的目标是创建库列表视图。在该列表视图中,用户可以添加任意数量的行,任意数量的列,任何项目的TextView,微调,etc.I需要建议如何实现

欢迎各界人士来给他们的想法。

+0

请发表您的代码。这与android-2.3有什么关系? – Tushar 2013-04-04 06:03:46

+0

你想从本地实现一些库或代码来创建一个库项目吗? – Nezam 2013-04-04 06:16:19

回答

0

尝试使用: 你的XML列表文件item.xml:

<LinearLayout> 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 
<CheckBox 
android:id="@+id/cbBox" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
</CheckBox> 
<LinearLayout 
android:id="@+id/linearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_marginLeft="5dp" 
android:layout_weight="1" 
android:orientation="vertical"> 
<TextView 
android:id="@+id/tvDescr" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginTop="5dp" 
android:text="" 
android:textSize="20sp"> 
</TextView> 
<TextView 
android:id="@+id/tvPrice" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="right" 
android:layout_marginRight="10dp" 
android:text=""> 
</TextView> 
</LinearLayout> 
<ImageView 
android:id="@+id/ivImage" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/ic_launcher"> 
</ImageView> 
<LinearLayout/> 

而且你的类适配器

public class BoxAdapter extends BaseAdapter { 
Context ctx; 
LayoutInflater lInflater; 
ArrayList<Product> objects; 

BoxAdapter(Context context, ArrayList<Product> products) { 
ctx = context; 
objects = products; 
lInflater = (LayoutInflater) ctx 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 
return objects.size(); 
} 

@Override 
public Object getItem(int position) { 
return objects.get(position); 
} 

@Override 
public long getItemId(int position) { 
return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
View view = convertView; 
if (view == null) { 
view = lInflater.inflate(R.layout.item, parent, false); 
} 

Product p = getProduct(position); 
((TextView) view.findViewById(R.id.tvDescr)).setText(p.name); 
((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + ""); 
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image); 

CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox); 
cbBuy.setOnCheckedChangeListener(myCheckChangList); 
cbBuy.setTag(position); 
cbBuy.setChecked(p.box); 
return view; 
} 

Product getProduct(int position) { 
return ((Product) getItem(position)); 
} 

ArrayList<Product> getBox() { 
ArrayList<Product> box = new ArrayList<Product>(); 
for (Product p : objects) { 
if (p.box) 
box.add(p); 
} 
return box; 
} 

OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() { 
public void onCheckedChanged(CompoundButton buttonView, 
    boolean isChecked) { 
getProduct((Integer) buttonView.getTag()).box = isChecked; 
} 
}; 
} 

和你的产品

public class Product { 
String name; 
int price; 
int image; 
boolean box; 

Product(String _describe, int _price, int _image, boolean _box) { 
name = _describe; 
price = _price; 
image = _image; 
box = _box; 
} 
} 

类别以及您的主要活动使用创建适配器

boxAdapter = new BoxAdapter(this, products); 
ArrayList<Product> products = new ArrayList<Product>(); 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState) 
... 
//create adapter 
fillData() 
boxAdapter = new BoxAdapter(this, products); 

// use lists 
ListView lvMain = (ListView) findViewById(R.id.lvMain); 
lvMain.setAdapter(boxAdapter); 
} 

// data for adapter 
void fillData() { 
for (int i = 1; i <= 20; i++) { 
products.add(new Product("Product " + i, i * 1000, R.drawable.ic_launcher, false)); 
} 
+0

ZHARIK86你的解释对我来说是非常有用的。但我的目标是为列表视图创建库。在该列表视图中,用户可以添加任意数量的行,任意数量的列,任何项目textview,微调器等。给出建议实现它 – 2013-04-22 04:49:10

+0

SIVAKUMAR.J,据我了解的任务,有必要使例如左右。例如,如果您希望用户定义列表外观,则可以默认创建一个外观,并将其用作LayoutInflater。此外,我们在某处设置了我们将AlertDialog打开的事件(或者FrameDialog,因为它更方便)。在这个对话框中,我们显示了我们的原始位置,并按照我们添加其他元素的列表显示。 – zharik86 2013-04-23 20:02:53

+0

SIVAKUMAR.J,有必要不要忘记删除不适合我们的元素。在用户决定使用getWindow()之后,我们使用它的存储。剩余操作类似于上面已经给出的操作。 – zharik86 2013-04-23 20:04:27