空列表 Android 工作室
Empty List Android Studio
为什么我的 phone 上的列表是空的。有人能帮忙吗?它显示了正确的 7 个位置,但它们是空的。
我的 php 文件可能没问题。我使用 json 编码。
public class ClientActivity extends UserAreaActivity {
private String jsonResult;
private String url = "http://vinusek.000webhostapp.com/Client2.php";
private ListView listView;
private TextView nazwa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
listView = (ListView) findViewById(R.id.listView1);
nazwa = (TextView) findViewById(R.id.textView2) ;
accessWebService();
}
// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("result");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String number = jsonChildNode.getString("id");
String name = jsonChildNode.getString("login");
String pass = jsonChildNode.getString("haslo");
String outPut = number + "-" + name + "-" + pass;
employeeList.add(createEmployee("list", outPut));
nazwa.setText(outPut);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(ClientActivity.this, employeeList,
android.R.layout.simple_list_item_1,
new String[] {"list"}, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(number, name);
return employeeNameNo;
}}
这里有什么问题,我从这里得到了代码http://codeoncloud.blogspot.in/2013/07/android-mysql-php-json-tutorial.html
它适用于他的 php 文件。
勾选 SimpleAdapter
class reference:
如您所见,构造函数采用以下参数:
SimpleAdapter (Context context,
List<? extends Map<String, ?>> data,
int resource,
String[] from,
int[] to)
- context Context: The context where the View associated with this SimpleAdapter is running
- data List: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
- resource int: Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
- from String: A list of column names that will be added to the Map associated with each item.
- to int: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
您将 SimpleAdapter
实例化为:
SimpleAdapter simpleAdapter = new SimpleAdapter(ClientActivity.this, employeeList,
android.R.layout.simple_list_item_1,
new String[] {"list"}, new int[] { android.R.id.text1 });
这意味着您的 employeeList
List
的每个元素都应该是包含键 "list".
的 Map
因此
employeeList.add(createEmployee("list", outPut));
应该向 List
添加一个 Map
,该条目具有键 "list" 和值 outPut
.
但是,您的 createEmployee()
方法创建了一个 HashMap
,其中包含交换键和值的单个条目。
你应该改变
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(number, name);
return employeeNameNo;
}
至
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
为什么我的 phone 上的列表是空的。有人能帮忙吗?它显示了正确的 7 个位置,但它们是空的。 我的 php 文件可能没问题。我使用 json 编码。
public class ClientActivity extends UserAreaActivity {
private String jsonResult;
private String url = "http://vinusek.000webhostapp.com/Client2.php";
private ListView listView;
private TextView nazwa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
listView = (ListView) findViewById(R.id.listView1);
nazwa = (TextView) findViewById(R.id.textView2) ;
accessWebService();
}
// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("result");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String number = jsonChildNode.getString("id");
String name = jsonChildNode.getString("login");
String pass = jsonChildNode.getString("haslo");
String outPut = number + "-" + name + "-" + pass;
employeeList.add(createEmployee("list", outPut));
nazwa.setText(outPut);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(ClientActivity.this, employeeList,
android.R.layout.simple_list_item_1,
new String[] {"list"}, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(number, name);
return employeeNameNo;
}}
这里有什么问题,我从这里得到了代码http://codeoncloud.blogspot.in/2013/07/android-mysql-php-json-tutorial.html 它适用于他的 php 文件。
勾选 SimpleAdapter
class reference:
如您所见,构造函数采用以下参数:
SimpleAdapter (Context context,
List<? extends Map<String, ?>> data,
int resource,
String[] from,
int[] to)
- context Context: The context where the View associated with this SimpleAdapter is running
- data List: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
- resource int: Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
- from String: A list of column names that will be added to the Map associated with each item.
- to int: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
您将 SimpleAdapter
实例化为:
SimpleAdapter simpleAdapter = new SimpleAdapter(ClientActivity.this, employeeList,
android.R.layout.simple_list_item_1,
new String[] {"list"}, new int[] { android.R.id.text1 });
这意味着您的 employeeList
List
的每个元素都应该是包含键 "list".
Map
因此
employeeList.add(createEmployee("list", outPut));
应该向 List
添加一个 Map
,该条目具有键 "list" 和值 outPut
.
但是,您的 createEmployee()
方法创建了一个 HashMap
,其中包含交换键和值的单个条目。
你应该改变
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(number, name);
return employeeNameNo;
}
至
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}