我需要帮助来设置我的listview.setOnItemClickListener();,对于一个简单的ListView(请仅使用1个类)

问题描述 投票:0回答:1

我正在为杂货店列表创建一个简单的ListView。用户输入项目并创建列表。我无法让其他活动认可所选项目。我已经设置了共享项目,但是它不发送列表。它的确发送带有主题行的电子邮件。我相信这是因为我的列表项是onclick侦听器。

这里是仿真器的图像。这显示了我打算分享的内容:enter image description here

这里是MainActivity:

public class MainActivity extends AppCompatActivity {

    //Global Variables for Grocery List
    ListView groceryList;
    ArrayList<String> aList =new ArrayList<String>();
    Button buttonAdd;
    EditText enterItem;
    ArrayAdapter<String> arrayAdapter;
    TextView item;
    String items = enterItem.getText().toString();

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

        //to include the action bar
        Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
        setSupportActionBar(toolbar);

        //Grocery List, ListView
        groceryList= (ListView) findViewById(R.id.grocery_list);
        //set mode
        groceryList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//this adds checkbox mode
        groceryList.setAdapter(arrayAdapter);


        //Set up User Input Button and Edit Text:
        buttonAdd=(Button)findViewById(R.id.button_add);
        enterItem = (EditText)findViewById(R.id.enter_item);
        item = (TextView)findViewById(R.id.textView);


        arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_multiple_choice, aList); //this adds the checkboxes

        //Add Item Button onClickListener
        buttonAdd.setOnClickListener(new View.OnClickListener(){
           @Override
           public void onClick(View v){
               aList.add(items);
               arrayAdapter.notifyDataSetChanged();
           }
        });

        // ListView on item selected listener.
        groceryList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // what do I put here?
            }
        });
    }

    //Create options menu, it will show the three dots, in the home screen-action bar:
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_home, menu); //fill it with the menu
        return super.onCreateOptionsMenu(menu);
    }

    //To determine what menu_main item is selected code:
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            //write cases to check which menu_main item is selected and to implement actions
            case R.id.share:
                //Share your grocery list with a helpful friend
                Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                shareIntent.setType("text/*");
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Grocery List");
                shareIntent.putExtra(Intent.EXTRA_TEXT, onContextItemSelected(item));
                startActivity(Intent.createChooser(shareIntent, "Share via"));

                Toast.makeText(this, "Shared It!", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.viewList:
                //Click to view a list of all of your saved grocery items
                Toast.makeText(this, "View All Items", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.save:
                //Save selected items to add to the "View All Items" list.
                Toast.makeText(this, "Items Are Saved", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.delete:
                //Delete selected items from the Grocery List
                Toast.makeText(this, "Grocery List Items Deleted", Toast.LENGTH_SHORT).show();
                return true;

            default:
                //unknown error

            return super.onOptionsItemSelected(item);
        }


    }
}

这是我的activity_mail.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include
        android:id="@+id/action_bar"
        layout="@layout/action_bar_home"></include>

    <EditText
        android:id="@+id/enter_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/enter_item"
        android:inputType="text"
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/button_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_item"
        android:layout_gravity="center"/>

    <ListView
        android:id="@+id/grocery_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

最后,这是item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />

</LinearLayout>

我如何让我的OnClickListener识别选定的项目,让我的应用为它们执行活动,如共享,删除等?

java android listview onclicklistener
1个回答
0
投票

在您的代码中,您已经将arrayAdapter的值设置为适配器。

MainActivitybeginingonCreate

groceryList= (ListView) findViewById(R.id.grocery_list);
//set mode
groceryList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//this adds checkbox mode
groceryList.setAdapter(arrayAdapter);

它以arrayAdapter的当前值影响杂货店清单的适配器。

但是,之后几行:

arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_multiple_choice, aList);

只需在开始时创建适配器,就不会影响其值(使用=)。相反,请使用其引用(使用addclearremoveetc)。

所以您的代码会像

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

    //to include the action bar
    Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
    setSupportActionBar(toolbar);

    // Affect the arrayAdapter
    arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_multiple_choice, aList); 

    //Grocery List, ListView
    groceryList= (ListView) findViewById(R.id.grocery_list);
    //set mode
    groceryList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//this adds checkbox mode

    // and then use it as the adapter but never '=' it again
    groceryList.setAdapter(arrayAdapter);


    //Set up User Input Button and Edit Text:
    buttonAdd=(Button)findViewById(R.id.button_add);
    enterItem = (EditText)findViewById(R.id.enter_item);
    item = (TextView)findViewById(R.id.textView);


    ...
}

[如果不这样做,则ListView的适配器将是arrayAdapter的“旧”值,并且您不使用它,因为您已用新值将其擦除。

希望我很清楚并且会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.