我在ListView中有一个隐形复选框,我希望它可见。但是,无法实现它。请查看以下代码段:
列出活动: -
public class SelectDateRangeActivity extends AppCompatActivity {
ListView mOptionsList;
LogRangeOptionsAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_date_range);
mOptionsList = (ListView) findViewById(R.id.date_range_options_list);
mAdapter = new LogRangeOptionsAdapter(this, R.layout.date_range_option_list_item, getResources().getStringArray(R.array.image_log_date_filter_options));
mOptionsList.setAdapter(mAdapter);
}
@Override
public void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String selectedOption = extras.getString(Utility.EXTRA_LOG_SELECTED_DATE_RANGE);
if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[0])){
final View view = mOptionsList.getAdapter().getView(0, null, mOptionsList);
CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
c.setVisibility(View.VISIBLE);
c.setChecked(true);
mAdapter.notifyDataSetChanged();
}
if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[1])){
final View view = mOptionsList.getAdapter().getView(1, null, mOptionsList);
CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
c.setVisibility(View.VISIBLE);
c.setChecked(true);
mAdapter.notifyDataSetChanged();
}
if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[2])){
final View view = mOptionsList.getAdapter().getView(0, null, mOptionsList);
CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
c.setVisibility(View.VISIBLE);
c.setChecked(true);
mAdapter.notifyDataSetChanged();
}
if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[3])){
final View view = mOptionsList.getAdapter().getView(0, null, mOptionsList);
CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
c.setVisibility(View.VISIBLE);
c.setChecked(true);
mAdapter.notifyDataSetChanged();
}
}
}
}
适配器: -
public class LogRangeOptionsAdapter extends ArrayAdapter<String> {
Context mAdapterContext;
String[] mItems;
public LogRangeOptionsAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
this.mAdapterContext = context;
this.mItems = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mAdapterContext.getSystemService(mAdapterContext.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.date_range_option_list_item, parent, false);
}
TextView mTitleText = (TextView) convertView.findViewById(R.id.date_range_option_text);
mTitleText.setText(mItems[position]);
final String titleText = mTitleText.getText().toString();
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent returnOptionIntent = new Intent(mAdapterContext, ImageLogActivity.class);
returnOptionIntent.putExtra(Utility.EXTRA_LOG_DATE_RANGE_OPTION, titleText);
Activity activity = (Activity) mAdapterContext;
activity.setResult(Utility.INTENT_IMAGE_LOG_OPTIONS_CODE, returnOptionIntent);
activity.finish();
activity.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left);
}
});
return convertView;
}
}
选择日期范围xml: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context="com.sigue.sigueagent.SelectDateRangeActivity">
<ListView
android:id="@+id/date_range_options_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
日期范围选项: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/item_date_range_padding_top"
android:paddingBottom="@dimen/item_date_range_padding_bottom">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/date_range_option_text"
android:layout_alignBottom="@+id/image_log_checkbox"
android:layout_alignTop="@+id/image_log_checkbox"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image_log_checkbox"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="invisible"/>
</RelativeLayout>
答案 0 :(得分:3)
所以我通过在适配器中设置可见或不可见而不是尝试在实际活动中执行此操作来解决此问题,这是更改的活动。希望它可以帮助某人:)!
public class SelectDateRangeActivity extends AppCompatActivity {
ListView mOptionsList;
LogRangeOptionsAdapter mAdapter;
String mSelectedOption;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_date_range);
Bundle extras = getIntent().getExtras();
if (extras != null) {
mSelectedOption = extras.getString(Utility.EXTRA_LOG_SELECTED_DATE_RANGE);
}
mOptionsList = (ListView) findViewById(R.id.date_range_options_list);
mAdapter = new LogRangeOptionsAdapter(this, R.layout.date_range_option_list_item, getResources().getStringArray(R.array.image_log_date_filter_options));
mAdapter.setCheckedOption(mSelectedOption);
mOptionsList.setAdapter(mAdapter);
}
}
**适配器
public class LogRangeOptionsAdapter extends ArrayAdapter<String> {
Context mAdapterContext;
String[] mItems;
String mSelectedOption;
public LogRangeOptionsAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
this.mAdapterContext = context;
this.mItems = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mAdapterContext.getSystemService(mAdapterContext.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.date_range_option_list_item, parent, false);
}
TextView mTitleText = (TextView) convertView.findViewById(R.id.date_range_option_text);
mTitleText.setText(mItems[position]);
final String titleText = mTitleText.getText().toString();
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.date_range_option_checkbox);
if (mSelectedOption.equalsIgnoreCase(mAdapterContext.getResources().getStringArray(R.array.image_log_date_filter_options)[0]) && position == 0) {
checkBox.setVisibility(View.VISIBLE);
checkBox.setChecked(true);
}
if (mSelectedOption.equalsIgnoreCase(mAdapterContext.getResources().getStringArray(R.array.image_log_date_filter_options)[1]) && position == 1) {
checkBox.setVisibility(View.VISIBLE);
checkBox.setChecked(true);
}
if (mSelectedOption.equalsIgnoreCase(mAdapterContext.getResources().getStringArray(R.array.image_log_date_filter_options)[2]) && position == 2) {
checkBox.setVisibility(View.VISIBLE);
checkBox.setChecked(true);
}
if (mSelectedOption.equalsIgnoreCase(mAdapterContext.getResources().getStringArray(R.array.image_log_date_filter_options)[3]) && position == 3) {
checkBox.setVisibility(View.VISIBLE);
checkBox.setChecked(true);
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent returnOptionIntent = new Intent(mAdapterContext, ImageLogActivity.class);
returnOptionIntent.putExtra(Utility.EXTRA_LOG_DATE_RANGE_OPTION, titleText);
Activity activity = (Activity) mAdapterContext;
activity.setResult(Utility.INTENT_IMAGE_LOG_OPTIONS_CODE, returnOptionIntent);
activity.finish();
activity.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left);
}
});
return convertView;
}
public void setCheckedOption(String option){
this.mSelectedOption = option;
}
}
答案 1 :(得分:0)
尝试这种方式。
if (extras != null) {
String selectedOption = extras.getString(Utility.EXTRA_LOG_SELECTED_DATE_RANGE);
if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[0])){
final View view = mOptionsList.getAdapter().getView(0, null, mOptionsList);
CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
c.setAlpha(1f);
c.setChecked(true);
mAdapter.notifyDataSetChanged();
}else{
c.setAlpha(0.5f);
c.setChecked(false);
}
if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[1])){
final View view = mOptionsList.getAdapter().getView(1, null, mOptionsList);
CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
c.setAlpha(1f);
c.setChecked(true);
mAdapter.notifyDataSetChanged();
}else{
c.setAlpha(0.5f);
c.setChecked(false);
}
if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[2])){
final View view = mOptionsList.getAdapter().getView(0, null, mOptionsList);
CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
c.setAlpha(1f);
c.setChecked(true);
mAdapter.notifyDataSetChanged();
}else {
c.setAlpha(0.5f);
c.setChecked(false);
}
if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[3])){
final View view = mOptionsList.getAdapter().getView(0, null, mOptionsList);
CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
c.setAlpha(1f);
c.setChecked(true);
mAdapter.notifyDataSetChanged();
}else{
c.setAlpha(0.5f);
c.setChecked(false);
}
}
并在xml文件中设置默认值“android:alpha =”0.5“”