如何以编程方式隐藏列表视图中的单个行(自定义适配器)

时间:2018-02-23 15:00:20

标签: java android listview

我有一个列表视图应用并使用自定义适配器,但我无法隐藏适配器中的特定行。

我的代码

- :在java中: -

import tkinter as tk import sqlite3 conn = sqlite3.connect("STATS.db") cur = conn.cursor() cur.execute("CREATE TABLE IF NOT EXISTS institution(id INTEGER PRIMARY KEY, name TEXT)") conn.commit() conn.close() root = tk.Tk() root.geometry("300x300") List = tk.Listbox(root, width=100) List.pack() e1_search = tk.StringVar() e1 = tk.Entry(root, textvariable=e1_search) e1.pack() def query_record(event=None): data1 = e1_search.get() conn = sqlite3.connect("STATS.db") cur = conn.cursor() cur.execute("SELECT * FROM institution WHERE name like ?", (data1+"%",)) row = cur.fetchall() for n in row: List.delete(0, tk.END) List.insert(tk.END, n) print(n) conn.close() query_record() e1.bind("<Return>", query_record) b = tk.Button(text="Search", command=query_record) b.pack(side=tk.BOTTOM) root.mainloop()

Adapter

class Adapter extends ArrayAdapter { int[] imageArray; String[] titleArray; String[] descriptionArray; public Adapter(Context context, String[] titles1, String [] description1, int[] img1) { super(context, R.layout.settings_list_row, R.id.settingsTitles, 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.settings_list_row,parent,false); ImageView myImage = (ImageView) row.findViewById(R.id.settingsIcons); TextView myTitle = (TextView) row.findViewById(R.id.settingsTitles); TextView myDescription = (TextView) row.findViewById(R.id.settingsDescriptions); myImage.setImageResource(imageArray[position]); myTitle.setText(titleArray[position]); myDescription.setText(descriptionArray[position]); return row; } }

Main activity

- :要删除: -

String [] titles = {"ti1","ti2","ti3","ti4","ti5"}; String [] descriptions = {"de1","de2","de3","de4","de5"}; int [] images = {R.drawable.ic_image,R.drawable.ic_image,R.drawable.ic_image,R.drawable.ic_image,R.drawable.ic_image}; 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); }

但没有工作!!

1 个答案:

答案 0 :(得分:0)

ListAdapter中,您只需根据需要返回getCount()getItem()getItemId()的修改值,过滤掉您不想显示的项目。

例如

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Holder holder = new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.custom_list_item, null);

    holder.tvName = (TextView) rowView.findViewById(R.id.textViewName);
    holder.img = (ImageView) rowView.findViewById(R.id.imageView1);

    if (items[position].NeedForCollapsed){
        System.out.println("set Visibility.Gone");
        holder.tvName.setVisibility(View.GONE);
        holder.img.setVisibility(View.GONE);

        return rowView;
    }

    //new values
    holder.tvName.setText(items[position].name);
    holder.img.setImageResource(GetImageID(items[position]));

    return rowView;
}

public class Holder
{
    TextView tvName;
    ImageView img;
}