我遇到了问题。基本上我的名单没有显示。
<{1>}Log.d(TAG, "\nDato 1: "+fechaini );
这些行显示正确的信息。
但是当我开始活动时没有任何事情发生(节目)。
package info.androidhive.loginandregistration.activity;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import info.androidhive.loginandregistration.R;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
public class Main4Activity extends ActionBarActivity {
private static final String TAG_RESULTS="result";
private static final String TAG_FI = "fechaini";
private static final String TAG_FF = "fechafin";
private static final String TAG_USR ="user";
private static final String TAG_MAIL ="email";
private static final String TAG = Main4Activity.class.getSimpleName();
String myJSON;
JSONArray reservas = null;
ArrayList<HashMap<String, String>> reservaslist;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
list = (ListView) findViewById(R.id.ListView);
reservaslist = new ArrayList<HashMap<String,String>>();
getData();
}
public void getData() {
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://10.238.21.136:8081/android_login_api/revisar.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
reservas = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<reservas.length();i++){
JSONObject c = reservas.getJSONObject(i);
String fechaini = c.getString(TAG_FI);
String fechafin = c.getString(TAG_FF);
String user = c.getString(TAG_USR);
String email = c.getString(TAG_MAIL);
Log.d(TAG, "\nDato 1: "+fechaini );
Log.d(TAG, "\nDato 2: "+fechafin );
Log.d(TAG, "\nDato 3: "+user );
Log.d(TAG, "\nDato 4: "+email );
HashMap<String,String> reservaciones = new HashMap<String,String>();
reservaciones.put(TAG_FI,fechaini);
reservaciones.put(TAG_FF,fechafin);
reservaciones.put(TAG_USR,user);
reservaciones.put(TAG_MAIL,email);
reservaslist.add(reservaciones);
}
ListAdapter adapter = new SimpleAdapter(
Main4Activity.this, reservaslist, R.layout.activity_main4,
new String[]{TAG_FI,TAG_FF,TAG_USR,TAG_MAIL},
new int[]{R.id.fechaini, R.id.fechafin, R.id.user,R.id.email}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
日志
04-10 14:50:53.730 216-358/? I/BufferQueue: [info.androidhive.loginandregistration/info.androidhive.loginandregistration.activity.MainActivity](this:0xb8103208,id:6815,api:1,p:-1,c:216) disconnect: api=1
04-10 14:50:53.731 216-358/? I/BufferQueue: [info.androidhive.loginandregistration/info.androidhive.loginandregistration.activity.MainActivity](this:0xb8103208,id:6815,api:1,p:-1,c:216) getReleasedBuffers: returning mask 0xffffffff
04-10 14:50:53.735 705-717/? V/ActivityManager: ACT-Activity idle: Token{4310e790 ActivityRecord{4310e628 u0 info.androidhive.loginandregistration/.activity.Main4Activity t496}}
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 1: 2017-04-10 10:56:00
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 2: 2017-04-10 11:56:00
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 3: A
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 4: a
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 1: 2017-04-10 15:08:00
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 2: 2017-04-10 23:08:00
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 3: A
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 4: a
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 1: 2017-04-11 11:21:00
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 2: 2017-04-11 12:21:00
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 3: A
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 4: a
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 1: 2017-04-12 11:23:00
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 2: 2017-04-12 12:23:00
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 3: A
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 4: a
04-10 14:50:53.857 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 1: 2017-04-14 11:52:00
04-10 14:50:53.858 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 2: 2017-04-14 12:52:00
04-10 14:50:53.858 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 3: A
04-10 14:50:53.858 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 4: a
04-10 14:50:53.858 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 1: 2017-04-19 11:53:00
04-10 14:50:53.858 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 2: 2017-04-19 12:53:00
04-10 14:50:53.858 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 3: A
04-10 14:50:53.858 5769-5769/info.androidhive.loginandregistration D/Main4Activity: Dato 4: a
04-10 14:50:53.871 5769-5769/info.androidhive.loginandregistration D/ListView: mSelectorRect.setEmpty in layoutChildren this=android.widget.ListView{42513b88 VFED.VC. ......I. 0,0-0,0 #7f0c0086 app:id/ListView}
04-10 14:50:53.889 705-763/? D/ActivityManager: ACT-IDLE_NOW_MSG from windowsVisible() for idle: ActivityRecord{4310e628 u0 info.androidhive.loginandregistration/.activity.Main4Activity t496}
04-10 14:50:53.894 5769-5769/info.androidhive.loginandregistration V/ActivityThread: Finishing stop of ActivityRecord{422a7668 token=android.os.BinderProxy@422a6c28 {info.androidhive.loginandregistration/info.androidhive.loginandregistration.activity.MainActivity}}: show=false win=com.android.internal.policy.impl.PhoneWindow@422a9fb8
04-10 14:50:53.894 5769-5769/info.androidhive.loginandregistration V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =ViewRoot{422b8288 info.androidhive.loginandregistration/info.androidhive.loginandregistration.activity.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{422aa990 I.E..... R....... 0,0-720,1184}
04-10 14:50:53.894 5769-5769/info.androidhive.loginandregistration D/ActivityThread: ACT-STOP_ACTIVITY_HIDE handled : 0 / android.os.BinderProxy@422a6c28
04-10 14:50:53.906 216-216/? I/BufferQueue: [info.androidhive.loginandregistration/info.androidhive.loginandregistration.activity.MainActivity](this:0xb8103208,id:6815,api:1,p:-1,c:-1) consumerDisconnect
04-10 14:50:53.906 216-216/? I/BufferQueue: [info.androidhive.loginandregistration/info.androidhive.loginandregistration.activity.MainActivity](this:0xb8103208,id:6815,api:1,p:-1,c:-1) ~BufferQueue
04-10 14:51:53.439 705-1139/? V/WindowManager: Changing focus from Window{42948330 u0 info.androidhive.loginandregistration/info.androidhive.loginandregistration.activity.Main4Activity} to Window{42c235f8 u0 dream} Callers=com.android.server.wm.WindowManagerService.addWindow:2756 com.android.server.wm.Session.addToDisplay:163 android.view.IWindowSession$Stub.onTransact:111 com.android.server.wm.Session.onTransact:126
04-10 14:51:53.566 5769-5769/info.androidhive.loginandregistration V/InputMethodManager: START INPUT: com.android.internal.policy.impl.PhoneWindow$DecorView{42509348 V.E..... R.....I. 0,0-720,1184} ic=null tba=android.view.inputmethod.EditorInfo@42544860 controlFlags=#100
04-10 14:51:53.568 5769-5769/info.androidhive.loginandregistration W/InputMethodManager: startInputInner : InputBindResult == null
04-10 14:51:53.587 5769-5769/info.androidhive.loginandregistration D/AbsListView: onWindowFocusChanged: hasWindowFocus=false, this=android.widget.ListView{42513b88 VFED.VC. ......I. 0,0-0,0 #7f0c0086 app:id/ListVie
答案 0 :(得分:0)
ListAdapter adapter =new SimpleAdapter(context, data, resource, from, to);
第三个参数是资源ID,用于为您有文本视图的列表项设置布局(xml文件)。因此,您应该设置放置R.id.fechaini,R.id.fechafin,R.id.user和R.id.email的布局文件。如果它们位于相同的xml文件中,则应定义新的xml文件。