我有一个活动,我想从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>
输出是这样的:
为什么列表没有同时显示这两个项目?我哪里错了? 提前致谢!