2017-03-01 11 views
0

ログイン画面と2番目の情報を持つアプリを実行しています。初めてログイン画面に空白が表示される

ユーザー名とパスワードはJSONからチェックされます。その後、アクティビティがApprovalsActivity.classにジャンプし、2番目のJSONの項目がLISTVIEWに表示されます。

それあなたがログインした後、あなたが持っているタスクを示すヘルプデスクシステムのようなものです。

すべての作品(感謝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 
} 
解決策:

OnPostExecuteには

getItemStringListFromJsonArray.add(itemDeCatalogo); 
       Log.i("****getItem", String.valueOf(getItemStringListFromJsonArray)); 

が含まれているため、変更されたことを認識して更新する必要があります。

も宣言:OnCreateの外

CustomizedListClass customizedListView; 

を。

+0

最初の試行では正しく動作しますが、連続試行はありませんか?それどころか、 – Remario

+0

。 1回目の試行は空白の画面になります。 2番目の画面は右側の画面であるlistviewに移動します。 – BlitzkriegBlue

+0

画面からの移行を調べましたか?ビューA(ログイン)からビューB(リストビュー)に移行すると、すべてが正しく設定されていますか?あなたは同じことを何回もやりますか?ロギング(System.out.println()またはLog.e( "debugger"、[message]))を追加することを検討して、何かが正しく機能しない場合に/を確認することができます。 – Zoe

答えて

0

リストビューが初期化された後にタスクを実行しようとしましたか? または、onPoastExecutreのタスク終了後にアダプタnotifyDataSetChangedを呼び出してみてください。

+0

ログインをクリックすると、インテントはリストビューのアクティビティにジャンプします。 notifyDataSetChangedは使用しませんでした。したほうがいい? (私はそれを使用する方法を正しく思い出さないが、それはいくつかの問題を解決することを知っている)。 – BlitzkriegBlue

+0

私はnotifyDataSetChangedの呼び出しがあなたの問題を解決すると信じています。 – Luci

+0

あなたの仕事は、あなたの活動が作成され、リストが既に読み込まれた後に終了するでしょう。タスクが終了してもデータリストに新しいデータが含まれていると、リストビュー/アダプタにデータセットが変更されたことが通知され、リストビューに「新しい」アイテムがロードされます。 – Luci

関連する問題