我试图查看" enderecos"在另一个布局中的值,我正在解析json并使用volley适配器,我正在关注this教程并尝试调整其中使用的onClick函数。但是,当我切换到新的布局时,它没有显示任何内容
我错过了什么?我对android开发很新。
public class MainActivity extends Activity {
private String TAG = this.getClass().getSimpleName();
private ListView lstView;
private RequestQueue mRequestQueue;
private ArrayList<ClientesModel> arrNews ;
private ArrayList<ClientesContatosModel> arrContatos ;
private LayoutInflater lf;
private VolleyAdapter va;
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lf = LayoutInflater.from(this);
arrNews = new ArrayList<ClientesModel>();
arrContatos = new ArrayList<ClientesContatosModel>();
va = new VolleyAdapter();
lstView = (ListView) findViewById(R.id.listView);
lstView.setAdapter(va);
mRequestQueue = Volley.newRequestQueue(this);
String url = "http://192.168.1.17/mais-api/api/clientes/index.php";
pd = ProgressDialog.show(this,"Please Wait...","Please Wait...");
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG,response.toString());
try {
parseJson(response);
} catch (JSONException e) {
e.printStackTrace();
}
va.notifyDataSetChanged();
pd.dismiss();
; }
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
error.printStackTrace();
pd.hide();
}
}) {
@Override
public Map<String, String> getParams() throws com.android.volley.AuthFailureError{
Map<String, String> params = new HashMap<String, String>();
params.put("email", "rm@.com.br");
params.put("senha", "xxx");
return params;
}
};
mRequestQueue.add(jr);
}
private void parseJson(JSONObject json) throws JSONException{
try{
JSONArray dados = json.getJSONArray("dados");
for(int i=0;i<dados.length();i++) {
JSONObject item = dados.getJSONObject(i);
ClientesModel mClientesModel = new ClientesModel();
ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();
/* cadastra os dados necessários no objeto no modelo */
mClientesModel.setId(item.optInt("id"));
mClientesModel.setNome(item.optString("nome"));
mClientesModel.setTipo_pessoa(item.optString("tipo_pessoa"));
mClientesModel.setInformacoes_adicionais(item.optString("informacoes_adicionais"));
mClientesModel.setCpf(item.optString("cpf"));
mClientesModel.setCnpj(item.optString("cnpj"));
JSONArray contatos = item.getJSONArray("enderecos");
for(int j=0;j<contatos.length();j++){
JSONObject data = contatos.getJSONObject(j);
mClientesContatoModel.setNome(data.optString("endereco"));
}
arrNews.add(mClientesModel);
arrContatos.add(mClientesContatoModel);
}
}
catch(Exception e){
e.printStackTrace();
}
}
public class VolleyAdapter extends BaseAdapter{
@Override
public int getCount() {
return arrNews.size();
}
@Override
public Object getItem(int i) {
return arrNews.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
final ViewHolder vh ;
if(view == null){
vh = new ViewHolder();
view = lf.inflate(R.layout.row_listview,null);
vh.tvTitle = (TextView) view.findViewById(R.id.txtTitle);
vh.tvDesc = (TextView) view.findViewById(R.id.txtDesc);
vh.tvDate = (TextView) view.findViewById(R.id.txtDate);
vh.tvContato = (TextView) view.findViewById(R.id.txtNome);
view.setTag(vh);
}
else{
vh = (ViewHolder) view.getTag();
}
ClientesModel nm = arrNews.get(i);
vh.tvTitle.setText(nm.getNome());
vh.tvDesc.setText(nm.getTipo_pessoa());
final ClientesContatosModel nm2 = arrContatos.get(i);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent in = new Intent(getApplicationContext(),
ContatosView.class);
in.putExtra("endereco", "nome");
startActivity(in);
}
});
return view;
}
public class ViewHolder{
TextView tvTitle;
TextView tvDesc;
TextView tvDate;
TextView tvContato;
}
}
}
ContatosView:
public class ContatosView extends MainActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contatos_view);
Intent in = getIntent();
// Get JSON values from previous intent
String name = in.getStringExtra("nome");
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.txtNome);
lblName.setText(name);
}
}
答案 0 :(得分:0)
我设法解决了这个问题,就像我被告知要做的那样,使用Intent:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClientesContatosModel md = arrContatos.get(i);
Intent in = new Intent(getApplicationContext(),
ContatosView.class);
in.putExtra("endereco",md.getNome());
startActivity(in);
}
});
ContatosView:
public class ContatosView extends MainActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contatos_view);
Intent in = getIntent();
// Get JSON values from previous intent
String name = in.getStringExtra("endereco");
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.txtNome);
lblName.setText(name);
}
}
感谢您的帮助!