访问片段中ListView中的单选按钮(从另一个片段获取更新)

时间:2012-11-22 14:28:00

标签: android listview android-fragments

我在Activity中有两个(List-)片段。当在左侧片段中选择一行时,我在右侧片段中显示一些数据,这也是一个ListView。此视图的一行包含TextView和三个RadioButtons。

我的目标是获得这些按钮,即。设置值并设置onClickListener。

我的问题是,右侧片段中的onItemSelected()不起作用,但我不知道为什么。我尝试从ListFragment(作为左侧片段)以及带有自定义ID的ListView执行。

  • 我必须从ListFragment扩展还是像普通的ListView一样?
  • 我是否必须使用onActivityCreate来设置onClickListener?

此代码是尝试从不同帖子尝试某些代码的许多尝试的产物。我看到有一些错误。

这是我的左片段:

public class HeaderListFragment extends ListFragment {
private final static String TAG = "HeaderListFragment";
List<ChecklistItem> header;
static ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();


private DBManager db_manager;
private SimpleAdapter simpleAdapter;

//getstring -> BUG
String[] from = { "number","text" };
int[] to = { R.id.listele_header_no, R.id.listele_header_text};

public void buildList(){
    Log.v(TAG, "buildList");
    list = new ArrayList<HashMap<String,String>>();
    for (ChecklistItem item : header){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("number", item.getnumber());
        map.put("text", item.gettext());        
        list.add(map);
    }
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    updateDetail(header.get(position).getparent_id());
}

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
}

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.headerfragment,
        container, false);

    db_manager = new DBManager(getActivity().getContentResolver());

    header = db_manager.getItemHeader();
    Log.v(TAG, "HEADER LIST SIZE "+header.size());


    buildList();

    simpleAdapter = new SimpleAdapter(getActivity(),
            list,
            R.layout.headeritem_row,
            from,
            to);
    setListAdapter(simpleAdapter);
    Log.v(TAG, "set adapter");


    return view;
  }

  // May also be triggered from the Activity
  public void updateDetail(int id) {

    QuestionListFragment fragment = (QuestionListFragment) getFragmentManager()
        .findFragmentById(R.id.questionFragment);
    if (fragment != null && fragment.isInLayout()) {
      fragment.setUI(id);
    } 

我正确的片段:

public class QuestionListFragment extends ListFragment {
private final static String TAG = "QuestionListFragment";

List<ChecklistItem> questions;
static ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
private DBManager db_manager;
private SimpleAdapter simpleAdapter;
private ProgressDialog dialog;
private RadioGroup radioGroup;
private RadioButton yes, na, open;
private ListView listview;

//getstring -> BUG
String[] from = { "number","text" };
int[] to = { R.id.listele_question_no, R.id.listele_question_text};


public void buildList(){
    Log.v(TAG, "buildList");
    list = new ArrayList<HashMap<String,String>>();
    for (ChecklistItem item : questions){
        Log.v(TAG, item.gettext());
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("number", item.getnumber());
        map.put("text", item.gettext());        
        list.add(map);
    }
}

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
}

 @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.questionfragment,
        container, false);

    Log.v(TAG, "onCreateView");



    db_manager = new DBManager(getActivity().getContentResolver());
    return view;
  }


 @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.v(TAG, "onActivityCreated");
    listview = getListView();
    listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {

                Log.d(TAG, "--> onItemClick listener..."); // You should see this now

                }*/
            }});
}


  public void setUI(Integer parent_id) {
      Log.v(TAG, "setUI");


      questions = db_manager.getQuestions(parent_id);
      buildList();
      simpleAdapter = new SimpleAdapter(getActivity(),
                list,
                R.layout.questionitem_row,
                from,
                to);

        listview.setAdapter(simpleAdapter);
        setListAdapter(simpleAdapter);
        Log.v(TAG, "set adapter");

根据您的反馈,它有效!这是我的代码:

public class QuestionListFragment extends ListFragment {
private final static String TAG = "QuestionListFragment";

List<ChecklistItem> questions;
static ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
private DBManager db_manager;
private ProgressDialog dialog;
private ListViewCustomAdapter adapter;

public void buildList(){
    Log.v(TAG, "buildList");
    list = new ArrayList<HashMap<String,String>>();
    for (ChecklistItem item : questions){
        Log.v(TAG, item.gettext());
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("number", item.getnumber());
        map.put("text", item.gettext());        
        list.add(map);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

 @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.questionfragment,
        container, false);

    Log.v(TAG, "onCreateView");

    db_manager = new DBManager(getActivity().getContentResolver());

    return view;
  }


 @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.v(TAG, "onActivityCreated");

    getListView().setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                Log.d(TAG, "--> onItemClick listener..."); 
            }});

}


  public void setUI(Integer parent_id) {
      Log.v(TAG, "setUI");

      questions = db_manager.getQuestions(parent_id);
      buildList();  
      adapter = new ListViewCustomAdapter(getActivity(), (ArrayList) questions);
      setListAdapter(adapter);
      Log.v(TAG, "set adapter");

  }


  public class ListViewCustomAdapter extends BaseAdapter{
        ArrayList<ChecklistItem> itemList;
        public Activity context;
        public LayoutInflater inflater;

        public ListViewCustomAdapter(Activity context, ArrayList<ChecklistItem> itemList) {
            super();
            this.context = context;
            this.itemList = itemList;
            this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return itemList.size();
        }

        @Override
        public Object getItem(int position) {
            return itemList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        public class ViewHolder
        {
            TextView number;
            TextView text;
            RadioButton yes,na,open;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final int pos = position;

            ViewHolder holder;
            if(convertView==null)
            {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.questionitem_row, null);

                holder.text = (TextView) convertView.findViewById(R.id.listele_question_text);
                holder.number = (TextView) convertView.findViewById(R.id.listele_question_no);
                holder.yes = (RadioButton)convertView.findViewById(R.id.rb_yes);

                RadioButton rb = (RadioButton)convertView.findViewById(R.id.rb_yes);
                rb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.v(TAG, "yes");
                        Log.v(TAG, questions.get(pos).gettext());
                    }
                });

                holder.yes = rb;

                convertView.setTag(holder);
            }
            else
                holder=(ViewHolder)convertView.getTag();
                ChecklistItem item = (ChecklistItem) itemList.get(position);
                holder.text.setText(item.gettext());
                holder.number.setText(item.getnumber());
                return convertView;
       }
  }

0 个答案:

没有答案