登录屏幕第一次加载空白活动

时间:2017-03-01 18:45:05

标签: android json listview

我正在做一个具有LOGIN屏幕和带有信息的SECOND屏幕的应用程序。

从JSON检查用户名和密码。之后,意图将活动跳转到ApprovalsActivity.class,它将在LISTVIEW中显示来自第二个JSON的项目。

它就像一个帮助台系统,显示您登录后的任务。

一切正常(感谢StackOverFlow xD),但仅限于用户第二次登录后。

因此,当我第一次点击登录时,应用程序会跳转活动,但会跳转到空白活动。如果我单击“返回”按钮并再次登录,则会显示列表视图中的项目。

有人可以帮我吗?这是我第一次使用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
}

解决方案:

添加customizedListView.notifyDataSetChanged();行之后:

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

on OnPostExecute,因此listview适配器将知道更改的内容并且必须更新。

同时声明:

 CustomizedListClass customizedListView;

在OnCreate之外。

1 个答案:

答案 0 :(得分:0)

在初始化列表视图后,您是否尝试执行该任务? 或者尝试在onPoastExecutre上的任务结束后调用适配器notifyDataSetChanged ..