正如已经建议你可以做的是开始你的第二个活动与您,当您编辑只是点击listview
项目,或当你箱子一个新的没有通过任何数据。
通过这种方式,您可以编辑以及创建新的Note,而无需对代码进行太多更改。
int RESPONSE_CODE = 123; // from this code will will differentiate if
// the activity which we started actually did the task or not
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), NoteEditActivity.class);
intent.putString("para1","arg1");
intent.putString("para2","arg2");
intent.putString("para3","arg3");
intent.putString("para4","arg4");
startActivityForResult(intent, RESPONSE_CODE);
}
});
in NoteEditActivity.class
如果有东西,您可以收到您的参数。同时创建新的Note Note,para2,para3 & para4将为空。但是,如果注意已经存在,你将不得不编辑
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent myIntent = getIntent();
String arg1 = myIntent.getStringExtra("para1");
String arg2 = myIntent.getStringExtra("para2");
String arg3 = myIntent.getStringExtra("para3");
String arg4 = myIntent.getStringExtra("para4");
// here you have to check that they or not null to differentiate
// if it is a new Note to be created OR you have to edit.
// you should probably use some another way to figure this out, i am writing this because i would not be using it :)
}
一旦您完成创建或编辑你的注意,相关数据附加到的意图和完成活动这样
Intent intent = new Intent();
intent.putExtra("para1","edited_data_1");
intent.putExtra("para2","edited_data_2");
intent.putExtra("para3","edited_data_3");
intent.putExtra("para4","edited_data_4");
setResult(RESULT_OK, intent);
finish();
如果任务成功,您将设置结果RESULT_OK
连接的意图和你在以前做过的事情
你必须覆盖一个名为onActivityResult()
这种方法从活动哟接收数据的方法ü已经开始做一些任务,
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String arg1 = data.getStringExtra("para1");
String arg2 = data.getStringExtra("para2");
String arg3 = data.getStringExtra("para3");
String arg4 = data.getStringExtra("para4");
// if you receive the data here,
// you can add another element to List from which the -
//`ListView` is populated and call `notifyDataSetChanged()` on the adapter.
// like this `adapter.notifyDataSetChanged();` it will update the `listview` -
//with new data that you received from the `NoteEditActivity.class`
adapter.notifyDataSetChanged(); // I am assuming you would add the changed item to the List
}
}
}
更新,我创建了一个虚拟的代码为你和它的作品,我已经测试过它,我还上传了它在Github上,你可以检查它有
NoteListActivity.java
public class NoteListActivity extends Activity {
private ListView listView;
private ArrayList<String> strings;
public static final String NEW_ELEMENT = "NEW_ELEMENT";
public static final String ELEMENT_VALUE = "ELEMENT_VALUE";
public static final String ELEMENT_POSITION = "ELEMENT_POSITION";
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_list);
listView = (ListView) findViewById(R.id.NoteListActivity_listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = (String) listView.getItemAtPosition(position);
Intent intent = new Intent(getApplicationContext(), NoteEditActivity.class);
intent.putExtra(NEW_ELEMENT, false);
intent.putExtra(ELEMENT_POSITION, position);
intent.putExtra(ELEMENT_VALUE, text);
startActivityForResult(intent, 123);
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.NoteListActivity_fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), NoteEditActivity.class);
intent.putExtra(NEW_ELEMENT, true);
startActivityForResult(intent, 123);
}
});
setUpList();
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.simple_list_item_1, strings);
listView.setAdapter(adapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("NoteListActivity1",""+data.getStringExtra(NoteListActivity.ELEMENT_VALUE));
if (requestCode == 123) {
if (resultCode == RESULT_OK) {
Log.d("NoteListActivity2",""+data.getStringExtra(NoteListActivity.ELEMENT_VALUE));
boolean isNewElement = data.getBooleanExtra(NoteListActivity.NEW_ELEMENT, false);
if (isNewElement) {
strings.add("" + data.getStringExtra(NoteListActivity.ELEMENT_VALUE));
adapter.notifyDataSetChanged();
} else {
strings.set(data.getIntExtra(NoteListActivity.ELEMENT_POSITION, 0), "" + data.getStringExtra(NoteListActivity.ELEMENT_VALUE));
adapter.notifyDataSetChanged();
}
}
}
}
private void setUpList() {
strings = new ArrayList<>();
for (int i = 0; i < 4; i++) {
strings.add("Element_1_" + i);
}
}
}
activity_note_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.nimgade.pk.mytutorialapplication.MainActivity">
<LinearLayout 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:orientation="vertical"
tools:context="activities.list.first.NoteListActivity">
<ListView
android:id="@+id/NoteListActivity_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/NoteListActivity_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_add" />
</android.support.design.widget.CoordinatorLayout>
NoteEditActivity.java
public class NoteEditActivity extends Activity {
private EditText editText;
private Button button;
private boolean isNewElement;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_edit);
editText = (EditText) findViewById(R.id.NoteEditActivity_editText);
button = (Button) findViewById(R.id.NoteEditActivity_button);
Intent myIntent = getIntent();
isNewElement = myIntent.getBooleanExtra(NoteListActivity.NEW_ELEMENT, false);
if (!isNewElement) {
editText.setText("" + myIntent.getStringExtra(NoteListActivity.ELEMENT_VALUE));
position = myIntent.getIntExtra(NoteListActivity.ELEMENT_POSITION,0);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
if (text != null && text.trim().length() > 0) {
Intent intent = new Intent();
Log.d("NoteEditActivity",""+text);
intent.putExtra(NoteListActivity.ELEMENT_VALUE, text);
intent.putExtra(NoteListActivity.NEW_ELEMENT, isNewElement);
if (!isNewElement) {
intent.putExtra(NoteListActivity.ELEMENT_POSITION, position);
}
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(getApplicationContext(), "Kindly write a valid element name", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_note_edit.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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="activities.list.first.NoteEditActivity">
<EditText
android:id="@+id/NoteEditActivity_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/NoteEditActivity_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:textAllCaps="false" />
</LinearLayout>
http://stackoverflow.com/a/14292451/2435402这是一个伟大的答案帮助你实现你的逻辑。 – ElliotM
现在发送作品,但是当我将特定项目添加到列表视图时,它不会出现。 – user3402719
调用你的列表视图的适配器,并在发送对象时调用notifyDataSetChanged –