1

我正在ListView使用ListAdapter基于BasedAdapter. 我的ListView项目有一个ToggleButton和一个EditText。在使用这个的活动中ListView,我有一个Button叫做Save. 当我单击Save时,活动必须找到某种方式来获取数据ListView以对此进行一些处理。

在自定义ListView适配器中,我使用 anArrayList来跟踪更改DataItem,其中表示ListView项目的模型(包含用于 的boolean变量ToggleButtonString用于 中的文本的变量EditText)。问题是,当我单击 时Save,我必须以某种方式ArrayList从适配器中获取数据,该适配器还包含每个列表项的更新数据EditText和中的数据?ToggleButton

我怎样才能做到这一点?

4

3 回答 3

3

通常,您会将此适配器设置到 ListView 中。您需要做的就是将此适配器的实例保存在 Activity 的成员变量中。Adapter 已经提供了访问单个项目的方法。您可以添加更多方法来获取数据。像这样的东西:

public class MyAdapter extends BaseAdapter {
    List data;
    ......

    public boolean[] getCheckStates() {
        //get values of your checkboxes here
    }

    public String[] getTexts() {
        //get text of your text boxes here
    }

    // implement all other methods of BaseAdapter
}

然后在您的活动中:

public class MyActivity extends Activity {
    MyAdapter adapter;

    public onCreate(Bundle instance) {
        .....
        adapter = new MyAdapter(...);
        ...
        myList.setAdapter(adapter);
        ...
        btnSave.setOnClickListener(new OnClickListener() {
            public void onClick (View v) {
                boolean[] checks = adapter.getCheckStates();
                String[] texts = adapter.getTexts();
                ....
            }
        });
    }

    ........
}
于 2012-04-26T08:42:47.350 回答
0

嗨,这是你需要的:

public class MusicTrack extends Activity {  
private ArrayAdapter<CardDetails> listAdapter ;  
ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();  
CardDetailsAdapter mListAdapter;  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  

    final ArrayList<CardDetails> cardResults = GetCardDetails();
    for (int i = 0; i < cardResults.size(); i++) {
        System.out.println("card redsults: " + cardResults.get(i).isSelected());
    }
    final ListView welcomelist = (ListView) findViewById(R.id.welcomelistID);

    listAdapter = new CardDetailsAdapter(this, cardResults);
    welcomelist.setAdapter(listAdapter );      

    final Button saveBtn = (Button) findViewById(R.id.saveID);
    saveBtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // get the checkbox status and save related details into database
        //  Intent intent = new Intent(MusicTrack.this, TransactionSummaryActivity.class);
            itemChecked = mListAdapter.itemChecked1;    
            System.out.println("item checked: " + itemChecked);

            for (int i = 0; i < cardResults.size(); i++) {
                System.out.println("cardResults and checked status: " + cardResults.get(i).getText());
                System.out.println("cardResults and checked status111: " + cardResults.get(i).isSelected());
            }
        }
    });

}
private ArrayList<CardDetails> GetCardDetails() {

    ArrayList<CardDetails> carddetailresults = new ArrayList<CardDetails>();
    CardDetails sr1 = new CardDetails();

      sr1.setText("Text1"); 
      sr1.setSelected(false);
      carddetailresults.add(sr1);

      sr1 = new CardDetails();
      sr1.setText("Text2"); 
      sr1.setSelected(true);
      carddetailresults.add(sr1);

      sr1 = new CardDetails();
      sr1.setText("Text3"); 
      sr1.setSelected(true);
      carddetailresults.add(sr1);

      sr1 = new CardDetails();
      sr1.setText("Text4"); 
      sr1.setSelected(true);
      carddetailresults.add(sr1);

      sr1 = new CardDetails();
      sr1.setText("Text5"); 
      sr1.setSelected(false);
      carddetailresults.add(sr1);

    return carddetailresults;
}}
class CardDetails{   
String text;   
Boolean selected;   
public String getText() {
    return text;
}

public void setText(String bankName) {
    text = bankName;
}

 public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }}


class CardDetailsAdapter extends ArrayAdapter<CardDetails> {
private static ArrayList<CardDetails> searchArrayList;
public static ArrayList<Integer> alSelCards = new ArrayList<Integer>();
public static ArrayList<Boolean> itemChecked1 = null;
private LayoutInflater mInflater;

protected Context getContext;
 public CardDetailsAdapter( Context context, ArrayList<CardDetails> results) {
      super( context, R.layout.webview, R.id.textID, results );
      // Cache the LayoutInflate to avoid asking for a new one each time.
      searchArrayList = results;
      getContext = context;
      mInflater = LayoutInflater.from(context) ;
      itemChecked1 = new ArrayList<Boolean>();
        for (int i = 0; i < results.size(); i++) {
            itemChecked1.add(i, results.get(i).isSelected()); // initializes all items value with

        }
    }

public int getCount() {
    if (searchArrayList != null)
        return searchArrayList.size();
    return 0;
}

public CardDetails getItem(int position) {

    return searchArrayList.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView,
        final ViewGroup parent) {
    CardDetails planet = (CardDetails) this.getItem(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.webview, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.textID);
            holder.chkbox = (ToggleButton) convertView.findViewById(R.id.togID);

            convertView.setTag(holder);

            holder.text.setText(searchArrayList.get(position).getText());
            holder.chkbox.setTag(planet);
            holder.chkbox.setChecked(planet.isSelected());

        } else {
            holder = (ViewHolder) convertView.getTag();
            ViewHolder viewHolder = (ViewHolder) convertView.getTag();
            holder.chkbox = viewHolder.getCheckBox();
            holder.text = viewHolder.getText();

            holder.text.setText(searchArrayList.get(position).getText());
            holder.chkbox.setTag(planet);

            holder.chkbox.setChecked(planet.isSelected());

        }
        holder.chkbox.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ToggleButton cb = (ToggleButton) v;
                CardDetails planet = (CardDetails) cb.getTag();
                //planet.setSelected(cb.isChecked());
                if (holder.chkbox.isChecked()) {
                    itemChecked1.set(position, true);
                    holder.chkbox.setChecked(true);
                    planet.setSelected(true);


                } else if (!holder.chkbox.isChecked()) {
                    itemChecked1.set(position, false);
                    holder.chkbox.setChecked(false);
                    planet.setSelected(false);
                }
            }
        });

        return convertView;
    }

    public static class ViewHolder {
        private ToggleButton chkbox;
        TextView text;

        public ViewHolder() {
        }

        public ViewHolder(TextView text, ToggleButton chkbox) {
            this.chkbox = chkbox;
            this.text = text;
        }

        public ToggleButton getCheckBox() {
            return chkbox;
        }

        public void setCheckBox(CheckBox checkBox) {
            this.chkbox = chkbox;
        }

        public TextView getText() {
            return text;
        }

        public void setText(TextView text) {
            this.text = text;
        }

    }}

导入需要的东西

于 2012-04-26T09:48:41.597 回答
0

对,那是正确的。此外,要获得 radioButton checkedId,您需要创建一个 onCkeckedChangeListener 并在那里设置 checkedId。之后覆盖以下方法

public int[] getChkId() {
        return chkId;
    }

获取选中的单选按钮。谢谢。

于 2013-05-18T07:26:47.320 回答