我試圖做一個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;
}
,如果我想onclickListener添加到這兩個按鈕,其中應該怎麼辦呢? – Ollikas
也在CustomAdapter getView()上。那就是你在特定位置參考按鈕的地方。 –