如何在android Fragment中设置自定义ListView?

时间:2012-11-21 12:21:46

标签: android android-fragments android-arrayadapter

我一直在为Android平板电脑做申请。在这里,我需要显示两个ListViews。一个用于简单列表视图,一个用于自定义列表视图。一旦我点击简单的列表视图行,那么必须在另一个自定义列表视图中显示该详细信息。为此,我被片段用于在单个屏幕中显示两个列表视图。对于Custom ListView,我使用自定义适配器来绑定自定义数据。但是当我点击简单列表视图行时,应用程序显示没有响应错误。我的代码就像这样。 因为我的片段包含像这样的代码

public class listDetails extends Fragment{

private int nAndroids;

public static ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
ListDetailsAdapter adapter;

static final String KEY_Title = "title";//"item";

public listDetails() {

}

  /**
    * Constructor for being created explicitly
    */
   public listDetails(int nAndroids) {
        this.nAndroids = nAndroids;
    }

   /**
     * If we are being created with saved state, restore our state
     */
    @Override
    public void onCreate(Bundle saved) {
        super.onCreate(saved);
        if (null != saved) {
            nAndroids = saved.getInt("nAndroids");
        }
    }

    /**
     * Save the number of Androids to be displayed
     */
    @Override
    public void onSaveInstanceState(Bundle toSave) {
        toSave.putInt("nAndroids", nAndroids);
    }

    /**
     * Make a grid and fill it with n Androids
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
        int n;
        Context c = getActivity().getApplicationContext();

        LinearLayout l = new LinearLayout(c);

        String listitems[]=new String[nAndroids];

        HashMap<String, String> map = new HashMap<String, String>();
        map.put(KEY_Title, "Question1");

        map = new HashMap<String, String>();
        map.put(KEY_Title, "Question2");

        map = new HashMap<String, String>();
        map.put(KEY_Title, "Question3");

        map = new HashMap<String, String>();
        map.put(KEY_Title, "Question4");
        menuItems.add(map);

        for (n = 0; n < nAndroids; n++) 
        {

            listitems[n] = "one"+n;
             ListView list = new ListView(c);

             adapter = new ListDetailsAdapter(this, menuItems);
             list.setAdapter(adapter);


        }
        return l;
    }

}

我的自定义适配器代码就像这样

public class ListDetailsAdapter extends BaseAdapter{

///private listDetails listactivity;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;

public ListDetailsAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


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

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.customlist, null);

            TextView title = (TextView)vi.findViewById(R.id.txtTitle);
            Button btnone = (Button)vi.findViewById(R.id.btnfirst);
            Button btntwo = (Button)vi.findViewById(R.id.btnsecond);
            Button btnthree = (Button)vi.findViewById(R.id.btnthird);
            Button btnfour = (Button)vi.findViewById(R.id.btnfourth);
            Button btnfive = (Button)vi.findViewById(R.id.btnfifth);

            btnone.setOnClickListener(oneclick);
            btntwo.setOnClickListener(twoclick);
            btnthree.setOnClickListener(thrirdclick); 
            HashMap<String, String> mymap = new HashMap<String, String>();
            mymap = data.get(position);
            title.setText(mymap.get(listDetails.KEY_Title));


    return vi;
}

private View.OnClickListener oneclick = new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.e("button clicked", "button one clicked");
    }
};

}

请指导我在上面的代码中出错了。

1 个答案:

答案 0 :(得分:0)

我被拍成片段,用于在单个屏幕中显示两个列表视图。 可能是一个列表视图的问题。一个片段 并在一个活动中显示两个片段。 我认为这要好得多。 因为我不认为支持片段加载多个布局(列表视图)