2017-06-01 117 views
-2

我試圖顯示產品列表,但是當我運行應用程序時,它確實在設備上顯示任何東西。我無法弄清楚我的問題在哪裏。我想在我的MainActivity.java上填充數據。當我運行應用程序時,Android ListView不顯示任何東西

MainActivity.java

public class MainActivity extends AppCompatActivity { 

// ProductsListAdapter productsListAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     ListView productlistview = (ListView) findViewById(R.id.productlistview); 
//  productsListAdapter = new ProductsListAdapter() 
//  productlistview.setAdapter(productsListAdapter); 

    } 

    private List<Product> createListofProducts(){ 
     ArrayList<Product> productArrayList = new ArrayList<Product>(); 

     productArrayList.add(new Product("Kimbo", 50.0, 0, " ")); 

     productArrayList.add(new Product("Sugar", 60.0, 0, " ")); 

     productArrayList.add(new Product("Oil", 70.0, 0, " ")); 

     productArrayList.add(new Product("Bread", 40.0, 0, " ")); 

     productArrayList.add(new Product("water", 30.0, 0, " ")); 

     return productArrayList; 
    } 
} 

ProductListAdapater.java

public class ProductsListAdapter extends BaseAdapter { 

    //reference of activity 
    Context context; 
    List<Product> productList; 

    public ProductsListAdapter(Context context, List<Product> productList) { 
     this.context = context; 
     this.productList = productList; 
    } 



    //tells you how many items ARE in a list 
    @Override 
    public int getCount() { 
     return productList.size(); 
    } 

    //tells the item that has been clicked at a particular position 
    @Override 
    public Object getItem(int position) { 
     return productList.get(position); 
    } 

    //tells the position of an item 
    @Override 
    public long getItemId(int position) { 
     return position; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater layoutInflater = LayoutInflater.from(context); 

     convertView = layoutInflater.inflate(R.layout.product_list_item,null); 
     Product currentproduct = productList.get(position); 

     TextView tvProductName = (TextView) convertView.findViewById(R.id.tvproductname); 
     TextView tvPrice = (TextView) convertView.findViewById(R.id.tvprice); 
     TextView btnAddToCart = (TextView) convertView.findViewById(R.id.buttonAddToCart); 
     //setText only takes string 
     tvProductName.setText(currentproduct.name); 
     tvPrice.setText(String.valueOf(currentproduct.price)); 


     return convertView; 
    } 
} 
+2

你從來沒有設置適配器...發表評論的行...並使用正確的構造器 – Selvin

+0

不要忘記調整列表視圖。 – Jer

+0

@謝爾文,謝謝。我不知道如何設置適配器,導致我嘗試的所有錯誤。我不知道我只需要添加(這,createListofProducts())。 – Olal

回答

1

如何類似的東西?

ListView productlistview = (ListView) findViewById(R.id.productlistview); 
productsListAdapter = new ProductsListAdapter(this, createListofProducts()) 
productlistview.setAdapter(productsListAdapter); 
+0

謝謝@Prexx,我只需要添加(this,createListofProducts()) – Olal

+0

你能接受我的答案嗎?:)謝謝 – Prexx