0

ここに、APIデモで見たものに基づいたベースアダプターがあります。リストからアイテムを動的に追加/削除したい。この場合、ボタンによって呼び出されたインテントから追加し、リストビューのイメージビューのクリックから削除します。この場合、DATA[]からアイテムを追加/削除します。私はSOとグーグルでさまざまなPASSWORD()メソッドやRemove()メソッドを探しましたが、この状況についてはあまり思いつきませんでした。どんな助けでも素晴らしいでしょう。コードは次のとおりです。

public class myActivity extends Activity{
    private static final int CONTACT_PICKER_RESULT = 1001;  
    private static final String TAG = myActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView lv = (ListView)findViewById(R.id.listView1);
        lv.setAdapter(new myAdapter(this));     
        Button bAdd = (Button)findViewById(R.id.bAdd);
        bAdd.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
        ........xxxx....xxxx....
        }
public class myAdapter extends BaseAdapter{
        private LayoutInflater mInflater;

        public myAdapter(Context context) {
            mInflater = LayoutInflater.from(context);

        }


        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return DATA.length;
        }


        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }


        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }


        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.row,null);
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.txtTitle);
                holder.icon = (ImageView) convertView.findViewById(R.id.imgIcon);
                convertView.setTag(holder);


            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }
            holder.text.setText(DATA[position]);
            holder.icon.setImageResource(android.R.drawable.ic_delete);

            return convertView;
        }



        }


    static class ViewHolder {
        TextView text;
        ImageView icon;
    }
//this will not be hard-coded, jsut included for clarity
private static final String[] DATA ={
        "one","two","three" };
4

1 に答える 1

1

DATA []配列から項目を追加/削除するだけでよい場合は、配列を、たとえばListによって実装された単純なものに置き換えてみませんか。ArrayList

これにより、必要に応じてリストから項目を追加/削除するアダプタのクリックハンドラを作成できます。

于 2011-11-10T00:28:39.303 に答える