2016-11-07 93 views
-1

我重写从互联网上一些代码,当我尝试它的列表视图并没有显示任何东西只是一个空白活动 下面是代码:自定义列表视图没有显示项目

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

任何代码错了吗?请帮忙

+0

尝试在填充ArrayList之后设置适配器。目前,您正在设置一个空数组,并且您正在更新本地数组列表,而不是更新列表视图中使用的数组列表。 –

+0

@JordiSipkens你能给我一些解决方案吗?我尝试移动callVolley方法,但仍有一些空白,有些让我的应用程序组合关闭 – agrarendzo

回答

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 +适配器。

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

+0

我得到NullPointerExecption错误 – agrarendzo

+0

你从哪里得到一个空指针? –