gpt4 book ai didi

php - 空列表 Android Studio

转载 作者:行者123 更新时间:2023-11-29 01:13:55 24 4
gpt4 key购买 nike

为什么我的手机上显示空列表。有人能帮忙吗?它显示了正确的 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)
  1. context Context: The context where the View associated with this SimpleAdapter is running
  2. 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"
  3. 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"
  4. from String: A list of column names that will be added to the Map associated with each item.
  5. 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;
}

关于php - 空列表 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41100056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com