简体   繁体   English

如何在Android上使用多个适配器

[英]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. 我正在尝试创建2个活动,在第一个活动中,我在列表视图中显示“ nome”和“ tipo_pessoa”,我想在第二个活动中显示“ cargo”。 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? 正如您在解析的Json中看到的那样,我正在使用2个模型,我的问题是,是否需要使用2个适配器才能做到这一点? 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)); Java第33行是: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. 该错误是由于contatoList为空引起的,它似乎已在MainActivity中填充,但仅使用意图传递了提要数据。

You need to find a way to pass the contatoList data to your second activity as well. 您还需要找到一种将contatoList数据传递到第二个活动的方法。 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. 一种方法是在第二个活动启动Intent中将JSON字符串作为Extra传递(如果它不是太大),就像在第二个活动中传递“ nome”并重新解析一样。

You could also store the data using Sqlite or even SharedPreferences. 您还可以使用Sqlite甚至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);

            }

        });



    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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