我正在尝试使用#include<stdio.h>
#include<string.h>
#include<math.h>
//practice question
int main()
{
int i,t,l,j,k,count=0;
scanf("%d",&t);
char name1[4][11];
char name2[4][11];
for(i=0;i<t;i++)
{
count=0;
for(j=0;j<4;j++)
{
scanf("%s",name1[j]);
}
for(j=0;j<4;j++)
{
scanf("%s",name2[j]);
}
for(k=0;k<4;k++)
{
for(j=0;j<4;j++)
{
l=strcmp(name1[k],name2[j]);
if(l==0)
{
count++;
break;
}
}
}
if(count>=2)
printf("similar\n");
else
printf("dissimilar\n");
}
return 0;
}
对BaseAdapter
进行编码,以便选择/取消选择多个项目。选择/取消选择项目时,我想执行特定操作。
我正在使用CheckBox
,因为我无法使OnCheckedChangedListener
工作(从未调用OnItemClickListener
方法)。现在我可以看到我的logcat上调用的OnItemClick()
方法,但是我无法取消选中 Item ,即使我多次点击一个项目,也会调用方法onCheckedChanged()
只有一次。
请注意,此代码用于在Android系统的“设置”菜单中添加新菜单(行)(可能会引入一些我不知道的限制)。 这是我的代码:
文件nfc_routing_overflow_option.xml:
onCheckedChanged()
文件nfc_routing_overflow.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/nfc_routing_overflow"
android:focusable="true"
android:clickable="false"
android:orientation="horizontal"
android:paddingLeft="24dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:background="?android:attr/selectableItemBackground">
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonOther"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checked="false"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
<ImageView
android:id="@+id/bannerOther"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:scaleType="centerInside"
/>
<TextView
android:id="@+id/textOther"
android:layout_width="wrap_content"
android:layout_height="64dp"
/>
</LinearLayout>
文件NfcRoutingOverflow.java:
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
* Copyright 2012, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<ListView
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/overflow_list"
android:paddingTop="16dp"
android:paddingBottom="16dp"/>
</LinearLayout>
有人可以帮我弄清楚我的代码有什么问题吗?
注意:暂时不使用package com.android.settings.nfc;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.android.internal.logging.MetricsLogger;
import com.android.settings.R;
import android.util.Log;
import com.android.settings.SettingsPreferenceFragment;
import android.nfc.NfcAdapter;
import android.nfc.cardemulation.ApduServiceInfo;
import android.nfc.cardemulation.CardEmulation;
import android.nfc.cardemulation.AidGroup;
import android.graphics.drawable.Drawable;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ListView;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.AdapterView;
import android.view.View.OnClickListener;
public class NfcRoutingOverflow extends SettingsPreferenceFragment{
public static final String TAG = "SETTINGSNfcRoutingOverflow";
static final boolean DBG = true;
private NfcAdapter mAdapter;
private CardEmulation mCardEmuManager;
private Context mContext;
private LayoutInflater mLayoutInflater;
private NfcOverflowAdapter mOverflowAdapter;
private ListView mListView;
class NfcOtherOverflow
{
CharSequence label;
CharSequence description;
Drawable banner;
public ComponentName componentName;
public AidGroup aidGroup;
public int uid;
boolean selected = false;
}
@Override
protected int getMetricsCategory() {
return MetricsLogger.NFC_PAYMENT;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (DBG) Log.d(TAG, "onCreate()" );
mContext = getActivity();
mAdapter = NfcAdapter.getDefaultAdapter(mContext);
mCardEmuManager = CardEmulation.getInstance(mAdapter);
mLayoutInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
mOverflowAdapter = new NfcOverflowAdapter();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (DBG) Log.d(TAG, "onViewCreated()" );
ViewGroup contentRoot = (ViewGroup) getListView().getParent();
View emptyView = getActivity().getLayoutInflater().inflate(R.layout.nfc_routing_overflow, contentRoot, false);
contentRoot.addView(emptyView);
PackageManager pm = mContext.getPackageManager();
List<ApduServiceInfo> serviceInfos =
mCardEmuManager.getServices(CardEmulation.CATEGORY_OTHER);
if (DBG) Log.d(TAG, "onViewCreated() - Nb of services in category OTHER found: " + serviceInfos.size());
mListView = (ListView) view.findViewById(R.id.overflow_list);
ArrayList<NfcOtherOverflow> appList = new ArrayList<NfcOtherOverflow>();
for(ApduServiceInfo service:serviceInfos)
{
NfcOtherOverflow appOther = new NfcOtherOverflow();
ArrayList<AidGroup> aidGroups = service.getAidGroups();
if (DBG)
Log.d(TAG, "onViewCreated() - Processing service: " + service.getDescription() + ", with " + aidGroups.size() + " AidGroup");
if(aidGroups.size() > 0)
{
for(AidGroup aidGroup : aidGroups)
{
appOther.label = service.loadLabel(pm);
if (appOther.label == null)
{
appOther.label = service.loadAppLabel(pm);
}
appOther.componentName = service.getComponent();
appOther.description = service.getDescription();
appOther.banner = service.loadBanner(pm);
appOther.aidGroup = aidGroup;
appOther.uid = service.getUid();
appList.add(appOther);
}
}
}
if (appList.size() != 0)
{
NfcOtherOverflow[] apps = appList.toArray(new NfcOtherOverflow[appList.size()]);
mOverflowAdapter.updateApps(apps, null);
}
mListView.setAdapter(mOverflowAdapter);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (DBG) Log.d(TAG, "onCreateOptionsMenu()" );
}
class NfcOverflowAdapter extends BaseAdapter implements OnCheckedChangeListener{
// Only modified on UI thread
private NfcOtherOverflow[] appInfos;
public NfcOverflowAdapter() {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - constructor" );
}
public void updateApps(NfcOtherOverflow[] appInfos, NfcOtherOverflow currentDefault) {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - updateApps() - Nb apps: " + appInfos.length);
// Clone app infos, only add those with a banner
this.appInfos = appInfos;
// notifyDataSetChanged();
}
@Override
public int getCount() {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - getCount()" );
return appInfos.length;
}
@Override
public NfcOtherOverflow getItem(int i) {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - getItem()" );
return appInfos[i];
}
@Override
public long getItemId(int i) {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - getItemId()" );
return appInfos[i].componentName.hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (DBG) Log.d(TAG, "NfcOverflowAdapter - getView() - position: " + position);
final ViewHolder holder;
NfcOtherOverflow appInfo = appInfos[position];
if (convertView == null)
{
convertView = mLayoutInflater.inflate(R.layout.nfc_routing_overflow_option, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.bannerOther);
holder.radioButton = (CheckBox) convertView.findViewById(R.id.buttonOther);
holder.textView = (TextView) convertView.findViewById(R.id.textOther);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.radioButton.setContentDescription(appInfo.label);
holder.radioButton.setTag(appInfo);
holder.radioButton.setOnCheckedChangeListener(this);
if(appInfo.banner != null)
{
holder.imageView.setImageDrawable(appInfo.banner);
}
else
{
Drawable dummy = mContext.getDrawable(R.drawable.ic_settings_nfc_payment);
holder.imageView.setImageDrawable(dummy);
}
holder.imageView.setTag(appInfo);
holder.imageView.setContentDescription(appInfo.label);
holder.textView.setContentDescription(appInfo.label);
holder.textView.setText(appInfo.aidGroup.getDescription());
AidGroup aidGroup = appInfo.aidGroup;
boolean isEnabled = aidGroup.isGroupEnabled();
if(isEnabled == true)
{
holder.radioButton.setChecked(true);
}
else
{
holder.radioButton.setChecked(false);
}
return convertView;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (DBG) Log.d(TAG, "NfcOverflowAdapter - onCheckedChanged() - isChecked: " + isChecked);
NfcOtherOverflow appInfo = (NfcOtherOverflow) buttonView.getTag();
AidGroup aidGroup = appInfo.aidGroup;
if (DBG) Log.d(TAG, "NfcOverflowAdapter - onClick() - AidGroup description: " + aidGroup.getDescription());
aidGroup.setIsEnabled(isChecked);
notifyDataSetChanged();
}
public class ViewHolder {
public CheckBox radioButton;
public ImageView imageView;
public TextView textView;
}
public void updateRouting(View view)
{
if (DBG) Log.d(TAG, "NfcOverflowAdapter - updateRouting()" );
NfcOtherOverflow appInfo = (NfcOtherOverflow) view.getTag();
AidGroup aidGroup = appInfo.aidGroup;
if (DBG) Log.d(TAG, "NfcOverflowAdapter - onCheckedChanged() - AidGroup description: " + aidGroup.getDescription());
boolean isEnabled = true;
if(((CheckBox)view).isChecked() == false)
{
isEnabled = false;
}
aidGroup.setIsEnabled(isEnabled);
//Inform system of changes
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PACKAGE_CHANGED);
intent.putExtra(Intent.EXTRA_UID, appInfo.uid);
intent.putExtra(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST, appInfo.componentName);
mContext.sendBroadcast(intent);
// /notifyDataSetChanged();
}
}
}
类的方法updateRouting()
。
答案 0 :(得分:0)
你在哪里设置onCheckChanged里面的复选框?添加radioButton.setChecked(false / true);在onCheckChanged内。
答案 1 :(得分:0)
请勿使用NfcOtherOverflow
buttonView.getTag()
尝试使用索引/位置从NfcOtherOverflow
获取appInfos
,例如appInfos[position]
holder.radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (DBG) Log.d(TAG, "NfcOverflowAdapter - onCheckedChanged() - isChecked: " + isChecked);
NfcOtherOverflow appInfo = appInfos[position];;
AidGroup aidGroup = appInfo.aidGroup;
if (DBG) Log.d(TAG, "NfcOverflowAdapter - onClick() - AidGroup description: " + aidGroup.getDescription());
aidGroup.setIsEnabled(isChecked);
notifyDataSetChanged();
}
});