Listview没有按照需要生成

时间:2013-04-01 05:34:17

标签: java android listview arraylist hashmap

我有一项活动,我希望从 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>

输出如下:

Output

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

3 个答案:

答案 0 :(得分:2)

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

这样做,

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

在地图内只有1个值,即

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

而第一个被覆盖。

你是否试图获得地图的大小?你将获得1而不是2的大小。

答案 1 :(得分: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);

答案 2 :(得分:0)

请按照以下步骤操作

  1. 创建HashMap对象
  2. 将值放在hashmap对象中
  3. 将hashmap添加到arraylist
  4. 对每个项目执行此步骤。

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

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