我有一个ListView的主要活动。然后,我有第二个活动,其中包含用于用户输入的文本框和一个按钮。当用户输入输入并单击按钮时,它应该将项目添加到主活动中的ListView,但由于某种原因,ListView不会使用新数据刷新。关于如何解决这个问题的任何想法?
答案 0 :(得分:0)
Create an adapter class for your listView.
class MyAdapter extends BaseAdapter {
ArrayList<Item> items;
// Override all other needed methods.
//Add method to add items to list from outside.
public void addItems(Item item) {
items.add(item);
adapter.notifyDataSetChanged();
}
}
在您的活动中 // Crate MyAdapter实例分配给listview,如 -
listView,setAdapter(adapter);
Item item= new Item();
//在item中添加必要的属性。并将项目添加到列表中。
adapter.addItem(item);
这是整体想法。环顾四周。应该工作。
答案 1 :(得分:0)
您的第二个活动B无法在活动a中看到您的数据资源,因此,当您在活动B中添加新项目时,您必须通知活动A
发生了变化。 这可以使用broadcastreceive获得。 当您将活动B中的项目sendBroadcastreceive添加到活动A. 活动A收到更改后将其添加到dataresource,并且仅在通知您的适配器后。
请参阅下面的示例。它有效!
MainActivity.java
public class MainActivity extends AppCompatActivity {
static String[] bike = new String[]{
"Ducati", "Honda", "BMW", "Bimota", "Kawasaky", "Yahamaha", "Suzuky", "Harlay-Davinson",
"Fantic", "Morini", "Hasquarna", "Aprilia", "Guzzi", "Beta", "Borile", "Buell", "Kymco", "Ktm"
};
ArrayList<String> alBike = new ArrayList<>(Arrays.asList(bike));
private ArrayAdapter adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
Button button = (Button) findViewById(R.id.button2);
adapter = new myadapter(getBaseContext(),0);
listView.setAdapter(adapter);
IntentFilter intentFilter =new IntentFilter();
intentFilter.addAction("ITEM_ADDED");
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// Log.i("bogo",action);
switch (action) {
case "ITEM_ADDED":
String newitem = intent.getStringExtra("newitem");
alBike.add(newitem);
adapter.notifyDataSetChanged();
break;
}
}
};
registerReceiver(broadcastReceiver, intentFilter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),FormActivity.class));
}
});
}
public class myadapter extends ArrayAdapter<String> {
public myadapter(Context context, int resource) {
super(context, resource);
}
@Override
public int getCount() {
return alBike.size();
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.listviewrow, parent, false);
TextView textView = (TextView) row.findViewById(R.id.textView);
textView.setText(alBike.get(position));
return row;
}
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bogo.com.additemtolistview.MainActivity">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new item"
android:id="@+id/button2"
android:layout_above="@+id/listView"
android:layout_alignParentEnd="true"
android:layout_marginEnd="198dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true" />
</LinearLayout>
FormActivity.java(您的活动B)
package bogo.com.additemtolistview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class FormActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.editText);
String newitem = editText.getText().toString();
Intent intent = new Intent("ITEM_ADDED");
intent.putExtra("newitem",newitem);
sendBroadcast(intent);
finish();
}
});
}
}
activity_form.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="bogo.com.additemtolistview.FormActivity">
<EditText
android:layout_width="220dp"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="78dp"
android:layout_marginTop="60dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/editText" />
</RelativeLayout>
listviewrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="@android:color/black"
android:id="@+id/textView" />
</LinearLayout>
</LinearLayout>