2017-10-20 85 views
-1

我的最後一個應用程序有一個自定義佈局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; 
} 
+0

的[動態添加項目使用Android應用自定義適配器列表視圖]可能的複製(https://stackoverflow.com/questions/23939800 /動態添加項到列表視圖,使用定製適配器換Android的應用程序) –

回答

0

有兩個問題:

  1. 您在onCreate()方法中找不到voegToe。試圖對空對象調用setOnClickListener()會產生NPE。
  2. 您未初始化您的列表。如果在您嘗試將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(); 
     } 
    }); 
} 
相關問題