如何增加Listview项中的TextView计数

时间:2016-08-05 15:45:40

标签: android listview counter android-adapter

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

如何在每个列表视图项目中为按钮(或TextView)添加“1”按钮?谢谢。

item_services.xml(Listview布局)

checkboxGroupInput("select", label = h3("3. Select genre"), 
 choices = list('Action','Fantasy','Biography', 'Fantasy', 
                 'History', 'Musical', 'Reality TV', 
                 'Sport', 'War', 'Adventure', 'Comedy', 'Drama', 
                 'Romance', 'Talk Show', 'Animation', 'Crime', 
                   'Game_show', 'Music', 'Film Noir', 'Horror', 
                  'Mystery', 'Western', 'Family', 'Sci Fi', 
                    'Thriller', 'News', 'Unknown', 'Other'), inline = TRUE), 

DanceAdapter.java

<?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>

ServicesActivity.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());
}
}

1 个答案:

答案 0 :(得分:0)

public View getView()内,您应该检查counterTextView的可见度以显示您的点数:

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

您需要在适配器中传递活动中的计数值:

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

由于您的计数来自按下+按钮的次数,因此其onClick()可能会执行:增量,通过计数,刷新列表

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

这个过程将是这样的:

  1. 点击
  2. 增量
  3. 传递计数
  4. 刷新列表,setView运行
  5. textview检查可见性
  6. 如果可见,则为textview settext
  7. 现在你的停止按钮可以从适配器的数量中获得该值。