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

6

我一直在为 Android 平板应用程序开发。在这个应用中,我需要显示两个列表视图。一个是简单的列表视图,另一个是自定义的列表视图。当我点击简单列表视图行时,那些详情需要在另一个自定义列表视图中显示。为此,我使用 fragment 在单个屏幕上显示两个列表视图。对于自定义 ListView,我使用自定义适配器来绑定自定义数据。但是,当我点击简单列表视图行时,应用程序会显示未响应错误。我的代码如下所示。 对于我的 fragment 包含以下代码:

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");
    }
};

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


我在你的代码中找不到明显的错误,但是如果你在Eclipse中使用调试器,并在onClickListener中Log.e()语句上设置断点后,逐步执行代码,也许你可以准确定位问题? - span
我看到你添加了三个不同的onClickListners。 它们在哪里,为什么你想这样做?btnone.setOnClickListener(oneclick); btntwo.setOnClickListener(twoclick); btnthree.setOnClickListener(thrirdclick); - Noloxs
是的,我们可以采取一个操作方法来处理多个按钮的操作。但就我的问题而言,这不是问题所在。我只是这样做而已。但我没有为此实现功能。 - nari
1个回答

0

我使用了一个片段来在单个屏幕上显示两个列表视图。可能这就是问题所在,因为一个片段只能用于一个列表视图。最好的方法是在一个活动中显示两个片段。因为我认为片段不支持加载多个布局(列表视图)。


是的,我只为自定义列表视图取了一个片段,而简单列表视图不在同一个片段中。 - nari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接