6

我一直在为android平板电脑做申请。在此,我需要显示两个 ListView。一种用于简单列表视图,一种用于自定义列表视图。一旦我单击简单的列表视图行,则必须在另一个自定义列表视图中显示详细信息。为此,我采取了在单个屏幕中显示两个列表视图的片段。对于自定义 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");
    }
};

}

请指导我上面的代码出了什么问题。

4

1 回答 1

0

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

于 2012-11-21T14:44:29.700 回答