2015-05-14 36 views
0

我試圖做一個spannable ListView。當你點擊ListView中的一個項目時,它應該跨越並顯示兩個按鈕。到目前爲止,我已經爲此創建了CustomAdapter。onItemClickListener只打開第一個項目的span菜單

我的問題是,當我點擊一個項目總是ListView上的第一個項目跨越。如何解決這個問題,以及如何將onClickListener設置爲這兩個按鈕。

我的ListView活動:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ListView productList=(ListView)findViewById(android.R.id.list); 
     productList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Product product= (Product) parent.getItemAtPosition(position); 
       Button CheckButton= (Button)findViewById(R.id.CheckButton); 
       Button DeleteButton=(Button)findViewById(R.id.DeleteButton); 
       CheckButton.setVisibility(View.VISIBLE); 
       DeleteButton.setVisibility(View.VISIBLE); 
      } 
     }); 


     dbhandler=new DBHandler(this,null,null,1); 
     // getProductsFromDb(); 
     adapter= new CustomAdapter(this,productnames); 
     productList.setAdapter(adapter); 

    } 
    public void addNewProduct(View view){ 
     EditText userInput=(EditText)findViewById(R.id.userInput); 
     userInput.setVisibility(View.VISIBLE); 
     String productname=userInput.getText().toString(); 

     if(productname.equals(""))return; 

     Product product=new Product(); 
     product.set_productname(productname); 
     product.set_checked(false); 
     productnames.add(product); 
     //dbhandler.addProduct(product); 
     adapter.notifyDataSetChanged(); 
     userInput.setText(""); 
     //getProductsFromDb(); 

CustomAdapter:

CustomAdapter(Context context, ArrayList<Product> productNames) { 
     super(context,R.layout.custom_list_row ,productNames); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater=LayoutInflater.from(getContext()); 
     View customView= inflater.inflate(R.layout.custom_list_row,parent,false); 

     final Product singleProduct=getItem(position); 
     TextView productName=(TextView)customView.findViewById(R.id.ProductName); 
     Button CheckButton = (Button)customView.findViewById(R.id.CheckButton); 
     Button DeleteButton = (Button)customView.findViewById(R.id.DeleteButton); 

     productName.setText(singleProduct.get_productname()); 


     return customView; 
    } 

回答

0

你不發表您的custom_list_row佈局,但我可以從你的代碼的猜測是,在佈局你有一個ViewGroup或查看(讓我們假設它是在您的代碼中顯示產品名稱的TextView),其中的項目和2個按鈕的VISIBILITY首先設置爲GONE。然後你想要的是,當用戶點擊該項目時,你將設置該項目下面的2個按鈕爲可見。以下是這樣做的一個辦法:

  1. 在productList的從活動
  2. 在你CustomAdapter設置onClickListener到產品名稱的視圖的getView()方法取出setOnItemClickListener。這樣的事情:

    TextView productName =(TextView)customView.findViewById(R.id.ProductName);

    Button CheckButton =(Button)customView.findViewById(R.id.CheckButton);

    Button DeleteButton =(Button)customView.findViewById(R.id.DeleteButton);

    productName.setOnClickListener(new OnClickListener() { 
    
    @Override 
    public void onClick(View v) { 
        CheckButton.setVisibility(View.VISIBLE); 
        DeleteButton.setVisibility(View.VISIBLE); 
    } 
    } 
    
+0

,如果我想onclickListener添加到這兩個按鈕,其中應該怎麼辦呢? – Ollikas

+0

也在CustomAdapter getView()上。那就是你在特定位置參考按鈕的地方。 –

相關問題