尝试将列表视图中的变量信息传递给新活动

时间:2018-05-05 15:48:41

标签: java android listview

我是Android应用程序编程的新手,现在我已经苦苦挣扎了几天,只需点击一下就能从列表视图中的项目中传递信息。我在这里发布了我已经做过的事情。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private LeagueAdapter mLeagueAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLeagueAdapter = new LeagueAdapter();

        final ListView listLeague = (ListView) findViewById(R.id.listView);

        listLeague.setAdapter(mLeagueAdapter);


        // Handle clicks on the ListView
        listLeague.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int whichItem, long id) {

                /*
                    Create  a temporary League
                    Which is a reference to the League
                    that has just been clicked
                */
                // Create a new dialog window
                //DialogShowLeague dialog = new DialogShowLeague();

                // Send in a reference to the League to be shown
                //dialog.sendLeagueSelected(tempLeague);

                // Show the dialog window with the League in it
                //dialog.show(getFragmentManager(), "");


                String selected = ListView[whichItem];
                Intent i = new Intent(getApplicationContext(), BowlersActivity.class);
                i.putExtra("name", selected);
                startActivity(i);
            }
        });
    }

    public void createNewLeague(League n){

        mLeagueAdapter.addLeague(n);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        if (id == R.id.action_addLeague) {
            DialogNewLeague dialog = new DialogNewLeague();
            dialog.show(getFragmentManager(), "");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public class LeagueAdapter extends BaseAdapter {

        List<League> leagueList = new ArrayList<League>();

        @Override
        public int getCount() {
            return leagueList.size();
        }

        @Override
        public League getItem(int whichItem) {
            // Returns the requested league
            return leagueList.get(whichItem);
        }

        @Override
        public long getItemId(int whichItem) {
            // Method used internally
            return whichItem;
        }

        @Override
        public View getView(
                int whichItem, View view, ViewGroup viewGroup) {

            /*
                Prepare a list item to show our data
                The list item is contained in the view parameter
                The position of the data in our ArrayList is contained
                in whichItem parameter
            */


            // Has view been inflated already
            if(view == null){

                // No. So do so here
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                view = inflater.inflate(R.layout.single_league_list_item, viewGroup,false);

            }// End if

            // Grab a reference to all our TextView and ImageView widgets
            TextView txtTitle = (TextView) view.findViewById(R.id.txtTitle);

            // Hide any ImageView widgets that are not relevant
            League tempLeague = leagueList.get(whichItem);

            // Add the text to the heading and description
            txtTitle.setText(tempLeague.getTitle());

            return view;
        }

        public void addLeague(League n){

            leagueList.add(n);
            notifyDataSetChanged();

        }


    }

}

DialogNewLeague.java

public class DialogNewLeague extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_new_league, null);

        final EditText editTitle = (EditText) dialogView.findViewById(R.id.editTitle);
        Button btnCancel = (Button) dialogView.findViewById(R.id.btnCancelLeague);
        Button btnOK = (Button) dialogView.findViewById(R.id.btnAddLeague);

        builder.setView(dialogView).setMessage("Add New mLeague");

        btnCancel.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        // Handle the OK button
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // Create a new note
                League newLeague = new League();

                // Set its variables to match the users entries on the form
                newLeague.setTitle(editTitle.getText().toString());

                // Get a reference to MainActivity
                MainActivity callingActivity = (MainActivity) getActivity();

                // Pass newNote back to MainActivity
                callingActivity.createNewLeague(newLeague);

                // Quit the dialog
                dismiss();
            }
        });


        return builder.create();

    }

}

League.java

package rvogl.ca.mydialog;

public class League {

    private String mLeague;

    public String getTitle() {
        return mLeague;
    }

    public void setTitle(String mLeague) {
        this.mLeague = mLeague;
    }
}

BowlersActivty.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class BowlersActivity extends AppCompatActivity {

    League mLeague;
    String title;
    String editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bowlers);

        Bundle extras = getIntent().getExtras();
        String name = extras.getString("name");
        editText.setText(name);

    }
}

我已经浏览了本网站上几乎所有可以找到的帖子,并且我已经尝试了其中几个但没有成功。

此时,当我点击联赛列表视图中的某个项目时,应用程序就会崩溃。

任何关于如何实现这一目标的建议和建议都将受到赞赏。

我可以使用以下内容将信息提供给警报对话框:

DialogShowLeague

    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class DialogShowLeague extends DialogFragment {

        League mLeague;

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            LayoutInflater inflater = getActivity().getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.dialog_show_league, null);

            TextView txtTitle = (TextView) dialogView.findViewById(R.id.txtTitle);
            txtTitle.setText(mLeague.getTitle());


            Button btnOK = (Button) dialogView.findViewById(R.id.btnOK);

            builder.setView(dialogView).setMessage("Your mLeague");

            btnOK.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });



        return builder.create();
    }


    public void sendLeagueSelected(League leagueSelected) {
        mLeague = leagueSelected;
    }

}

使用类似的东西是不可能实现这一点的。我只是不知道用object替换dialogView的内容。

2 个答案:

答案 0 :(得分:0)

您可以在listview上传递值,如下面的代码所示。在您的模型类中实现Serializable接口,即League

listLeague.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

    @Override public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) { 
         League selected = (League)adaper.getItemAtPosition(pos);

         Intent i = new Intent(getApplicationContext(), BowlersActivity.class); 
         i.putExtra("name", (Serializable)selected);             
         startActivity(i); 
    }
 });

修改

用此

替换您的模型类
public class League implement Serializable { 
    private String mLeague; 

    public String getTitle() { 
        return mLeague; 
    } 

    public void setTitle(String mLeague) {
        this.mLeague = mLeague;
    } 
}

获取此类第二项活动的数据

String name = extras.getSerializableExtra("name").getTitle();

答案 1 :(得分:0)

我已设法使用以下代码解决这个问题:

MainActivity

mLeagueAdapter = new LeagueAdapter();

        final ListView listLeague = (ListView) findViewById(R.id.listView);

        listLeague.setAdapter(mLeagueAdapter);
        listLeague.setOnItemClickListener(new   AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) {

                String leagueName = mLeagueAdapter.getItem(pos).getTitle();
                Intent myIntent = new Intent(getBaseContext(), BowlersActivity.class);
                myIntent.putExtra("value1", leagueName);
                startActivity(myIntent);
            }
        });

在我的第二个活动BowlersActivity.java

    String savedExtra = getIntent().getStringExtra("value1");
    TextView myText = (TextView) findViewById(R.id.tvLeagueName);
    myText.setText(savedExtra);

我的问题是我试图从一个空的变量“选中”获得联盟名称。一旦我通过使用我的mLeagueAdapter得到我在列表视图中点击的联盟的位置,传递它。 putExtra不再是一个问题。

@Fazal Hussain非常感谢你的耐心和帮助,非常感谢。