为什么我的ListActivity卡在/ empty元素上?

时间:2014-11-03 14:02:39

标签: android listview android-arrayadapter listactivity notifydatasetchanged


我正在尝试在我的Android应用程序上使用ListActivity,但它没有按照应有的方式工作 这是我的活动:

public class MainActivity extends ListActivity {

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_terminals_list); //The special layout

        TerminalsArrayAdapter adapter = new TerminalsArrayAdapter(this); // A custom adapter
        setListAdapter(adapter);
        RequestTask task = new RequestTask(adapter); // An Async task
        task.execute("/terminals/all");
    }
}

这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

    <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..." />
</LinearLayout>

这是我的自定义适配器:

public class TerminalsArrayAdapter extends ArrayAdapter<String[]> {

    private Activity mContext;
    private ArrayList<String[]> mObjects = new ArrayList<String[]>();

    public TerminalsArrayAdapter(Activity context) {
        super(context, R.layout.terminals_listview_item);
        this.mContext = context;
    }

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

        if(convertView==null){
            LayoutInflater inflater = mContext.getLayoutInflater();
            elementView = inflater.inflate(R.layout.terminals_listview_item, parent);
            ...
            // Some things with my view

        } else {
            elementView = convertView;
        }

        ...
        // Some things with my view
        return elementView;
    }

    public void updateTerminals(ArrayList<String[]> newObject){
        mObjects.clear();
        mObjects.addAll(newObject);
        notifyDataSetChanged();
        Log.d("TerminalsArrayAdapter", "Dataset Changed !");
    }
}

这是我的异步任务:

public class RequestTask extends AsyncTask<String, Void, ArrayList<String[]>>{

    private final TerminalsArrayAdapter mAdapter;

    public RequestTask(TerminalsArrayAdapter adapter) {
        mAdapter = adapter;
    }

    @Override
    protected ArrayList<String[]> doInBackground(String... params) {

        ...
        // retrieve some data from my server
        return response;
    }

    @Override
    protected void onPostExecute(ArrayList<String[]> response) {
        Log.d("response_server","GOT IT !");
        mAdapter.updateTerminals(response);
    }
}


据我所知,只有在适配器不为空时才会显示ListView。如果它是空的,它将是将显示的TextView。

这是我的应用程序应该工作的方式:

  1. 该应用会创建一个空适配器。这将显示“正在加载...”TextView
  2. 该应用程序会创建一个新的Async任务,该任务将从我的服务器中检索一些所需的数据
  3. 当Async任务完成时,它将调用.updateTerminals()方法
  4. 此方法将更新自定义适配器的条目并调用notifyDataSetChanged()
  5. 应用程序现在应该显示带有新检索数据的ListView
步骤1到4工作正常。问题出现在第5步:即使数据已更新,我的应用程序也不刷新屏幕以显示ListView并保持在“正在加载...”TextView。

我试图找出我的代码有什么问题,但我无法做到。我很想听听你的建议/解决方案!谢谢 !

PS:告诉我你是否需要更好地理解我的问题。

2 个答案:

答案 0 :(得分:0)

ListView身高应为match_parent

尝试使用setVisibility(View.GONE)setVisibility(View.VISIBLE)来玩:

  • 显示TextView&amp;隐藏ListView
  • 显示ListView&amp;隐藏TextView

答案 1 :(得分:0)

public class MainActivity extends ListActivity {

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_terminals_list); //The special layout
        // create your list with specific method to be able call her many time
        // prefer to create then refresh with new data
         CreateList(Passanydata);
         RequestTask task = new RequestTask(adapter); // An Async task
            task.execute("/terminals/all");

    }
}

public void CreateList(ArrayList<String[]> response){
 TerminalsArrayAdapter adapter = new TerminalsArrayAdapter(this,response); // A custom adapter
            setListAdapter(adapter);

}



 @Override
    protected void onPostExecute(ArrayList<String[]> response) {
        Log.d("response_server","GOT IT !");
      // after you got your new data , pass it to your list 
       CreateList(response);
    }




public class TerminalsArrayAdapter extends ArrayAdapter<String[]> {

    private Activity mContext;
    private ArrayList<String[]> mObjects = new ArrayList<String[]>();

    public TerminalsArrayAdapter(Activity context,ArrayList<String[]> objects) {
        super(context, R.layout.terminals_listview_item);
        this.mContext = context;
        // when you have your data it will be able to use it here 
        this.mObjects  = objects;
    }

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

        if(convertView==null){
            LayoutInflater inflater = mContext.getLayoutInflater();
            elementView = inflater.inflate(R.layout.terminals_listview_item, parent);
            ...
            // Some things with my view

        } else {
            elementView = convertView;
        }

        ...
        // Some things with my view
        return elementView;
    }


}