1

我有一个包含多个项目的列表视图。我想在单击按钮时更新项目值,并希望在单击按钮时获取所有项目的值。但我无法做到这一点。请帮我获取此功能

活动代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_ItemValue);
        setContentView(R.layout.activity_iween_booking_page);
        listView = (ListView) findViewById(R.id.passengerList);

            showPassengerListView(passengerList);

        }

    getValue.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            if(){


            }else{
                //Here i have to get all the value of the listview items in a array
            } 

        }
    });


    }

    private void showPassengerListView(final String[] passengerList) {

        listView.setAdapter(new PassengerListUpdate(passengerList));
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            }
        });
    }
    public void passengerInformationPopup(final String[] passengerList, final int position) {
        final Dialog dialog= new Dialog(MainActivity.this,R.style.Dialog_Fullscreen);
        dialog.setContentView(R.layout.passenger_details_dialog); 
        dialog.show();

        setValueDialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                isSuccess= true;

                if(isSuccess){
                    dialog.cancel();
                    //Here i Have to Update the item at position with some value
                }
            }
        });
    }


    public void goBack(View v) {
        finish();
    }

    class PassengerListUpdate extends BaseAdapter {
        String[] ItemValue;

        public PassengerListUpdate(String[] text) {
            ItemValue = text;
        }

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

        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View vi;
            vi = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
            TextView ItemValue;

            ItemValue = (TextView) vi.findViewById(android.R.id.text1);
            ItemValue.setText(ItemValue[position]);
            return (vi);
        }
    }

请帮助我如何设置项目值以及如何获取列表视图值

4

2 回答 2

0

首先,为了获取 ListView 中的全部值。您可以获取适配器中的项目数,然后通过其位置一一获取值。

    ArrayList<String> results = new ArrayList<String>();
    final int count = mMyAdapter.getCount();
    for( int i = 0; i < count; ++i ){
        String item = mMyAdapter.getItem(i);
        results.add(item);
    }
    return results;

其次,为了增加或更新ListView的值,一般来说扩展ArrayAdapter要比实现BaseAdapter容易得多,因为ArrayAdapter本身就支持add/remove/insert方法。例如,

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context) {
        super(context, 0);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View vi;
        vi = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        TextView ItemValue;

        ItemValue = (TextView) vi.findViewById(android.R.id.text1);
        ItemValue.setText(ItemValue[position]);
        return (vi);
    }
}

你可以直接更新适配器的值 quiet ,

mMyAdapter.add("Add Item");
mMyAdapter.remove("Remove Item");
mMyAdapter.insert("Insert Item", 0);

完成工作后,您可以调用适配器的 notifyDatSetChanged() 方法来更新 UI,我认为这比每次更新数据时都设置新适配器更有效。

mMyAdapter.notifyDataSetChanged();
于 2013-08-21T09:48:04.823 回答
0

[想要]点击按钮获取所有项目的值

要获取所有getAllValues()值,请在内部创建一个方法PassengerListUpdate

public String[] getAllValues() {
    return ItemValue;
}

全局声明你的适配器:

PassengerListUpdate myAdapter;

初始化它showPassengerListView(String[])

myAdapter = new PassengerListUpdate(passengerList);
listView.setAdapter(myAdapter);

要获取按钮上所有值的列表,请单击:

button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        if (myAdapter != null) {
            String[] allValues = myAdapter.getAllValues();
        }

    }
});

我想在单击按钮时更新项目值

要更新这些值,您可以简单地重新初始化并设置myAdapter

button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        myAdapter = new PassengerListUpdate(updatedPassengerList);
        listView.setAdapter(myAdapter);
    }
});
于 2013-08-21T09:36:07.293 回答