在子视图中,我希望显示合适的单个项目,但它显示所有发生的子视图中的所有必需项目!怎么解决这个? see this pic for clarity
这是我的代码,用于根据键设置标题(父项),并根据收到的密钥设置描述(子项)
Map<String, String> getdes(int keyId,int valId){
String[] key = getResources().getStringArray(keyId);
String[] des = getResources().getStringArray(valId);
Map<String, String> dict = new HashMap<String,String>();
for (int i=0, l=des.length; i<l; i++){
dict.put(key[i],des[i]);
}
return dict;
}
//this for titles
Map<String, String> getDict(int keyId, int valId) {
String[] keys = getResources().getStringArray(keyId);
String[] vals = getResources().getStringArray(valId);
Map<String, String> dict = new HashMap<String, String>();
for (int i = 0, l = keys.length; i < l; i++) {
dict.put(keys[i], vals[i]);
}
return dict;
}
private void dataOk(String res) {
FaultTitle title;
FaultDesc d1; /*= new FaultDesc("ghj");*/
Map<String, String> dtcVals = getDict(R.array.dtc_keys, R.array.dtc_values);
Map<String , String> dtcdes = getdes(R.array.dtc_keys,R.array.dtc_descrip);
if (res != null) {
for (String dtcCode : res.split("\n")) {
d1 = new FaultDesc(dtcCode, dtcdes.get(dtcCode));
des.add(d1);
Log.d("Trouble codes", dtcCode + " : " + dtcVals.get(dtcCode)+ ": "+ numberFaults);
title = new FaultTitle(dtcCode,dtcVals.get(dtcCode), des);
titles.add(title);
Log.d("TEST", titles.get(numberFaults).getFaultCode() + " -- "+ titles.get(numberFaults).getFaultTitle());
numberFaults++;
}
fault_counter.setText(numberFaults+"");
mAdapter = new FaultAdapter(getContext(),titles);
recyclerViewFaults.setAdapter(mAdapter);
} else {
//TODO NO FAULTS
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
remoteDevice = prefs.getString(ConfigActivity.BLUETOOTH_LIST_KEY, null);
if (remoteDevice == null || "".equals(remoteDevice)) {
Log.e(TAG, "No Bluetooth device has been selected.");
mHandler.obtainMessage(NO_BLUETOOTH_DEVICE_SELECTED).sendToTarget();
} else {
gtct = new GetTroubleCodesTask();
gtct.execute(remoteDevice);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_faults, container, false);
recyclerViewFaults = (RecyclerView) view.findViewById(R.id.recyclerviewFaults);
fault_counter = (TextView) view.findViewById(R.id.fault_counter);
mAdapter = new FaultAdapter(view.getContext(),titles);
recyclerViewFaults.setAdapter(mAdapter);
recyclerViewFaults.setLayoutManager(new LinearLayoutManager(view.getContext()));
return view;
}
这是Adapter类:
public class FaultAdapter extends ExpandableRecyclerAdapter<FaultTitleViewHolder,FaultDescViewHolder> {
private LayoutInflater mInflator;
private List<FaultTitle> mTitleList;
private List<FaultDesc> mChildList;
public FaultAdapter(Context context, @NonNull List<FaultTitle> parentItemList) {
super(parentItemList);
mTitleList = parentItemList;
mInflator = LayoutInflater.from(context);
}
@Override
public FaultTitleViewHolder onCreateParentViewHolder(@NonNull ViewGroup parentViewGroup) {
View movieCategoryView = mInflator.inflate(R.layout.view_fault_title, parentViewGroup, false);
return new FaultTitleViewHolder(movieCategoryView);
}
@Override
public FaultDescViewHolder onCreateChildViewHolder(@NonNull ViewGroup childViewGroup) {
View moviesView = mInflator.inflate(R.layout.view_fault_desc, childViewGroup, false);
return new FaultDescViewHolder(moviesView);
}
@Override
public void onBindParentViewHolder(FaultTitleViewHolder faultTitleViewHolder, int position, @NonNull ParentListItem parentListItem) {
FaultTitle faultTitle = (FaultTitle) parentListItem;
faultTitleViewHolder.bind(faultTitle);
}
@Override
public void onBindChildViewHolder(FaultDescViewHolder faultDescViewHolder, int position, @NonNull Object childListItem) {
FaultDesc faultDesc = (FaultDesc) childListItem;
faultDescViewHolder.bind(faultDesc);
}
}
这是标题(父项)和描述(子项)的getter类
public class FaultTitle implements ParentListItem{
private String mFaultCode, mFaultTitle;
private List<FaultDesc> mFaultDesc;
public FaultTitle(String mFaultCode, String mFaultTitle, List<FaultDesc> mFaultDesc) {
this.mFaultCode = mFaultCode;
this.mFaultTitle = mFaultTitle;
this.mFaultDesc = mFaultDesc;
}
public String getFaultCode() {
return mFaultCode;
}
public String getFaultTitle() {
return mFaultTitle;
}
@Override
public List<FaultDesc> getChildItemList() {
return mFaultDesc;
}
public List<FaultDesc> getmFaultDesc() {
return mFaultDesc;
}
@Override
public boolean isInitiallyExpanded() {
return false;
}
}
public class FaultDesc {
private String faultDesc, mFaultCode;
public FaultDesc(String dtccode, String faultDesc) {
this.faultDesc = faultDesc;
this.mFaultCode = dtccode;
}
public FaultDesc(String faultDesc) {
this.faultDesc = faultDesc;
}
public String getFaultDesc() {
return faultDesc;
}
public String getmFaultCode() {
return mFaultCode;
}
}
这是我使用的数组:
<string-array name="dtc_descrip">
<item>Wiring, regulator control solenoid. </item>
<item>Wiring, regulator control solenoid. </item>
<item>Wiring short to earth, regulator control solenoid. </item>
<item>Wiring open circuit/short to positive, regulator control solenoid. </item>
<item>Wiring open circuit, fuel shut -off valve. </item>
</string-array>
<string-array name="dtc_keys">
<item>P0001</item>
<item>P0002</item>
<item>P0003</item>
<item>P0004</item>
<item>P0005</item>
</string-array>
<string-array name="dtc_values">
<item>Fuel Volume Regulator Control Circuit/Open</item>
<item>Fuel Volume Regulator Control Circuit Range/Performance</item>
<item>Fuel Volume Regulator Control Circuit Low</item>
<item>Fuel Volume Regulator Control Circuit High</item>
<item>Fuel Shutoff Valve A Control Circuit/Open</item>
</string-array>