gpt4 book ai didi

java - ListView 未按预期生成

转载 作者:行者123 更新时间:2023-12-01 18:51:34 25 4
gpt4 key购买 nike

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

这是生成ListView的 Activity :

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

}


}

这是 Activity 的布局:

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

这是 ListView 每行的布局(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

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

最佳答案

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

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

关于java - ListView 未按预期生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15738164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com