2017-02-10 83 views
0

我是android新手,我搜索了很多,但找不到合适的答案。有人可以帮助我吗? 我有两个活动,活动1包含一个列表视图,活动2是一个编辑视图,我想要做的是在点击listview上的项目后,它会进入活动2,操作数据点击提交按钮(提交方法) ,它将返回到列表视图,并且列表视图将显示更新后的结果。 这里是我的活性起着1码如何从其他活动更新ListView

public class I4ComponentActivity extends AppCompatActivity { 
private String quantity; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_component); 

    final ArrayList<ComponentDetail> details = new ArrayList<ComponentDetail>(); 

    details.add(new ComponentDetail("Screen", "1")); 
    details.add(new ComponentDetail("Camera", "2")); 
    details.add(new ComponentDetail("Battery", "5")); 
    details.add(new ComponentDetail("Change Port", "10")); 
    details.add(new ComponentDetail("Speaker", "5")); 
    details.add(new ComponentDetail("Power Key", "14")); 
    details.add(new ComponentDetail("Screen Cable", "24")); 

    ListView listView = (ListView) findViewById(R.id.component_list); 
    DetailAdapter detailAdapter = new DetailAdapter(I4ComponentActivity.this, details); 
    listView.setAdapter(detailAdapter); 


    // Set a click listener to edit quantity when the list item is clicked on 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
      ComponentDetail cd = details.get(position); 
      quantity = cd.getQuantity(); 
      Intent i = new Intent(I4ComponentActivity.this, Edit.class); 
      //Create the bundle 
      Bundle bundle = new Bundle(); 
      //Add your data to bundle 
      bundle.putString("Item_quantity", quantity); 
      //Add the bundle to the intent 
      i.putExtras(bundle); 
      //Fire that second activity 
      startActivity(i); 
     } 
    }); 

} 
} 

下面是活动2码

public class Edit extends AppCompatActivity { 
private int quantity; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_edit); 


    //Get the bundle 
    Bundle bundle = getIntent().getExtras(); 
    //Extract the data… 
    String Item_quantity = bundle.getString("Item_quantity"); 

    TextView tv = (TextView) findViewById(R.id.quantity_edit); 
    tv.setText(Item_quantity); 
    quantity = Integer.parseInt(Item_quantity); 
} 

public void decrease(View v){ 

    quantity --; 
    displayQuantity(); 
} 
public void increase(View v){ 

    quantity ++; 
    displayQuantity(); 
} 
public void submit(View v){ 
    Intent i = new Intent(Edit.this, I4ComponentActivity.class); 
    Bundle bundle = new Bundle(); 
    bundle.putString("quantity_back", ""+quantity); 
    i.putExtras(bundle); 
    startActivity(i); 
} 

public void displayQuantity(){ 
    TextView tv = (TextView) findViewById(R.id.quantity_edit); 
    tv.setText(""+quantity); 
} 
} 
+1

看看'startActivityForResult' –

+0

谢谢你,你回答如此之快,startActivityForResult是我需要使用的。 – Arvin

回答

0

StartActivity2与startActivityForResult(Intent,1)

后你得到你想要的东西在Aty2 setResult(1,Intent)

和重写onActivityResult你会得到回来的意图。

将项目添加到您的数组列表后。使用adapter.notifyDataSetChanged();刷新你的列表视图

如果你不清楚。必须有大量的信息在谷歌

+0

谢谢startActivityForResult正是我需要使用的,我需要提高我的搜索技巧:) – Arvin

+0

给我一个投票... plz..i真的需要它... –