我使用的是帶有textview和Radiobutton的listview。我想實現單選按鈕獲取檢查ListView onItemClickListener,我該怎麼做?這是我的主要活動。我試圖做到這一點,但當我點擊listview複選框檢查多個選擇,當我滾動列表單選按鈕框損失其狀態。Radiobutton檢查點擊列表視圖onclicklistener
package com.timesheets.momentumtech.foremantimesheets.Activities;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.timesheets.momentumtech.foremantimesheets.Adapters.ListsAdapter;
import com.timesheets.momentumtech.foremantimesheets.Adapters.NamesAdapter;
import com.timesheets.momentumtech.foremantimesheets.Models.EmployeeModel;
import com.timesheets.momentumtech.foremantimesheets.Models.LitModel;
import com.timesheets.momentumtech.foremantimesheets.R;
import java.util.ArrayList;
import java.util.List;
import android.os.Handler;
public class MainActivity extends AppCompatActivity {
Handler handler;
Button setupBtn;
Button reportTimeBtn;
FloatingActionButton floatingActionButton;
private PopupWindow mpopup;
TextView cancle_popup;
NamesAdapter namesAdapter;
public static List<LitModel> litModelsList = new ArrayList<>();
List<EmployeeModel> employeeModelList = new ArrayList<>();
public static ListView employeeNames, list;
public String chek = "cost";
public static TextView scheled;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupBtn = (Button) findViewById(R.id.setup_main_btn);
setupBtn.setBackgroundColor(Color.rgb(30, 171, 225));
floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
scheled = (TextView) findViewById(R.id.scheld);
scheled.setText("Cost Code");
employeeNames = (ListView) findViewById(R.id.names);
list = (ListView) findViewById(R.id.lists);
final View popUpView = getLayoutInflater().inflate(R.layout.raw, null);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// inflating popup layout
mpopup = new PopupWindow(popUpView, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); //Creation of popup
mpopup.setAnimationStyle(android.R.style.Animation_Translucent);
mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 0, 0);
mpopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
mpopup.dismiss();
}
});
cancle_popup = (TextView) popUpView.findViewById(R.id.cancle);
cancle_popup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mpopup.dismiss();
}
});
}
});
prepareListData();
prepareList();
//prepareTime();
setupBtn = (Button) findViewById(R.id.setup_main_btn);
setupBtn.setBackgroundColor(Color.rgb(30, 171, 225));
//setupBtn.setTextColor(Color.BLACK);
reportTimeBtn = (Button) findViewById(R.id.report_main_btn);
reportTimeBtn.setBackgroundColor(Color.rgb(30, 171, 225));
setupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setupBtn.setBackgroundColor(Color.rgb(255, 254, 254));
setupBtn.setTextColor(Color.BLACK);
Intent i = new Intent(MainActivity.this, ReportTime.class);
startActivity(i);
}
});
reportTimeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
reportTimeBtn.setBackgroundColor(Color.rgb(255, 254, 254));
reportTimeBtn.setTextColor(Color.BLACK);
Intent i = new Intent(MainActivity.this, TimeEntry.class);
startActivity(i);
}
});
**list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int selected = -1;
final RadioButton radioButton = (RadioButton) view.findViewById(R.id.costcode_rb);
if (radioButton.isChecked()){
radioButton.setChecked(false);
}else {
radioButton.setChecked(true);
}
radioButton.setChecked(true);
Toast.makeText(getApplicationContext(),"Helllo",Toast.LENGTH_SHORT).show();
}
});**
}
public void prepareList() {
LitModel listModel = new LitModel("120,Poured");
litModelsList.add(listModel);
listModel = new LitModel("125,Form");
litModelsList.add(listModel);
listModel = new LitModel("130,Reber");
litModelsList.add(listModel);
listModel = new LitModel("14,018,HDPE");
litModelsList.add(listModel);
listModel = new LitModel("145,Sampl");
litModelsList.add(listModel);
listModel = new LitModel("150,Sample");
litModelsList.add(listModel);
listModel = new LitModel("160,Sample");
litModelsList.add(listModel);
listModel = new LitModel("170,Sample");
litModelsList.add(listModel);
listModel = new LitModel("171,Sample");
litModelsList.add(listModel);
listModel = new LitModel("172,Sample");
litModelsList.add(listModel);
listModel = new LitModel("172,Sample");
litModelsList.add(listModel);
listModel = new LitModel("172,Sample");
litModelsList.add(listModel);
listModel = new LitModel("172,Sample");
litModelsList.add(listModel);
listModel = new LitModel("172,Sample");
litModelsList.add(listModel);
ListsAdapter listAdapter = new ListsAdapter(MainActivity.this, litModelsList);
list.setAdapter(listAdapter);
}
private void prepareListData() {
EmployeeModel employeeModel = new EmployeeModel("All");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Steve");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Dean");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Jacob Betten");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Doug");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Dennis Peters");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Roger,StoneStone");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("JB Flet");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Justin Willis");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Bill Jones");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Roger Doug Roberts");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Roger Doug ");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Roberts");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel("Roger ");
employeeModelList.add(employeeModel);
employeeModel = new EmployeeModel(" Doug Roberts");
employeeModelList.add(employeeModel);
namesAdapter = new NamesAdapter(employeeModelList, this);
employeeNames.setAdapter(namesAdapter);
}
@Override
public void onBackPressed() {
}
}
我的適配器類別
package com.timesheets.momentumtech.foremantimesheets.Adapters;
/**
* Created by MomentumTech on 4/24/2017.
*/
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.timesheets.momentumtech.foremantimesheets.Activities.MainActivity;
import com.timesheets.momentumtech.foremantimesheets.Models.LitModel;
import com.timesheets.momentumtech.foremantimesheets.R;
import java.util.List;
import static com.timesheets.momentumtech.foremantimesheets.Activities.MainActivity.list;
import static com.timesheets.momentumtech.foremantimesheets.Activities.MainActivity.litModelsList;
//import static com.timesheets.momentumtech.foremantimesheets.Activities.MainActivity.prepareList;
import static com.timesheets.momentumtech.foremantimesheets.Activities.MainActivity.scheled;
/**
* Created by MomentumTech on 3/14/2017.
*/
public class ListsAdapter extends BaseAdapter {
private List<LitModel> litModelList;
private Context context;
private LitModel litModels;
int selected = -1;
public String chek = "cost";
public ListsAdapter(Context context, List<LitModel> litModelList) {
this.context = context;
this.litModelList = litModelList;
}
@Override
public int getCount() {
return litModelList.size();
}
@Override
public Object getItem(int i) {
return litModelList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
final Holder holder;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflater.inflate(R.layout.costcode_lv_model, null);
holder = new Holder(v);
v.setTag(holder);
} else {
holder = (Holder) v.getTag();
}
litModels = litModelList.get(i);
holder.costcode_tv.setText(litModels.getList());
if (holder.costcode_rb != null) {
holder.costcode_rb.setTag(i);
holder.costcode_rb.setChecked(i == selected);
}
if (holder.costcode_rb != null) {
holder.costcode_rb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
selected = (Integer) view.getTag();
notifyDataSetChanged();
}
});
}
return v;
}
private class Holder {
public TextView costcode_tv;
public RadioButton costcode_rb;
Holder(View v) {
costcode_tv = (TextView) v.findViewById(R.id.costcode_tv);
costcode_rb = (RadioButton) v.findViewById(R.id.costcode_rb);
// costcode_rb.setFocusable(false);
}
}
}
和XML文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/costcode_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginTop="10dp"
android:button="@drawable/radio_check" />
<TextView
android:id="@+id/costcode_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/costcode_rb"
android:layout_alignBottom="@+id/costcode_rb"
android:layout_marginLeft="15dp"
android:layout_toEndOf="@+id/costcode_rb"
android:layout_toRightOf="@+id/costcode_rb"
android:text="TextView"
android:textColor="@color/textcolor" />
<View
android:id="@+id/view3"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="@+id/costcode_tv"
android:layout_marginLeft="40dp"
android:layout_marginRight="25dp"
android:background="#a3a3a3" />
</RelativeLayout>
其中是checkBox? – Opiatefuchs
抱歉,我的錯誤是單選按鈕沒有複選框抱歉。 – user3470118
請不要只複製你的整個程序。它可能包含許多不相關的細節,讀者在嘗試重現問題時需要忽略這些細節。有關此方面的幫助,請閱讀[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – Vinzenz