1

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

我的目标是获取这些按钮,fe 来设置值并设置 onClickListener。

我的问题是,我在右侧 Fragment 中的 onItemSelected() 不起作用,但我不知道为什么。我尝试从 ListFragment (作为左 Fragment)以及具有自定义 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;
       }
  }
4

0 回答 0