0

我有一个活动,我想从Arraylist生成一个列表视图,该列表视图是从Hashmap填充的。列表正在生成,但列表中只显示一项,它应该显示两项。

这是生成ListView的活动:

public class ExamNamesActivity extends Activity {
    TextView showUser;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> acList = new ArrayList<HashMap<String, String>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exam_names);

    //assigning objects to layout
    showUser=(TextView) findViewById(R.id.exam_names_user_full_name_textView);
    //showing user's full name
    showUser.setText("Welcome, "+getFromPreference("user_name"));


    //------------------for list-----------------------------

    // creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();

    // adding each child node to HashMap key => value
    map.put("Name", "Online Speed Test");
    map.put("Name", "Practice Test");

    acList.add(map);


    ListView list = (ListView) findViewById(R.id.ac_list_listView);
      if (null != list && null != acList) {
       list.setAdapter(new ListAdapter(ExamNamesActivity.this,
         R.id.ListViewContainer, new String[acList.size()]));



      }


    }


    //class for the list and on click handler
    private class ListAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] values;



        public ListAdapter(Context context, int textViewResourceId,
                String[] objects) {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
            this.context = context;
            this.values = objects;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            // return super.getView(position, convertView, parent);

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            final View rowView = inflater.inflate(R.layout.list_layout, parent,
                    false);

            final HashMap<String, String> map =  acList.get(position);

            TextView textView = (TextView) rowView
                     .findViewById(R.id.list_label_name);
                     textView.setText(map.get("Name"));




            rowView.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    rowView.setBackgroundResource(R.drawable.list_bg_pressed);

                    Handler handler = new Handler();
                    Runnable r = new Runnable() {
                        public void run() {
                            rowView.setBackgroundResource(R.drawable.list_bg);

                        }
                    };
                    handler.postDelayed(r, 200);



//                           Intent intent = new Intent(RegisterFirstActivity.this, RegisterSecondActivity.class);
//                              
//                              intent.putExtra("AC_Code", map.get(TAG_CODE));
//                              RegisterFirstActivity.this.startActivity(intent);



                }
            });

            return rowView;

        }
    }





    //getting content from preferences
    public String getFromPreference(String variable_name)
    {
        String preference_return;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        preference_return = preferences.getString(variable_name,"");

        return preference_return;
    }
  //method to show double button alert dialog
    public void twoButtonAlert(String title, String message, String button1, String button2)
    {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(ExamNamesActivity.this);

        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);

        // Setting Icon to Dialog
        //alertDialog.setIcon(R.drawable.delete);

        // Setting Positive "Yes" Button
        alertDialog.setPositiveButton(button1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {

                // Write your code here to invoke YES event
                //goto login activity
                Intent intent = new Intent(ExamNamesActivity.this, LoginActivity.class);
                finish();
                ExamNamesActivity.this.startActivity(intent);


            }
        });

        // Setting Negative "NO" Button
        alertDialog.setNegativeButton(button2, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to invoke NO event
                dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();

    }


    }

这是活动的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AdapterActivity" >

    <TextView
        android:id="@+id/exam_names_user_full_name_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Name Title"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <RelativeLayout
        android:id="@+id/ListViewContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/exam_names_user_full_name_textView"
        android:layout_marginTop="17dp" >

        <ListView
            android:id="@+id/ac_list_listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
        </ListView>

    </RelativeLayout>

</RelativeLayout>

这是列表视图 ( list_layout.xml )每一行的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/library_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignParentLeft="true"
    android:background="@drawable/list_bg"
    android:padding="5dp" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="44dp"
        android:background="@null" >

        <TextView
            android:id="@+id/list_label_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="name"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="17sp" />

    </RelativeLayout>

</LinearLayout>

输出是这样的:

输出

为什么列表没有同时显示这两个项目?我哪里错了? 提前致谢!

4

3 回答 3

2

这是因为您使用的是相同的键"Name",它会覆盖您的第一个值。在 Map 中,当您使用相同的键放置值时,它会覆盖。

这样做,

map.put("Name", "Online Speed Test");
map.put("Name", "Practice Test");

只会在您的地图中产生 1 个值,即

map.put("Name", "Practice Test");

而第一个被覆盖。

您是否尝试获取地图的大小?你会得到大小为 1 而不是 2。

于 2013-04-01T05:36:03.950 回答
2

检查您的数组列表大小,即您只向该列表添加一个哈希映射。这就是为什么您的列表只显示一项。然后关于你的哈希图,你添加两个具有相同键的项目,以便第二个项目覆盖第一个值。所以改变你的代码如下

// creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();

    // adding each child node to HashMap key => value
    map.put("Name", "Online Speed Test");
    acList.add(map);

    map = new HashMap<String, String>();
    map.put("Name", "Practice Test");
    acList.add(map);
于 2013-04-01T05:44:53.827 回答
0

请按照以下步骤

  1. 创建一个 HashMap 对象
  2. 将值放入 hashmap 对象
  3. 将 hashmap 添加到 arraylist

对每个项目执行此步骤。

对于您的情况。它应该像这样

HashMap<String, String> map;

map = new HashMap<String, String>();
map.put("Name", "Online Speed Test");
acList.add(map);

map = new HashMap<String, String>();
map.put("Name", "Practice Test");
acList.add(map);
于 2013-04-01T05:44:57.693 回答