我列出了两个清单。一个是“列表列表”,另一个是包含所有要制作项目的列表。我试图根据我在“列表列表”中单击的项目来过滤第二个列表。但是,当我尝试在过滤器中编码后添加一个项目时,根本没有显示任何项目。如果我可以使过滤器正常工作,那就太好了。我将在下面发布代码。
public class MainActivity extends AppCompatActivity {
ListView ListofList;
ArrayList<String> Lists;
ArrayAdapter<String> ListAdapter;
Button button;
ArrayList<String> Subject = new ArrayList<>();
int Size = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListofList = findViewById(R.id.LofL);
Lists = new ArrayList<String>();
ListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Lists);
ListofList.setAdapter(ListAdapter);
button = findViewById(R.id.AddList);
Lists.add("Test");
ListofList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getBaseContext(), ItemList.class);
String s = (String) ListofList.getItemAtPosition(position);
i.putExtra("Title", s);
i.putExtra("size", Size);
int a = 0;
for (String t : Subject) {
i.putExtra("Item" + a, t);
a++;
}
startActivityForResult(i, 2);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), CreateItem.class);
String message = "Please enter the name of the list below.";
i.putExtra("mess", message);
startActivityForResult(i, 1);
}
});
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
Bundle extras = getIntent().getExtras();
if (data == null) {
}
if (data != null) {
if (requestCode == 1) {
String item = (data.getStringExtra("item"));
Lists.add(item);
ListAdapter.notifyDataSetChanged();
}
}
if (requestCode == 2) {
ArrayList<String> subject = new ArrayList<String>();
int i = 0;
assert data != null;
int size = (data.getIntExtra("ListSize", 0));
Size = size;
while (i <= size) {
String item = (data.getStringExtra("item" + i));
subject.add(item);
i++;
}
Subject = subject;
}
}
}
public class ItemList extends AppCompatActivity {
static ArrayList<String> customItems;
ArrayAdapter<String> adapter;
ListView lvItems;
Button button;
Button button2;
ArrayAdapter<String> trashAdapter;
ArrayList<String> trash;
ListView itemtrash;
static TextView Title;
String Tit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
Title = findViewById(R.id.Title);
//set up Item List
customItems = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, customItems);
lvItems = (ListView) findViewById(R.id.lvItems);
//Set up trash List
trash = new ArrayList<String>();
trashAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, trash);
itemtrash = findViewById(R.id.itemtrash);
//Initialize Buttons
button = findViewById(R.id.AddItem);
button2 = findViewById(R.id.ClearTrash);
Bundle extras = getIntent().getExtras();
if (extras != null) {
((TextView)findViewById(R.id.Title)).setText(extras.getString("Title"));
Tit = extras.getString("Title");
int i = 0;
int size = (extras.getInt("size", 0));
while (i <= size) {
String item = (extras.getString("Item" + i));
if (item == null) {
break;
}
customItems.add(item);
i++;
}
}
CharSequence Titlefilter = Title.getText();
lvItems.setTextFilterEnabled(true);
ItemList.this.adapter.getFilter().filter(Titlefilter);
adapter.notifyDataSetChanged();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), CreateItem.class);
String message = "Please enter new item below.";
i.putExtra("mess", message);
startActivityForResult(i, 1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
trashAdapter.clear();
trashAdapter.notifyDataSetChanged();
}
});
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String s = adapter.getItem(position);
trashAdapter.add(s);
adapter.remove(s);
assert s != null;
adapter.notifyDataSetChanged();
trashAdapter.notifyDataSetChanged();
}
});
itemtrash.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String s = trashAdapter.getItem(position);
customItems.add(s);
trash.remove(s);
adapter.notifyDataSetChanged();
trashAdapter.notifyDataSetChanged();
}
});
lvItems.setAdapter(adapter);
itemtrash.setAdapter(trashAdapter);
}
@Override
public void onBackPressed() {
Intent i = new Intent();
int a = 0;
for (String s : customItems){
String t = adapter.getItem(a);
i.putExtra("item" + a, t);
a++;
}
int size = customItems.size();
i.putExtra("ListSize", size);
setResult(2, i);
finish();
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
Bundle extras = getIntent().getExtras();
if (data == null) {
}
if (data != null) {
String item = (data.getStringExtra("item"));
String title = Tit;
customItems.add(title + "\n\n" + item);
adapter.notifyDataSetChanged();
}
}
}
public class CreateItem extends AppCompatActivity {
Button addItem;
Button addCreate;
EditText Listitem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_item);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
((TextView)findViewById(R.id.textView)).setText(extras.getString("mess"));
}
Listitem = findViewById(R.id.editText);
addItem = findViewById(R.id.Add);
addCreate = findViewById(R.id.AddCreate);
addItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i2 = new Intent();
String Item = "";
if (Listitem.getText().toString() == null) {
}else {
Item = Listitem.getText().toString();
}
i2.putExtra("item", Item);
setResult(1, i2);
finish();
}
});
}
public void onBackPressed() {
finish();
}
}