2017-02-10 10 views
0

私はアンドロイドに新しいです、私はたくさんのグーグルで探せますが、適切な答えを見つけることができません。誰かが私を助けることができますか? 私は2つのアクティビティを持っています。アクティビティ1にはリストビューがあり、アクティビティ2には編集ビューがあり、リストビューでアイテムをクリックするとアクティビティ2に移動します。データを操作した後にサブミットボタンをクリックします。リストビューに戻り、リストビューに更新された結果が表示されます。あなたはAty2 setResult(1,Intent)他のアクティビティからリストビューを更新するには

欲しいものを取得し、onActivityResultにあなたを書き換えた後 はここ1つのコードここ

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); 
     } 
    }); 

} 
} 

私avtivity startActivityForResult(Intent,1)

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アクティビティ2コードされています後ろの意図を得るでしょう。

あなたのarraylistに商品を追加した後に。 adapter.notifyDataSetChanged();を使用してリストビューをリフレッシュしてください。

クリアしないと、 Googleに多くの情報が必要です

+0

ありがとう、startActivityForResultは私が使用する必要がある正確には、私は私の検索スキルを向上させる必要があります:) – Arvin

+0

私に投票を与える... plz ..私は本当にそれが必要... –

関連する問題