2016-06-02 5 views
0
genannt

Ich versuche, eine Toast Nachricht zu machen, wenn ich aus der Liste ein Element klicken, Aus irgendeinem Grund, wenn ich das Element passiert nichts klicken,OnItemClickListener nicht funktioniert, wird OnItemClick nicht

Ich hoffe, können Sie hilf mir, danke.

ProductList.java

import android.app.Activity; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.support.v4.app.FragmentTransaction; 
import android.os.Bundle; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.SparseBooleanArray; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.support.v4.app.FragmentManager; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 


public class ProductList extends AppCompatActivity { 


    private List<myProductsView> myProducts_types = new ArrayList<myProductsView>(); 
    ArrayAdapter<myProductsView> adapter; 

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

     //Button btnDel = (Button) findViewById(R.id.btnDel); 
     //adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice,list); 
     populateProductsList(); 
     populateListView(); 


    } 

    private void populateProductsList() { 
     myProducts_types.add(new myProductsView("aaa", "aaa", 1111, 12.90, R.drawable.cereal, 1)); 
     myProducts_types.add(new myProductsView("aaa", "aaaaaa ", 1112, 10.90, R.drawable.cereal, 2)); 
     myProducts_types.add(new myProductsView("aaa", "aaa", 1112, 30.00, R.drawable.cereal, 1)); 
     myProducts_types.add(new myProductsView("aaa", "aaa", 1112, 20.00, R.drawable.cereal, 3)); 

    } 

    private void populateListView() { 
     adapter = new MyListAdapter(); 
     ListView list = (ListView) findViewById(R.id.product_list); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       System.out.println("BLALBALBLALBLABLAL"); 
       Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT); 
      } 
     }); 

    } 
    public void StartCalck(View view){ 
     Intent intent = new Intent(ProductList.this, SplitBuying.class); 
     startActivity(intent); 
    } 

    public class MyListAdapter extends ArrayAdapter<myProductsView>{ 



     public MyListAdapter(){ 
      super(ProductList.this, R.layout.pop_productlist, myProducts_types); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      //make sure we have a view to work with(may have been given null 
      View itemView = convertView; 

      if(itemView == null){ 
       itemView = getLayoutInflater().inflate(R.layout.product_item_view, parent, false); 
      } 
      //we need to populate the list 
      //find the product to work with 
      myProductsView currentProduct = myProducts_types.get(position); 

      //fill the view 
      CheckBox checkBox = (CheckBox)itemView.findViewById(R.id.product_checkBox); 
      checkBox.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        Toast.makeText(ProductList.this,"BLA",Toast.LENGTH_SHORT); 
       } 
      }); 

      TextView productname = (TextView) itemView.findViewById(R.id.product_name); 
      productname.setText(currentProduct.getProductName()); 

      EditText quantity = (EditText)itemView.findViewById(R.id.edit_text); 
      quantity.setText(String.valueOf(currentProduct.getQuantity())); 





      return itemView; 
     } 




    } 






} 

product_item_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="2"> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingBottom="7dp" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/btn_minus" 
       android:layout_width="35dp" 
       android:layout_height="40dp" 
       android:text="-" /> 

      <EditText 
       android:id="@+id/edit_text" 
       android:layout_width="40dp" 
       android:layout_height="wrap_content" 
       android:inputType="number" 
       android:gravity="center" 
       android:focusable="false" 
       android:text="0" /> 

      <Button 
       android:id="@+id/btn_plus" 
       android:layout_width="35dp" 
       android:layout_height="40dp" 
       android:text="+" /> 
     </LinearLayout> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="Text" 
      android:textSize="15dp" 
      android:textIsSelectable="true" 
      android:gravity="center" 
      android:id="@+id/product_name" 
      android:layout_alignParentTop="true" 
      android:layout_toLeftOf="@+id/product_checkBox" 
      android:layout_toStartOf="@+id/product_checkBox" /> 

     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:padding="5dp" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      android:id="@+id/product_checkBox" /> 

     </RelativeLayout> 

    </LinearLayout> 

</LinearLayout> 

pop_productlist.xml

<?xml version="1.0" encoding="utf-8"?> 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:background="@drawable/ightwall" 
    android:id="@+id/drawerlayout"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="הרשימה שלי" 
      android:textAlignment="center" 
      android:textSize="30dp" /> 


    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="2"> 




    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <ListView 
      android:id="@+id/product_list" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:choiceMode="multipleChoice" 
      android:layout_below="@+id/ChooseStore" 
      > 
     </ListView> 

    </LinearLayout> 


    </LinearLayout> 
     <Button 
      android:id="@+id/CalckButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/calckButton" 
      android:onClick="StartCalck" 
      android:layout_gravity="center" /> 

     <Button 
      android:id="@+id/btnDel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:text="@string/lblBtnDel" 
      /> 



    </LinearLayout> 




    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="#D5D4D4" 
     android:layout_gravity="bottom"> 

     <ImageView 
      android:id="@+id/mylist" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/createlist_mylist" 
      android:adjustViewBounds="true" 
      android:maxHeight="90dp" 
      android:maxWidth="90dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" /> 

     <ImageView 
      android:id="@+id/freeadd" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/createlist_freetext" 
      android:adjustViewBounds="true" 
      android:maxHeight="90dp" 
      android:maxWidth="90dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" /> 



     <ImageView 
      android:id="@+id/favproductadd" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/createlist_favproduct" 
      android:adjustViewBounds="true" 
      android:maxHeight="90dp" 
      android:maxWidth="90dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" /> 



    </LinearLayout> 


</LinearLayout> 

Antwort

3

Sie müssen auf Ihrem Toast .show() anrufen.

Änderung

Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT); 

zu

Toast.makeText(ProductList.this, "BLA", Toast.LENGTH_SHORT).show();