我正在做一個具有登錄屏幕和第二個屏幕信息的應用程序。登錄屏幕在第一次加載空白活動
從JSON中檢查用戶名和密碼。之後,意圖將活動跳轉到ApprovalsActivity.class,這將在LISTVIEW中顯示來自第二個JSON的項目。
這就像展示你有你在登錄後的任務的幫助桌面系統。
一切正常(感謝StackOverflow的XD),但用戶登錄後第二次只。
所以,當我首先點擊登錄,應用程序跳轉活動,但空白。如果我單擊BACK按鈕並再次登錄,則會出現列表視圖中的項目。
任何人都可以幫助我嗎?這是我第一次用JSON做更復雜的事情。
MainActivity(LoginScreen):
public class MainActivity extends AppCompatActivity {
EditText usernameWidget;
EditText passwordWidget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
usernameWidget = (EditText) findViewById(R.id.tv_username);
passwordWidget = (EditText) findViewById(R.id.tv_password);
}// END ON CREATE
public class DownloadTask extends AsyncTask<String, Void, String> {
String message = "message";
String loginSuccess;
String pessoaFisicaId = "PessoaFisicaId";
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
int data = reader.read();
while (data != -1){
char current = (char) data; // each time creates a char current
result += current;
data = reader.read();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
} // END doInBackground
//Method called when the doInBack is complete
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
loginSuccess = jsonObject.getString("success");
pessoaFisicaId = jsonObject.getString("PessoaFisicaId");
message = jsonObject.getString("message");
}catch(JSONException e) {
e.printStackTrace();
}// END CATCH
if (loginSuccess.contains("true")){
Intent intent = new Intent(getApplicationContext(), ApprovalsActivity.class);
intent.putExtra("pessoaFisicaId", pessoaFisicaId);
startActivity(intent);
}else if (loginSuccess.contains("false")){
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
}// END POST EXECUTE
}// END Download Task
public void login(View view) {
String user = usernameWidget.getText().toString();
String pass = passwordWidget.getText().toString();
String stringJSON = "URLHIDDENlogin=" + user + "&senha=" + pass;
DownloadTask task = new DownloadTask();
task.execute(stringJSON);
}// END LOGIN
ApprovalsActivity(登錄後屏幕採用正確的用戶名和密碼通過):
public class ApprovalsActivity extends AppCompatActivity {
public static List<String> getChamadoStringListFromJsonArray = new ArrayList<String>();
public static List<String> getSolicitanteStringListFromJsonArray = new ArrayList<String>();
public static List<String> getItemStringListFromJsonArray = new ArrayList<String>();
public static String chamado;
public static String solicitante;
public static String itemDeCatalogo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aprovacoes);
Intent intent = getIntent();
String pessoaFisicaId = intent.getExtras().getString("pessoaFisicaId");
String stringJSON = "URLHIDDEN" + pessoaFisicaId;
DownloadTask task = new DownloadTask();
task.execute(stringJSON);
ListView listApprovals = (ListView) findViewById(R.id.list_aprovacoes);
CustomizedListClass customizedListView = new CustomizedListClass(ApprovalsActivity.this);
listApprovals.setAdapter(customizedListView); //Layout inflator
listApprovals.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.aprovacoes_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
int data = reader.read();
while (data != -1){
char current = (char) data; // each time creates a char current
result += current;
data = reader.read();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonarray = jsonObject.getJSONArray("dados");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonPart = jsonarray.getJSONObject(i);
chamado = jsonPart.getString("Chamado");
solicitante = jsonPart.getString("Solicitante");
itemDeCatalogo = jsonPart.getString("ItemDeCatalogo");
getChamadoStringListFromJsonArray.add(chamado);
Log.i("****getChamado", String.valueOf(getChamadoStringListFromJsonArray));
getSolicitanteStringListFromJsonArray.add(solicitante);
Log.i("****getsolicitante", String.valueOf(getSolicitanteStringListFromJsonArray));
getItemStringListFromJsonArray.add(itemDeCatalogo);
Log.i("****getItem", String.valueOf(getItemStringListFromJsonArray));
}
}catch(JSONException e) {
e.printStackTrace();
}// END CATCH
return result;
} // END doInBackground
//Method called when the doInBack is complete
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}// END POST EXECUTE
}// END Download Task
}
CustomizedListClass(顯示在列表上的項目) :
public class CustomizedListClass extends BaseAdapter {
Context context;
LayoutInflater mLayoutInflater;
TextView txtChamado;
TextView txtSolicitante;
TextView txtItemDeCatalogo;
public CustomizedListClass(Context context) {
this.context = context;
//Instantiate the layoutinflater object to use the layout inflater service on a object context
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() { //count the objects
return getChamadoStringListFromJsonArray.size(); //return the length of the array
}
@Override
public Object getItem(int position) { //gets an specific item in the array
return position; //return the specific value in array animalImages in the position position(variable)
}
@Override
public long getItemId(int position) { //returns the item ID of our objects
return position; //The ID is the position of the item in the array
}
@Override
public View getView(final int position, View view, final ViewGroup parent) {
view = mLayoutInflater.inflate(R.layout.customized_list_view, null);
txtChamado = (TextView) view.findViewById(R.id.txtChamado);
txtSolicitante = (TextView) view.findViewById(R.id.txtSolicitante);
txtItemDeCatalogo = (TextView) view.findViewById(R.id.txtItemDeCatalogo);
String oldChamado = txtChamado.getText().toString();
String newChamado = getChamadoStringListFromJsonArray.get(position);
txtChamado.setText(oldChamado + newChamado);
String oldSolicitante = txtSolicitante.getText().toString();
String newSolicitante = getSolicitanteStringListFromJsonArray.get(position);
txtSolicitante.setText(oldSolicitante + newSolicitante);
String oldItemDeCatalogo = txtItemDeCatalogo.getText().toString();
String newItemDeCatalogo = getItemStringListFromJsonArray.get(position);
txtItemDeCatalogo.setText(oldItemDeCatalogo + newItemDeCatalogo);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+ getChamadoStringListFromJsonArray.get(position), Toast.LENGTH_LONG).show();
}
});
return view;
}// END METHOD
}
SOLUTION:
Add customizedListView.notifyDataSetChanged();在該行之後:
getItemStringListFromJsonArray.add(itemDeCatalogo);
Log.i("****getItem", String.valueOf(getItemStringListFromJsonArray));
OnPostExecute,所以listview適配器將知道一些更改,並且必須更新。
而且聲明:
CustomizedListClass customizedListView;
的OnCreate中之外。
等第一次嘗試它的工作正確,但連續嘗試不? – Remario
恰恰相反。第一次嘗試進入空白屏幕。第二個轉到右側的屏幕,即listview。 – BlitzkriegBlue
你看過屏幕的過渡嗎?從視圖A(登錄)轉換到視圖B(ListView)時,是否所有設置都正確?你兩次都做同樣的事情嗎?考慮添加日誌記錄(System.out.println()或Log.e(「debugger」,[message])),以便您可以查看/如果某些內容無法正常運行。 – Zoe