BaseAdapter 不更新列表视图中的数据
BaseAdapter is not updating data in listview
我试图通过 BaseAdapter
获取 Arraylist<Hashmap<String, String>
,但是 BaseAdapter
只显示 ListView
中的第一个元素,这意味着它没有显示完整的 Arraylist
在 `ListView.
这是我的列表视图xmlRating_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:text="text"
android:textColor="#6eb5c4"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
这是Rating_layout.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="@+id/custom_top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/action_bar_popup" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:background="@drawable/new_background"
android:orientation="horizontal" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="0.38"
android:divider="#000000"
android:dividerHeight="1dp" >
</ListView>
</LinearLayout>
</LinearLayout>
这是我用来设置CustomAdapter的方法
protected void Rating_info() {
pop_pref = getSharedPreferences("rating_info", MODE_PRIVATE);
flag = pop_pref.getString("flag", null);
size = pop_pref.getInt("a",0);
for (int i = 0; i<=(size-1);i++){
app_detail.put("provider_name",pop_pref.getString("key"+i, null));
System.out.println("in for loop "+pop_pref.getString("key"+1, null));
}
// provider_name = pop_pref.getString("provider_name", null);
applist.add(app_detail);
if(flag.equalsIgnoreCase("-1")){
popup_dialog = new Dialog(Home_Screen.this);
popup_dialog.setContentView(R.layout.rating_layout);
popup_dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
popup_dialog.setCancelable(true);
//final View view = popup_dialog.findViewById(R.id.custom_top);
TextView tv1 = (TextView)popup_dialog.findViewById(R.id.textView1);
// RatingBar rb = (RatingBar)popup_dialog.findViewById(R.id.popup_ratingbar);
// ImageView close = (ImageView)popup_dialog.findViewById(R.id.close);
tv1.setText("Give Rating");
// rb.setRating(Float.parseFloat("2.0"));
ListView lv = (ListView) popup_dialog.findViewById(R.id.listView1);
ImageView close = (ImageView)popup_dialog.findViewById(R.id.close);
Custum_Adapter adapter = new Custum_Adapter(Home_Screen.this, applist);
lv.setAdapter(adapter);
}
}
这是我的 CustomAdapter
private class Custum_Adapter extends BaseAdapter {
ArrayList<HashMap<String, String>> list;
public Custum_Adapter(Context context, ArrayList<HashMap<String, String>> applist) {
this.list = applist;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.rating_list, parent, false);
}
System.out.println("list size*******"+list.size());
HashMap<String, String> a = list.get(position);
System.out.println("Position of hash map----->"+position);
// String id = a.get("id");
String name = a.get("provider_name");
System.out.println("name--------->"+name);
// String c_cost = a.get("cost");
// String c_id = a.get("cost_id");
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
// TextView tv1 = (TextView) convertView.findViewById(R.id.textView2);
// TextView tv2 = (TextView) convertView.findViewById(R.id.costing_id);
// TextView tv3 = (TextView) convertView.findViewById(R.id.cost);
tv.setText(name);
// tv1.setText(id);
// tv2.setText(c_id);
// tv3.setText(c_cost);
return convertView;
}
}
但它只显示列表中的一个项目,尽管我正在发送一个包含五个项目的列表。
So why it is not showing all five items in listview.
因为在 for 循环的每次迭代中,您都会覆盖您实例化的唯一 HashMap
的值,并且仅在循环结束时将其添加到 ArrayList。像
for (int i = 0; i< size ;i++){
app_detail = new HashMap<>();
app_detail.put("provider_name",pop_pref.getString("key"+i, null));
System.out.println("in for loop "+pop_pref.getString("key"+1, null));
applist.add(app_detail);
}
应该做
执行此操作时,您 return arraylist 的大小为 1,因为 arraylist 仅包含 hashmap.So 适配器认为您只有一项要显示。您应该 return 来自 arraylist 的 hashmap 的大小。对所有其他方法执行相同的操作。
@Override
public int getCount() {
return list.size();
}
解决方案:
@Override
public int getCount() {
return list.get(0).size();
}
我试图通过 BaseAdapter
获取 Arraylist<Hashmap<String, String>
,但是 BaseAdapter
只显示 ListView
中的第一个元素,这意味着它没有显示完整的 Arraylist
在 `ListView.
这是我的列表视图xmlRating_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:text="text"
android:textColor="#6eb5c4"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
这是Rating_layout.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="@+id/custom_top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/action_bar_popup" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:background="@drawable/new_background"
android:orientation="horizontal" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:layout_weight="0.38"
android:divider="#000000"
android:dividerHeight="1dp" >
</ListView>
</LinearLayout>
</LinearLayout>
这是我用来设置CustomAdapter的方法
protected void Rating_info() {
pop_pref = getSharedPreferences("rating_info", MODE_PRIVATE);
flag = pop_pref.getString("flag", null);
size = pop_pref.getInt("a",0);
for (int i = 0; i<=(size-1);i++){
app_detail.put("provider_name",pop_pref.getString("key"+i, null));
System.out.println("in for loop "+pop_pref.getString("key"+1, null));
}
// provider_name = pop_pref.getString("provider_name", null);
applist.add(app_detail);
if(flag.equalsIgnoreCase("-1")){
popup_dialog = new Dialog(Home_Screen.this);
popup_dialog.setContentView(R.layout.rating_layout);
popup_dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
popup_dialog.setCancelable(true);
//final View view = popup_dialog.findViewById(R.id.custom_top);
TextView tv1 = (TextView)popup_dialog.findViewById(R.id.textView1);
// RatingBar rb = (RatingBar)popup_dialog.findViewById(R.id.popup_ratingbar);
// ImageView close = (ImageView)popup_dialog.findViewById(R.id.close);
tv1.setText("Give Rating");
// rb.setRating(Float.parseFloat("2.0"));
ListView lv = (ListView) popup_dialog.findViewById(R.id.listView1);
ImageView close = (ImageView)popup_dialog.findViewById(R.id.close);
Custum_Adapter adapter = new Custum_Adapter(Home_Screen.this, applist);
lv.setAdapter(adapter);
}
}
这是我的 CustomAdapter
private class Custum_Adapter extends BaseAdapter {
ArrayList<HashMap<String, String>> list;
public Custum_Adapter(Context context, ArrayList<HashMap<String, String>> applist) {
this.list = applist;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.rating_list, parent, false);
}
System.out.println("list size*******"+list.size());
HashMap<String, String> a = list.get(position);
System.out.println("Position of hash map----->"+position);
// String id = a.get("id");
String name = a.get("provider_name");
System.out.println("name--------->"+name);
// String c_cost = a.get("cost");
// String c_id = a.get("cost_id");
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
// TextView tv1 = (TextView) convertView.findViewById(R.id.textView2);
// TextView tv2 = (TextView) convertView.findViewById(R.id.costing_id);
// TextView tv3 = (TextView) convertView.findViewById(R.id.cost);
tv.setText(name);
// tv1.setText(id);
// tv2.setText(c_id);
// tv3.setText(c_cost);
return convertView;
}
}
但它只显示列表中的一个项目,尽管我正在发送一个包含五个项目的列表。
So why it is not showing all five items in listview.
因为在 for 循环的每次迭代中,您都会覆盖您实例化的唯一 HashMap
的值,并且仅在循环结束时将其添加到 ArrayList。像
for (int i = 0; i< size ;i++){
app_detail = new HashMap<>();
app_detail.put("provider_name",pop_pref.getString("key"+i, null));
System.out.println("in for loop "+pop_pref.getString("key"+1, null));
applist.add(app_detail);
}
应该做
执行此操作时,您 return arraylist 的大小为 1,因为 arraylist 仅包含 hashmap.So 适配器认为您只有一项要显示。您应该 return 来自 arraylist 的 hashmap 的大小。对所有其他方法执行相同的操作。
@Override
public int getCount() {
return list.size();
}
解决方案:
@Override
public int getCount() {
return list.get(0).size();
}