2017-02-10 6 views
0

Я новичок в android, я много искал, но не могу найти правильный ответ. Кто-то может мне помочь? У меня есть два действия, активность 1 содержит список, активность 2 - это вид редактирования, что я хочу сделать, после нажатия на элемент в списке, он перейдет к действию 2, после того, как манипулирует данными, нажмите кнопку отправки (метод отправки) , он вернется к списку, и в списке будет показан обновленный результат. Вот мой avtivity 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();, чтобы обновить список.

Если вы не поняли. В Google должно быть много информации

+0

Спасибо, startActivityForResult - это именно то, что мне нужно использовать, мне нужно улучшить свое поисковое умение :) – Arvin

+0

дайте мне голосование ... plz..i действительно это нужно ... –

Смежные вопросы