2016-07-03 8 views
1

私は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

あなたは自分のデータから項目を削除する必要があります(あなたのケースで - 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; 

    } 
+0

次に、ボタンを押したアダプタエレメントの位置はどうやって取得できますか? –

関連する問題