如何在没有数据库的情况下将我的添加活动中的ListView项目静态添加到主活动的ListView静态?
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listviewitemclick();
Button btn=(Button)findViewById(R.id.add);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(getApplicationContext(), Add.class);
startActivity(intent);
}
});
}
private void listviewitemclick(){
ListView lv=(ListView)findViewById(R.id.listView1);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(getApplicationContext(), UpdateDel.class);
startActivity(intent);
}
});
}}
activity_main.xml中
<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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Shopping App"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp" >
</ListView>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/listView1"
android:layout_marginLeft="16dp"
android:text="add"
/>
Add.java
public class Add extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
}
public void adduser(View v){
Intent i=new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}
activity_add.xml
<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=".Add" >
<EditText
android:id="@+id/usret"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:ems="10"
android:hint="UserName" />
<requestFocus />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/usret"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="Contact" >
</EditText>
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Address" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="Total Purchase" />
<Button
android:id="@+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:text="Add"
android:onClick="addtask"/>
这是我无法找到如何在listitem中添加编辑框值的代码,你可以帮助我吗
答案 0 :(得分:1)
以startActivityForResult开始第二个活动,并使用setResult()方法将数据从Activity2发送回Activity1。在activity1中,您需要覆盖onActivityResult以使用Activity2中的EditText数据更新TextView
例如:
从Acivity1启动Activity2为:
Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, 1);
Activity2中的使用setResult来发回数据:
Intent intent = new Intent();
intent.putExtra("edittextvalue","value_here");
setResult(RESULT_OK, intent);
finish();
并在First Activity中接收数据为onActivityResult:
onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String stredittext=data.getStringExtra("edittextvalue");
//list.add(stredittext); for Adding a data to listview and notifydataset changed by listview.notifydatasetchanged();
}
}
答案 1 :(得分:0)
您可以使用startActivityForResult()。如果你不想使用它,我会告诉你更难的方式。
我假设第一次列表视图为空。
现在,如果您要从添加用户转到MainActivity,那么您只需要使用putEXtra方法传递editText的数据。以下是代码段:
String textOfEt1 = editTextReffernce1.getText().toString();
String textOfEt2 = editTextReffernce1.getText().toString();
Intent i = new Intent(this, MainActivity.class);
i.putExtra("key1", textOfEt1); //set key value pair
i.putExtra("key2", textOfEt2);
startActivity(i);
阅读Intent类及其方法,您将了解putExtra方法是什么。
现在来到您的MainActivity:
在onCreate方法中:
if(getIntent().getExtras() != null)
{
Bundle bd = getintent().getExtras();
String name1 = bd.getString("key1"); //get value by key
String name2 = bd.getString("key");
String[] strArray = {name1, name2};
ArrayAdapter ad = new ArrayAdapter(this, android.R.layout.simple_list_item_1, strArray);
yourListViewRefference.setAdapter(ad);
}