有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java向自定义listView动态添加行(每行有多个字符串)

我的上一个应用程序有一个自定义布局,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;
}

共 (1) 个答案

  1. # 1 楼答案

    有两个问题:

    1. onCreate()方法中找不到voegToe。尝试对空对象调用setOnClickListener()会产生NPE
    2. 您没有初始化列表。当试图将setAdapter()调用到ListView时,getCount()调用时,当它们为null时传递它们将产生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();
            }
        });
    }