简体   繁体   中英

How to use multiple adapters on Android

I'm trying to create 2 activities, in the first one I'm showing the "nome" and the "tipo_pessoa" in a listview, I want to show the "cargo" in the second activity. As you can see in my parsed Json, I'm using 2 models, my question is, do I need to use 2 adapters in order to do that? here is what I tried so far:

public void parseJson(JSONObject json) throws JSONException {

        try {

            JSONArray dados = json.getJSONArray("dados");
            feedList = new ArrayList<ClientesModel>();
            contatoList = new ArrayList<ClientesContatosModel>();
            // parsing json object
            for (int i = 0; i < dados.length(); i++) {

                JSONObject item = dados.getJSONObject(i);
                ClientesModel mClientesModel = new ClientesModel();


                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("contatos");

                    for (int j = 0; j < contatos.length(); j++) {


                        JSONObject data = contatos.getJSONObject(j);

                        ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();


                        contatoList.add(mClientesContatoModel);

                        mClientesContatoModel.setNomeContato(data.optString("nome"));
                        mClientesContatoModel.setCargo(data.optString("cargo"));


                    }


                    feedList.add(mClientesModel);

                    System.out.println(contatoList);


            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

MainActivity:

public void updateList()  {
        feedListView= (ListView) findViewById(R.id.custom_list);

        feedListView.setVisibility(View.VISIBLE);
        progressbar.setVisibility(View.GONE);

        feedListView.setAdapter(new CustomListAdapter(this, feedList));


        feedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {

ntent intent = new Intent(FeedListActivity.this, FeedDetailsActivity.class);

            intent.putExtra("data", contatoList);
            startActivity(intent);
            }
        });
    }

the second Activity:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_details);



    ListView lv = (ListView) findViewById(R.id.listViewContatos);

    lv.setAdapter(new ContatosListAdapter(this, contatoList));

    feed = (ClientesContatosModel) this.getIntent().getSerializableExtra("data");






        TextView title = (TextView) findViewById(R.id.textView);
        title.setText(feed.getNomeContato());


    }

Now here is my log:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.javatechig.feedreader/com.javatechig.feedreader.FeedDetailsActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.access$800(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.javatechig.feedreader.ContatosListAdapter.getCount(ContatosListAdapter.java:33)
        at android.widget.ListView.setAdapter(ListView.java:480)
        at com.javatechig.feedreader.FeedDetailsActivity.onCreate(FeedDetailsActivity.java:33)

java line 33 is : lv.setAdapter(new ContatosListAdapter(this, contatoList));

The data from your first activity is not completely passed into the second activity. The error is from contatoList being null, which appears to be populated in the MainActivity, but only the feed data is passed using the intent.

You need to find a way to pass the contatoList data to your second activity as well. One way to do this is to pass your JSON string as an Extra in the second activity launch Intent (if it is not too large) just like you are passing "nome" and re-parse is in the second activity.

You could also store the data using Sqlite or even SharedPreferences.

And if you have two different displays, then you will need two different adapters.

I could solve it:

public void updateList() {


        feedListView= (ListView) findViewById(R.id.custom_list);
        feedListView.setVisibility(View.VISIBLE);
        progressbar.setVisibility(View.GONE);
        feedListView.setAdapter(new CustomListAdapter(this, feedList));

        final ListView V = (ListView) findViewById(R.id.contato_list);
        //V.setVisibility(View.VISIBLE);
        V.setAdapter(new ContatosListAdapter(this, contatoList));



        feedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                Object o = V.getItemAtPosition(position);
                ClientesContatosModel newsData = (ClientesContatosModel) o;


                Intent intent = new Intent(FeedListActivity.this, FeedDetailsActivity.class);
                intent.putExtra("feed", newsData);
                startActivity(intent);

            }

        });



    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM