java - 如何访问另一个布局的元素?

标签 java android listview mobile

我创建了 ListView 。 enter image description here 还有我的适配器,它有一个按钮,textView。 enter image description here 我想当我点击一个按钮时从列表中删除元素。 如何在MainActiviti 类中访问另一个布局的元素?

public class AdapterItem extends BaseAdapter {

    Context context;
    LayoutInflater lInflater;
    ArrayList<ItemInList> items;


    public AdapterItem(Context context, ArrayList<ItemInList> items){
        this.context = context;
        this.items = items;
        lInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.item, parent, false);
        }

        ItemInList itemInList = getItemList(position);



        ((TextView) view.findViewById(R.id.id_item)).setText(itemInList.id);
        ((TextView) view.findViewById(R.id.name)).setText(itemInList.name);
        ((ImageButton) view.findViewById(R.id.imageButton)).setImageResource(itemInList.imageButton);

        return view;

    }


    ItemInList getItemList(int position) {
        return ((ItemInList) getItem(position));
    }
}

ItemInList.java

        public class ItemInList  {

    String name;
    String id;
    int imageButton;
    boolean deleteChek;

    public ItemInList(String name, String id, int imageButton, boolean deleteChek){

        this.name = name;
        this.id = id;
        this.imageButton = imageButton;
        this.deleteChek = deleteChek;
    }
}

MainActivity.java

 public class MainActivity extends AppCompatActivity {

ArrayList<ItemInList> arrayItemInLists = new ArrayList<ItemInList>();
AdapterItem adapterItem;
ListView listViewl;
Button delete;
Button checkAll;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fillData();

    listViewl = (ListView) findViewById(R.id.listView);
    delete = (Button) findViewById(R.id.delete_select);
    checkAll = (Button) findViewById(R.id.select_all);

    adapterItem = new AdapterItem(this, arrayItemInLists);
    listViewl.setAdapter(adapterItem);

}

public void fillData() {
    for (int i = 1; i <= 20; i++) {
        arrayItemInLists.add(new ItemInList("String" + i," " + i, R.drawable.delete,false));
    }
}

最佳答案

您只需从您的数据中删除一个项目(在您的例子中 - arrayItemInLists)并调用 notifyDataSetChanged()。事实上,您可以在 Adapter 中执行此操作。

private OnClickListener listener= new OnClickListener() {
    @Override
    public void onClick(View v) {
        int position = ((Integer) v.getTag());

    }
};

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.item, parent, false);
        }

        ItemInList itemInList = getItemList(position);



        ((TextView) view.findViewById(R.id.id_item)).setText(itemInList.id);
        ((TextView) view.findViewById(R.id.name)).setText(itemInList.name);
        ((ImageButton) view.findViewById(R.id.imageButton)).setImageResource(itemInList.imageButton);

        ((ImageButton) view.findViewById(R.id.imageButton)).setTag(position);
        ((ImageButton) view.findViewById(R.id.imageButton)).setOnClickListener(listener);
        return view;

    }

关于java - 如何访问另一个布局的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38168553/

相关文章:

java - 如何使用 Java 从解析查询中检索数组?

android - 独立 RadioGroup 列表

java - 拆分EIP中的Camel异常路由

android - 不支持的方法 : BaseConfig. getApplicationIdSuffix()

java - Rest webservice 生成客户端 stub

java - 调试时会忽略依赖项 org.apache.httpcomponents :httpclient:4. 5,因为它可能与提供的内部版本冲突

qt - QML 更改项目的最小拖动/轻弹距离

java - 为什么我的光标在 ListView 中不起作用?

java - 如何在 Java 中使用 Aether 搜索 RemoteRepositories?

java - 简单的父子 Java 应用程序