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