我正在尝试在我的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。
这是我的应用程序应该工作的方式:
答案 0 :(得分:0)
ListView
身高应为match_parent
。
尝试使用setVisibility(View.GONE)
和setVisibility(View.VISIBLE)
来玩:
答案 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;
}
}