Android Studio:如何将列表视图中的项目添加到列表视图 onClick
Posted
技术标签:
【中文标题】Android Studio:如何将列表视图中的项目添加到列表视图 onClick【英文标题】:Android Studio: How to add an item from listview1 to listview2 onClick 【发布时间】:2020-02-12 17:58:31 【问题描述】:对 android Studio 非常陌生,正在尝试解决以下问题;我有 2 个列表视图,并希望“onClick”将 listView1 中的项目添加到 listView2。
单击(和添加)的项目必须从 ArrayList1 --> ArrayList2 存储。
我该怎么做?
布局的屏幕截图。 https://imgur.com/a/7nJ0uX9
我已尝试按照 youtube 教程解决此问题,但它们似乎没有明确涵盖我的问题。
public class SearchFilter extends AppCompatActivity
private static final String TAG = "SearchActivity";
private ArrayAdapter adapter1;
private ArrayAdapter adapter2;
private ListView listView1;
private ListView listView2;
private String names1[] = new String[] "mitch", "luke", "samantha", "jessi", "janelle", "jordan", "rick";
private String [] names2 = new String[names1.length];
private List name1List = new ArrayList();
private List name2List = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_filter);
for(String a : names1)
name1List.add(a);
for (String b : names2)
name2List.add(b);
listView1 = (ListView) findViewById(R.id.listView1);
listView2 = (ListView) findViewById(R.id.listView2);
adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name1List);
listView1.setAdapter(adapter1);
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name2List);
listView2.setAdapter(adapter2);
EditText theFilter = (EditText) findViewById(R.id.searchFilter);
Log.d(TAG, "onCreate: Started");
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
name2List.add(name1List.get(position));
adapter2.notifyDataSetChanged();
);
theFilter.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
(SearchFilter.this).adapter1.getFilter().filter(charSequence);
@Override
public void afterTextChanged(Editable editable)
);
Logcat:
2019-10-16 06:16:38.843 29135-29191/? E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
2019-10-16 06:48:39.276 29135-29191/? E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
2019-10-16 07:52:39.608 29135-29191/? E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
2019-10-16 10:00:39.972 29135-29191/? E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
2019-10-16 10:00:41.539 536-536/? E/FirebaseInstanceId: Failed to start service while in background: java.lang.IllegalStateException: Not allowed to start service Intent act=com.google.firebase.INSTANCE_ID_EVENT pkg=com.google.android.apps.messaging cmp=com.google.android.apps.messaging/.fcm.BugleFirebaseInstanceIDService (has extras) : app is in background uid UidRecord7000ddf u0a54 RCVR idle change:idle|uncached procs:1 seq(0,0,0)
2019-10-16 12:12:47.116 3132-3205/com.google.android.youtube E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
2019-10-16 12:13:17.318 3132-3205/com.google.android.youtube E/FirebaseInstanceId: Token retrieval failed: QUOTA_EXCEEDED
2019-10-16 12:14:17.534 3132-3205/com.google.android.youtube E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
2019-10-16 12:16:17.706 3132-3205/com.google.android.youtube E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
2019-10-16 12:20:36.753 3132-3205/com.google.android.youtube E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
2019-10-16 12:28:44.679 3132-3205/com.google.android.youtube E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
2019-10-16 12:45:19.843 3132-3205/com.google.android.youtube E/FirebaseInstanceId: Token retrieval failed: INVALID_SENDER
【问题讨论】:
使用界面进行项目点击并代替列表视图使用 Recyclerview 以获得最佳性能.. 您的代码看起来不错。该错误与 Firebase 有关。 谢谢。你知道我该如何修改 firebase 问题吗? 【参考方案1】:这可能会有所帮助。 notifyDataSetChanged()
不足以刷新 ListView 数据,必须先调用 setAdapter() 才能正确加载信息。执行以下操作:-
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
【讨论】:
以上是关于Android Studio:如何将列表视图中的项目添加到列表视图 onClick的主要内容,如果未能解决你的问题,请参考以下文章
如何将 listView 放在 android studio 的滚动视图中?
仅将一部分数据显示到列表视图中并获取新活动 android studio 中的所有值?
如何正确连接并显示从 Firebase 到自定义列表视图 Android Studio 的数据