[英]How to build custom ListView in which each row has rectangles and circles
[英]Dynamically add rows to a custom listView ( each row has multiple Strings )
我的最后一個應用程序有一個自定義布局listview
,我可以添加項目,這些項目將出現在listview
。 現在,我想制作一個應用程序,也有自定義布局listview
,但每一行都存在 3 個不同的字符串,所以在我的應用程序給出一個新行之前,你必須輸入 3 個不同的字符串,然后他會創建一個新的排。 但這不起作用......你能幫我哪里出錯嗎? 已經謝謝了!
主要活動
ListView ListView ;
EditText editTextMerk ;
EditText editTextBeschrijving ;
EditText editTextKm ;
Button voegToe ;
ArrayList<String> merk;
ArrayList<String> beschrijving ;
ArrayList<String> km ;
CustomAdapter adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextMerk = (EditText) findViewById(R.id.editTextMerk) ;
editTextBeschrijving = (EditText) findViewById(R.id.editTextBeschrijving) ;
editTextKm = (EditText) findViewById(R.id.editTextKm) ;
ListView =(ListView) findViewById(R.id.Listview) ;
adapter = new CustomAdapter(this , merk , beschrijving , km ) ;
ListView.setAdapter(adapter);
voegToe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
merk.add(editTextMerk.getText().toString()) ;
beschrijving.add(editTextBeschrijving.getText().toString()) ;
km.add(editTextKm.getText().toString()) ;
adapter.notifyDataSetChanged();
}
});
}
這是我的 CustomAdapter 類
LayoutInflater mInflater ;
ArrayList<String> merk;
ArrayList<String> beschrijving ;
ArrayList<String> km ;
public CustomAdapter(Context c, ArrayList<String> merk, ArrayList<String> beschrijving, ArrayList<String> km) {
this.merk = merk;
this.beschrijving = beschrijving;
this.km = km;
mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
}
@Override
public int getCount() {
return merk.size();
}
@Override
public Object getItem(int position) {
return merk.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = mInflater.inflate(R.layout.layout_row, null) ;
TextView brand = (TextView) v.findViewById(R.id.textViewMerk) ;
TextView discription = (TextView) v.findViewById(R.id.textViewBeschrijving) ;
TextView distance = (TextView) v.findViewById(R.id.textViewKm) ;
brand.setText(merk.get(position));
discription.setText(beschrijving.get(position));
distance.setText(km.get(position));
return v;
}
有兩個問題:
onCreate()
方法中找到voegToe
。 嘗試在空對象上調用setOnClickListener()
會產生 NPE。setAdapter()
為 ListView 時調用getCount()
時,在它們為 null 時傳遞它們會產生 NPE。因此,您需要按如下方式更新您的 Activity:
ListView listView;
...
ArrayList<String> merk = new ArrayList<>();
ArrayList<String> beschrijving = new ArrayList<>();
ArrayList<String> km = new ArrayList<>();
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextMerk = (EditText) findViewById(R.id.editText1);
editTextBeschrijving = (EditText) findViewById(R.id.editText2);
editTextKm = (EditText) findViewById(R.id.editText3);
voegToe = (Button) findViewById(R.id.button);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomAdapter(this, merk, beschrijving, km);
listView.setAdapter(adapter);
voegToe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
merk.add(editTextMerk.getText().toString());
beschrijving.add(editTextBeschrijving.getText().toString());
km.add(editTextKm.getText().toString());
adapter.notifyDataSetChanged();
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.