动态添加行到自定义listView(每行有多个字符串)
问题描述:
我的最后一个应用程序有一个自定义布局listview
,我可以添加项目,这些项目将出现在listview
。 现在,我想创建一个应用程序,也有自定义布局,listview
,但每行都存在3个不同的字符串,所以在我的应用程序将给出一个新行之前,您必须键入3个不同的字符串,而不是他会做出新排。但是这不起作用......你能帮我解决我犯了什么错误吗?谢谢!动态添加行到自定义listView(每行有多个字符串)
主要活动
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()
,则在它们为空时传递它们会产生NPE。
因此,你需要按照以下步骤更新您的活动:
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();
}
});
}
的[动态添加项目使用Android应用自定义适配器列表视图]可能的复制(https://stackoverflow.com/questions/23939800 /动态添加项到列表视图,使用定制适配器换Android的应用程序) –