【问题标题】:how to access the elements of another layout?如何访问另一个布局的元素?
【发布时间】:2016-07-03 09:25:40
【问题描述】:

我创建了列表视图。 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));
    }
}

【问题讨论】:

    标签: java android listview mobile


    【解决方案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;
    
        }
    

    【讨论】:

    • 那么如何进入我按下按钮的适配器元素位置?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    相关资源
    最近更新 更多