【发布时间】: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