如何使Section ListView Header不可点击?

时间:2019-07-09 07:58:22

标签: android android-studio android-listview

我有一个节列表视图,并且有一个行和标题。标头显示月份和年份,其中行显示利润和物料。我为“行”创建了一个对话框,但是每当我单击“标题”时,都会提示我错误。如何使页眉不可点击?

我的适配器类

public class TransactionAdapter extends BaseAdapter {

    ArrayList<Object> transactions;
    Context c;
    LayoutInflater inflater;
    static final int ROW = 0;
    static final int HEADER = 1;

    public TransactionAdapter(Context c, ArrayList<Object> transactions){
        this.c = c;
        this.transactions = transactions;
    }

    //Get size of the Transaction ArrayList
    @Override
    public int getCount() {
        return transactions.size();
    }

    //Get single transaction from the Transaction ArrayList
    @Override
    public Object getItem(int position) {
        return transactions.get(position);
    }

    //Get Single transaction identifier from the Transaction ArrayList
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position){
        //Check the current transaction is Transaction
        if(getItem(position) instanceof ProfitTransactions){
            return ROW;
        }
        return HEADER;
    }

    @Override
    public int getViewTypeCount(){
        return 2;
    }


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

        //Type of View which is ROW(0) or HEADER(1)
        int type = getItemViewType(position);

        //If there is no View create it,
        if (convertView == null) {
            inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            switch (type) {
                case ROW:
                    convertView = inflater.inflate(R.layout.activity_transaction_items, null);
                    break;
                case HEADER:
                    convertView = inflater.inflate(R.layout.activity_transaction_header, null);
                    convertView.setBackgroundColor(Color.rgb(220,220,220));

                default:
                    break;
            }
        }

        switch (type){
            case ROW:
                ProfitTransactions transaction = (ProfitTransactions)getItem(position);

                TextView tvDay = (TextView)convertView.findViewById(R.id.day);
                TextView tvTID = (TextView)convertView.findViewById(R.id.tID);
                TextView tvTotalPrice = (TextView)convertView.findViewById(R.id.totalPrice);
                TextView tvTimeOrder = (TextView)convertView.findViewById(R.id.tvTime);

                Log.d("transadapter","-----Test: " + Integer.parseInt(transaction.getDayOfOrder()));
                tvDay.setText(transaction.getDayOfOrder()+ getDayNumberSuffix(Integer.parseInt(transaction.getDayOfOrder())));
                tvTID.setText("TID: " + transaction.gettId());
                tvTotalPrice.setText("+$"+String.format("%.2f", transaction.getTotalPrice()));
                tvTimeOrder.setText("At: " + transaction.getTimeOfOrder());
                break;
            case HEADER:
                String header = (String)getItem(position);
                // from string to date

                SimpleDateFormat inputFormat = new SimpleDateFormat("MM/yyyy");
                Date date = null;
                try {
                    date = inputFormat.parse(header);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                // from date to string
                SimpleDateFormat outputFormat = new SimpleDateFormat("MMM yyyy");
                String dateTime = outputFormat.format(date);


                TextView tvMonthYear = (TextView)convertView.findViewById(R.id.tvHeaderMonthYear);
                tvMonthYear.setText(dateTime);

                default:
                    break;
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ProfitTransactions transaction = (ProfitTransactions)getItem(position);
                //Create the Dialog Box
                AlertDialog.Builder builder = new AlertDialog.Builder(c);

                //Put message in the Dialog Box
                builder.setMessage("Name: " + transaction.getName() + "\n" +
                        "Price: " + transaction.getPrice() + "\n" +
                        "Quantity: " + transaction.getQuantity() + "\n" +
                        "Total Price: " + transaction.getTotalPrice() + "\n"
                )

                        //If user click Yes
                        .setPositiveButton("Close", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {



                            }
                        });



                //Show the dialog after creating
                AlertDialog dialog = builder.show();
            }
        });

        return convertView;

    }

}

我想要的预期结果是我希望包含月份和年份的标题不可单击,并且不会响应任何内容。

4 个答案:

答案 0 :(得分:0)

使用相同的方式来决定要填充的内容以及如何使用getView方法填充视图,您可以将OnClickListener设置为所需类型的null。您也可以使用setClickable方法。

答案 1 :(得分:0)

在getViewMethod内部使用此

case HEADER:
                convertView = inflater.inflate(R.layout.activity_transaction_header, null);
                convertView.setBackgroundColor(Color.rgb(220,220,220));
                convertView.setEnabled(false);

答案 2 :(得分:0)

在convertView.setOnClickListener之前,添加if逻辑以检查convertView是标题还是所选的行!喜欢-

if(!convertView.equals("HEADER"))
{
your onClick listener.....
}
else
{
what you want to do if its headerview being the view selected...
}

答案 3 :(得分:0)

当前,您正在每个视图上注册点击侦听器。 您只想将其分配给行,而不是标题,因为在处理程序中,您试图访问@app.route("/") def index(): id = "Vishal" p = "vishal94" connection.execute("INSERT INTO books (username , password) VALUES(:username , :password)",{"username":id, "password":p}) dat = connection.execute("select * from books") return render_template("data.html",dat = dat) sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near ":" LINE 1: INSERT INTO books (username , password) VALUES(:username , :... ^ [SQL: INSERT INTO books (username , password) VALUES(:username , :password)] [parameters: {'username': 'Vishal', 'password': 'vishal94'}] (Background on this error at: http://sqlalche.me/e/f405) 上仅存在于行中的字段。

因此,只需将您的点击侦听器代码拉到交换机的ProcessTransaction分支中即可。

ROW