[英]Dynamically add rows to a custom listView ( each row has multiple Strings )
My last app had a custom layout, listview
, I could add items and those items would appear in the listview
.我的最后一个应用程序有一个自定义布局listview
,我可以添加项目,这些项目将出现在listview
。 Now, I want to make an app, also with custom layout, listview
, but every row exist of 3 different Strings, so before my app will give a new row, you have to type in 3 different strings and than he will make a new row.现在,我想制作一个应用程序,也有自定义布局listview
,但每一行都存在 3 个不同的字符串,所以在我的应用程序给出一个新行之前,你必须输入 3 个不同的字符串,然后他会创建一个新的排。 But this doesn't work... Can you help me where I made a mistake?但这不起作用......你能帮我哪里出错吗? thanks already!已经谢谢了!
Main Activity主要活动
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();
}
});
}
This is my CustomAdapter Class这是我的 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;
}
There are two problems:有两个问题:
voegToe
in onCreate()
method.您没有在onCreate()
方法中找到voegToe
。 Trying to call setOnClickListener()
on a null object produces NPE.尝试在空对象上调用setOnClickListener()
会产生 NPE。getCount()
called when you try to setAdapter()
to your ListView.当您尝试将setAdapter()
为 ListView 时调用getCount()
时,在它们为 null 时传递它们会产生 NPE。Therefore, you need to update your Activity as follows:因此,您需要按如下方式更新您的 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.