【问题标题】:How to increase count of TextView in Listview item如何增加 Listview 项目中 TextView 的数量
【发布时间】:2016-08-05 15:45:39
【问题描述】:

您好,我有一个应用程序可以跟踪舞蹈的数量和舞者的可用性。我有一个带有自定义适配器的列表视图,它显示了舞者的名字和一个“开始”按钮。单击开始按钮时:开始按钮被移除并替换为“停止”按钮和 Counterview。这就是我想要完成的事情。我想这样做,如果已经为舞者按下了开始按钮并且他们目前正在跳舞。我想在活动布局中添加一个“+”按钮,单击时应在每个可见的反视图中添加“1”。添加舞蹈并选择停止舞蹈按钮后,我会将该号码发送到数据库进行记录。

如何让按钮在每个列表视图项的计数器视图(或 TextView)中添加“1”?谢谢。

item_services.xml(列表视图布局)

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

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#3d87d5">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:id="@+id/tvName"
        android:layout_weight="1"
        android:layout_gravity="center_vertical" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvAvail"
        android:layout_gravity="center_vertical"
        android:layout_weight=".1"
        android:src="@drawable/online"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/feature_button"
        android:id="@+id/button5"
        android:layout_gravity="center_vertical|right" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/start_dance"
        android:id="@+id/button4"
        android:layout_gravity="center_vertical"
        android:background="#005906"
        android:layout_weight="1"
         />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="00"
        android:id="@+id/textView10"
        android:visibility="gone"
        android:layout_weight="1" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/stop_dance"
        android:id="@+id/button6"
        android:background="#ab0000"
        android:visibility="gone"
        android:layout_weight="1" />

</LinearLayout>

DanceAdapter.java

static public Integer count=1;
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        final OneDancer oneDancer = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {

    //VIP/Dances Adapter
                else if(type==3) {

                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_services, parent, false);

                    final Button button4 = (Button)convertView.findViewById(R.id.button4);

                    final TextView counterTextView = (TextView)convertView.findViewById(R.id.textView10);

                    final Button button6 = (Button)convertView.findViewById(R.id.button6);

                    final Button button5 = (Button)convertView.findViewById(R.id.button5);



                    if(oneDancer.avail.equalsIgnoreCase("2"))
                    {
                        button4.setVisibility(View.INVISIBLE);
                        button6.setVisibility(View.VISIBLE);
                        counterTextView.setVisibility(View.VISIBLE);
                    }

    if (counterTextView.getVisibility() == View.VISIBLE) {
                counterTextView.setText(Integer.toString(count));
                Log.v("DancerAdapter","counterText visible");
            }
                    //Start Dance Button

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

                            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                    getContext());

                            // set title
                            alertDialogBuilder.setTitle("Confirm - Start Dance?");

                            // set dialog message
                            alertDialogBuilder
                                    .setMessage("Confirm Start New dance for " + oneDancer.name + "?")
                                    .setCancelable(false)
                                    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,int id) {
                                            // if this button is clicked, close
                                            // current activity
                                            AsyncHttpClient client = new AsyncHttpClient();
                                            RequestParams params = new RequestParams();
                                            params.put("action", "makeUnAvailable");
                                            params.put("name", oneDancer.name);

                                            Log.v("SignInActivity","Girl Made unAvailable Response Query");

                                            client.post("http://peekatu.com/", params,
                                                    new AsyncHttpResponseHandler() {
                                                        @Override
                                                        public void onSuccess(String response) {
                                                            Log.v("response", response);
                                                            //responseString = response;
                                                            //parseDancerList(response);


                                                            button4.setVisibility(View.GONE);
                                                            button5.setVisibility(View.GONE);
                                                            button6.setVisibility(View.VISIBLE);
                                                            counterTextView.setVisibility(View.VISIBLE);

                                                            if (response.indexOf("OK") > -1) {
                                                                Toast.makeText(getContext(),
                                                                        "Dance has began for " + oneDancer.name + "?",
                                                                        Toast.LENGTH_SHORT).show();



                                                            }

                                                        }

                                                        @Override
                                                        public void onFailure(Throwable error, String content) {
                                                            Log.v("response", "response failed network error");
                                                            //waitncall(true);

                                                        }

                                                    });
                                            dialog.cancel();
                                        }
                                    })
                                    .setNegativeButton("No",new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,int id) {
                                            // if this button is clicked, just close
                                            // the dialog box and do nothing
                                            dialog.cancel();
                                        }
                                    });

                            // create alert dialog
                            AlertDialog alertDialog = alertDialogBuilder.create();

                            // show it
                            alertDialog.show();


                        }
                    });


                    //Stop Dance Button

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

                            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
                            // set title
                            alertDialogBuilder.setTitle("Confirm - Stop Dance?");
                            // set dialog message
                            alertDialogBuilder
                                    .setMessage("Confirm End dance for " + oneDancer.name + "?")
                                    .setCancelable(false)
                                    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,int id) {

                                            AsyncHttpClient client = new AsyncHttpClient();
                                            RequestParams params = new RequestParams();
                                            params.put("action", "makeAvailable");
                                            params.put("name", oneDancer.name);
                                            //params.put("TimeOut", );


                                            Log.v("SignInActivity","Girl Made Available Response Query");

                                            client.post("http://peekatu.com/", params,
                                                    new AsyncHttpResponseHandler() {
                                                        @Override
                                                        public void onSuccess(String response) {
                                                            Log.v("response", response);
                                                            //responseString = response;
                                                            //parseDancerList(response);


                                                            button4.setVisibility(View.VISIBLE);
                                                            button6.setVisibility(View.GONE);
                                                            button5.setVisibility(View.VISIBLE);
                                                            counterTextView.setVisibility(View.GONE);


                                                            if (response.indexOf("OK") > -1) {
                                                                Toast.makeText(getContext(),
                                                                        "Dance has Ended for " + oneDancer.name,
                                                                        Toast.LENGTH_SHORT).show();



                                                            }

                                                        }

                                                        @Override
                                                        public void onFailure(Throwable error, String content) {
                                                            Log.v("response", "response failed network error");
                                                            //waitncall(true);

                                                        }

                                                    });
                                            dialog.cancel();
                                        }
                                    })
                                    .setNegativeButton("No",new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,int id) {
                                            // if this button is clicked, just close
                                            // the dialog box and do nothing
                                            dialog.cancel();
                                        }
                                    });

                            // create alert dialog
                            AlertDialog alertDialog = alertDialogBuilder.create();

                            // show it
                            alertDialog.show();



                        }
                    });


                //end of if(type3)
                }

static public void setMyCount(int count) {
    DancerAdapter.count = count;

    Log.v("setMyCount",DancerAdapter.count.toString());
}
}

ServicesActivity.java

Integer count=0;



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


                                               count++;
                                               DancerAdapter.setMyCount(count);
                                               adapter.notifyDataSetChanged();

                                               Log.v("addDanceButton",count.toString());


                                           }
                                       }
        );

【问题讨论】:

    标签: android listview counter android-adapter


    【解决方案1】:

    在您的public View getView() 中,您应该检查您的counterTextView 的可见性以显示您的计数:

    if (counterTextView.getVisibility() == View.VISIBLE) {
        counterTextView.setText(Integer.toString(count));
    }
    

    您需要在您的适配器中使用类似这样的东西从您的活动中传递该计数值:

    public void setMyCount(int count) {
        this.count = count;
    }
    

    由于您的计数是您按下 +Button 的次数,那么它的 onClick() 可能会做:递增、通过计数、刷新列表

    count++;
    myAdapter.setMyCount(count);
    myAdapter.notifyDataSetChanged();
    

    过程会是这样的:

    1. 点击
    2. 增量
    3. 通过次数
    4. 刷新列表,setView 运行
    5. textview 检查可见性
    6. 如果可见,textview settext

    现在您的停止按钮可以从适配器的计数中获取该值。

    【讨论】:

    • 小心...setText(int resId)不是你要调用的方法
    • 感谢您的帮助。我已经尝试了您的建议,现在我的问题是。我无法从 Activity 调用 setMyCount() 方法,因为它要求该方法是静态的,但是当我在适配器内使该方法成为静态时,我在静态方法中使用 this.count = count; 时遇到错误。
    • 我能够让它工作到一半。我现在的问题是所有counterview值都被设置为相同的值。
    • 这不是你想要的吗?所有可见的都设置为同一个计数器?如果没有,那么您需要将您的计数合并为舞者 obj 的一部分
    • 对不起,也许我不清楚。每个列表视图项上都有一个开始和停止按钮。单击开始时,计数器视图会显示并且应该上升onClick() 如果单击停止然后再次单击开始,计数器应该返回零然后再次开始计数,但其他列表视图项应保持其当前值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2020-02-05
    • 2017-05-10
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多