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

时间:2016-07-03 09:25:40

标签: 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));
    }
}

1 个答案:

答案 0 :(得分:1)

您只需从DATA中删除一个项目(在您的情况下为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;

    }