如何隐藏和取消隐藏开关首选项上的特定ListView项目?

时间:2018-02-09 17:07:19

标签: java android listview

我想在关闭时隐藏列表中的第二个选项,我知道交换机是如何工作的,只需告诉我如何隐藏 - 从列表中取消隐藏选项。我希望逐项隐藏列表视图或类似的东西。

新查询:是否可以添加两个不同的适配器并在切换首选项更改时切换它们?如果是,那该怎么做?

这是100%可能。

String [] titles = {"abc","def","ghi"};
String [] descriptions = {"abc","def","ghi"};
int [] images = {R.drawable.ic_abc,R.drawable.ic_def,R.drawable.ic_ghi};
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_list_view);
  lv = (ListView) findViewById(R.id.listView);
  final Adapter adapter = new Adapter(getApplicationContext(), titles, descriptions, images);
  lv.setAdapter(adapter);
  lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    }
  });
}

列出视图适配器

class Adapter extends ArrayAdapter {
  int[] imageArray;
  String[] titleArray;
  String[] descriptionArray;

  public Adapter(Context context, String[] titles1, String [] description1, int[] img1) {
    super(context, R.layout.list_row, R.id.Titles, titles1);
    this.imageArray = img1;
    this.titleArray = titles1;
    this.descriptionArray = description1;
  }

  @NonNull
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.list_row,parent,false);
    ImageView myImage = (ImageView) row.findViewById(R.id.Icons);
    TextView myTitle = (TextView) row.findViewById(R.id.Titles);
    TextView myDescription = (TextView) row.findViewById(R.id.Descriptions);

    myImage.setImageResource(imageArray[position]);
    myTitle.setText(titleArray[position]);
    myDescription.setText(descriptionArray[position]);
    return row;
  }
}

切换首选项

    public SwitchPreference sw;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_sw);
        sw = (SwitchPreference) findPreference("001");
    }

在主要活动中打开/关闭

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean sw = sharedPreferences.getBoolean("001", true);
if (sw) {
  //hide list view item (only one)
} else {
  //unhide list view item (only one)
}

1 个答案:

答案 0 :(得分:1)

好吧,看看吧,你可以正确地调用删除:

class Adapter extends ArrayAdapter {
  List<ItemObject> data = new ArrayList();

  public Adapter(Context context, String[] titles1, String [] description1,     int[] img1) {
    super(context, R.layout.list_row, R.id.Titles, titles1);
    for(int i = 0; i < titles1.lenght; i++)
        data.add(new ItemObject(titles1[i], description1[i], img1[i]);
  }
  static class ItemObject {
       String title, description;
       int image;
       ItemObject(String ti, String desc, int img) {
           title = ti;
           description = desc;
           image = img;
       }
  }
  //plus the rest of your class

在getView上更改

 ItemObject row = data.get(position);
 myImage.setImageResource(row.image);
 myTitle.setText(row.title);
 myDescription.setText(row.description);

并在适配器中添加此方法:

 public void removeObject(int at) {
      data.remove(at);
      notifyDataSetChanged();
 }

所以你可以用一个位置调用removeObject。