要填充我的ListView
,我使用ArrayAdapter<String>
,声明如下:
myListAdapter =new ArrayAdapter<String>(getActivity(),R.layout.myView,R.id.myTextView);
。
表示我的视图布局的xml文件是:
<CheckedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/DescriptionEtape"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/textCheckMark"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:checked="false"/>
为了对用户的点击做出反应,我添加了:(ListView)FragmentView.findViewById(R.id.myListView)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View vue, int position, long id) {
((CheckedTextView)vue.findViewById(R.id.myTextView)).setChecked(!((CheckedTextView)vue.findViewById(R.id.myTextView)).isChecked());
}
});
当我触摸onItemClick
的条目并正确检查相应条目时,ListView
功能正确执行。不幸的是,还检查了另一个在点击时不可见的条目!!!
我的代码出了什么问题?
根据Kintanpatel的回答,CheckedTextView
已嵌入LinearLayout
,我尝试使用自定义ArrayAdapter<String>
,其中getView
函数如下所示:< / p>
public View getView(int position,View container,ViewGroup parent){
View view;
view= super.getView(position,container,parent);
((CheckedTextView)vue.findViewById(R.id.myTextView)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((CheckedTextView)v).setChecked(!((CheckedTextView)v).isChecked());
}
});
return(view);
}
我仍有同样的问题...
答案 0 :(得分:0)
最好使用ArrayAdapter的BaseAdapter。 您只需要使用BaseAdapter创建Simple类和exted,也可以在布局中创建自定义单元格,最后将此适配器应用于ListvView。 您可以使用自定义单元格执行所有功能。 下面我为你创建样本。
lay_cell.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="match_parent"
android:orientation="vertical">
<CheckBox
android:id="@+id/check_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
MyAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
/**
* Created by sai on 14-Jul-16.
*/
public class MyAdapter extends BaseAdapter {
private Context mContext;
public MyAdapter(Context mContext) {
this.mContext = mContext;
}
@Override
public int getCount() {
return 10;//return how many cell you want to create
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.lay_cell, parent, false);
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.check_box);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBox.setChecked(true);
}
});
return convertView;
}
}
并且不要忘记将此权限应用于ListView,
listview.setAdapter(new MyAdapter(context));
答案 1 :(得分:0)
事实上,在查看了描述simple_list_item_checked
的xml文件之后,我注意到没有LinearLayout
。我从我自己的xml文件中压缩它,一切都很好!