自定义列表视图未显示项目

时间:2016-11-07 14:19:35

标签: php android

我从互联网上重写了一些代码,当我尝试它时,listview没有显示任何空白活动 这是代码:

Adapter.java:

public class Adapter extends BaseAdapter{
    private Activity activity;
    private LayoutInflater inflater;
    private List<DataAnggota> items;

    public Adapter(Activity activity, List<DataAnggota> items) {
        this.activity = activity;
        this.items = items;
    }

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

    @Override
    public Object getItem(int location) {
        return items.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
            convertView = inflater.inflate(R.layout.view_user_list, null);

        TextView id = (TextView) convertView.findViewById(R.id.id);
        TextView nama = (TextView) convertView.findViewById(R.id.nama);
        TextView status = (TextView) convertView.findViewById(R.id.status);

        DataAnggota data = items.get(position);

        id.setText(data.getId_anggota());
        nama.setText(data.getNama());
        status.setText(data.getStatus());

        return convertView;
    }
}

ViewUser.java

public class ViewUser extends AppCompatActivity {

    ListView list;
    List<DataAnggota> itemList = new ArrayList<DataAnggota>();
    Adapter adapter;
    LayoutInflater inflater;
    String url_viewuser = "http://10.0.2.2/test/viewuser.php";
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_user);

        list = (ListView) findViewById(R.id.listView);

        adapter = new Adapter(ViewUser.this, itemList);
        list.setAdapter(adapter);
        callVolley();

    }
    private void callVolley(){
        itemList.clear();
        adapter.notifyDataSetChanged();

        // membuat request JSON
        JsonArrayRequest jArr = new JsonArrayRequest(url_viewuser, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());
                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject obj = response.getJSONObject(i);

                        DataAnggota item = new DataAnggota();

                        item.setId_anggota(obj.getString("id_anggota"));
                        item.setNama(obj.getString("nama"));
                        item.setStatus(obj.getString("status"));
                        itemList.add(item);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
            }
        });
        AppController.getInstance(ViewUser.this).addToRequestQueue(jArr);
    }

}

activity_view_user.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_view_user"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.rzproject.koperasi.ViewUser">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"></ListView>

</RelativeLayout>

view_user_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/id"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/nama"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/status"/>

</LinearLayout>

viewuser.php

include "init.php";

    $query = mysql_query("SELECT * FROM anggota ORDER BY id_anggota ASC");

    $json = array();

    while($row = mysql_fetch_assoc($query)){
        $json[] = $row;
    }

    echo json_encode($json);

    mysql_close($con);

任何代码都错了?请帮忙

1 个答案:

答案 0 :(得分:0)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_user);

    callVolley();
}

private void setupList(){
    list = (ListView) findViewById(R.id.listView);

    adapter = new Adapter(ViewUser.this, itemList);
    list.setAdapter(adapter);
}

private void callVolley(){
    itemList.clear();
    adapter.notifyDataSetChanged();

    // membuat request JSON
    JsonArrayRequest jArr = new JsonArrayRequest(url_viewuser, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());
            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject obj = response.getJSONObject(i);

                    DataAnggota item = new DataAnggota();

                    item.setId_anggota(obj.getString("id_anggota"));
                    item.setNama(obj.getString("nama"));
                    item.setStatus(obj.getString("status"));
                    itemList.add(item);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            setupList();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
        }
    });
    AppController.getInstance(ViewUser.this).addToRequestQueue(jArr);
}

希望这很清楚。首先加载数据,加载后只需设置listview + adapter。

如果要更改数据AGAIN,只需在适配器中创建一个方法来设置listview,然后调用notifydatasetchanged方法。这样,您可以更新适配器中的列表,然后告诉列表视图重绘自己。