java - 如何为简单的 ListView 设置我的 listview.setOnItemClickListener()(仅使用一个类)
问题描述
我正在为杂货清单创建一个简单的 ListView。用户输入项目并创建列表。我无法让其他活动识别所选项目。我设置了共享项目,但它不发送列表。它确实会发送带有主题行的电子邮件。我相信这是因为我对列表项的 onclick 侦听器。
这是模拟器的图像。这显示了我打算分享的内容:
这是主要活动:
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 识别 SELECTED 项目,以便我的应用程序为它们执行活动,如共享、删除等?
解决方案
在您的代码中,您正在更改arrayAdapter
设置为适配器后的值。
在MainActivity
开头onCreate
groceryList= (ListView) findViewById(R.id.grocery_list);
//set mode
groceryList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//this adds checkbox mode
groceryList.setAdapter(arrayAdapter);
它以arrayAdapter 的当前值影响groceryList 的适配器。
但是,之后的几行:
arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_multiple_choice, aList);
只需在开始时创建您的适配器,并且永远不要重新影响它的价值(通过使用=
)。相反,处理它的参考(使用add
, clear
, remove
, etc
)。
所以你的代码会像
@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
并且您没有处理它,因为您已经用新值删除了它。
然后,当您想向列表中添加或项目时,请使用ArrayAdapter中的方法,例如
推荐阅读
- julia - 原子和键列表中分子的 2D 和/或 3D 图
- sql-server - sql server中的异常处理部分
- javascript - 如何同步 FileReader onload 函数?
- deep-learning - CycleGAN 用于未配对的图像到图像的转换
- node.js - 使用nodejs如何在循环中返回一个包含来自一批API调用的所有数据的对象?
- javascript - 即使在 subscribe() 函数中为其分配数据后,变量数据也不会保留在 HTML 中绑定
- ssl - Kubernetes 证书管理器 GoDaddy
- typescript - typescript:如何导出函数和接口
- javascript - 检查登录是否成功?
- ios - 在 UITableView 中以编程方式将 UIViews 添加到 UIStackView