在返回上一个活动时,列表视图中的项目重载

时间:2014-07-01 10:11:45

标签: android

我有2个活动。活动(1)包含项目列表,我将在活动(1)上使用onitemclick调用活动(2)。所以这里的问题是当我回到活动(2)到活动(1)时,活动(1)中存在的项目再次被添加(仅加倍)。我怎么能阻止这个?任何人都可以告诉我。我是android的初学者。应该感谢您的帮助。

活性(1)

public class ClassesActivity extends Activity {
protected void onCreate(Bundle SavedInstanceState){ 
    super.onCreate(SavedInstanceState);
    setContentView(R.layout.displayclass);
    Log.i("i'm in displayclass","activity");

    listview = (ListView) findViewById(R.id.classlistView1); 
    cv = (ListView)findViewById(R.id.classlistView1);
    listview.setOnItemClickListener(clicklistener); 
    @Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
protected void onPause() {
    super.onPause();
    }

@Override
protected void onRestart() {
    super.onRestart();

}
@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onStart() {

    super.onStart();
    ArrayAdapter<String> arrayAdapter3 = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, your_array_list3);
    cv.setAdapter(arrayAdapter3);
    try {
        Classes_list.main();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onStop() {
    super.onStop();
}

public static class ItemClickListener implements OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position,long id) {

c3=Classes_list.c2[position];
Log.i("c3", c3.getName());
    Intent myIntent2 = new   
 Intent(view.getContext(),Methods_on_class_list_activity.class);
    view.getContext().startActivity(myIntent2);
}

这是我的Methods_on_class_list_activity(2)

public class Methods_on_class_list_activity extends Activity {
private ListView listview, mv;
@SuppressWarnings({ "unchecked", "rawtypes" })
public static ArrayList<String> your_array_list9 = new ArrayList();
public ItemClickListener clicklistener = new ItemClickListener();
public static Method c5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.method_display);
    listview = (ListView) findViewById(R.id.fromclassto_methodlist);
    mv = (ListView) findViewById(R.id.fromclassto_methodlist);
    listview.setOnItemClickListener(clicklistener);

}

@Override
protected void onDestroy() {
    super.onDestroy();
}

@Override
protected void onPause() {

    super.onPause();
}

@Override
protected void onRestart() {

    super.onRestart();
}

@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onStart() {
    super.onStart();
    ArrayAdapter<String> arrayAdapter3 = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, your_array_list9);
    mv.setAdapter(arrayAdapter3);
    try {
        Methods_List.methodexecution();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
protected void onStop() {

    super.onStop();
}
public static class ItemClickListener implements OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position,long id) {
        c5=Parameter_List.metd[position];
        Log.i("c3", c5.getName());

    Intent myIntent2 = new Intent(view.getContext(),Parameterlist_activity.class);
            view.getContext().startActivity(myIntent2);
}

1 个答案:

答案 0 :(得分:0)

Activity Life Cycle call as follows

活动-1

点击列表项

1)onPause()
5)onStopt()

<强>活动-2

2)OnCreate()
3)onStrat()
4)OnResume()

返回活动1

1. onRestart()
2. onStart()
3. onResume()

请从onStart()中移除代码,将代码添加到ListView

这就是它加两次的原因。

希望这会对你有所帮助。