BaseAdapter不会更新listview中的数据

时间:2015-08-19 08:04:16

标签: android listview android-listview baseadapter custom-adapter

我正在尝试通过Arraylist<Hashmap<String, String>获取BaseAdapter,但BaseAdapter仅显示ListView中的第一个元素,这意味着它未显示完整Arraylist在`ListView。

中的1}}

这是我的listview xml Rating_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;
    }
}

但它只显示列表中的一个项目,但我发送的列表包含五个项目。

2 个答案:

答案 0 :(得分:0)

  

那么为什么它没有在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); 
}

应该这样做

答案 1 :(得分:0)

执行此操作时,返回arraylist的大小为1,因为arraylist仅包含hashmap。因此,适配器认为您只有一个要显示的项目。您应该从arraylist返回hashmap的大小。对所有其他方法也一样。

 @Override
    public int getCount() {
        return list.size();
    }

解决方案:

@Override
        public int getCount() {
            return list.get(0).size();
        }