gpt4 book ai didi

Android:当我启动一个新 Activity 并按返回返回时,Listview 会 self 复制

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:39 27 4
gpt4 key购买 nike

我有两个 ListView ,它们是 View 分页选项卡中的 fragment 。当您单击 ListView 中的项目时,它会启动一个新 Activity 。但是当我按下后退按钮返回选项卡式 ListView 时, ListView 已经翻倍,如果我打开 Activity 并再次返回,它会再次翻倍,并且会继续这样做。我还有另一个带有五个选项卡的选项卡式 ListView ,当我离开其中一个 View 时有两个选项卡。当我回到它们时,该 View 中的项目加倍,这对于所有其他选项卡都是相同的。两个 ListView 的代码是相同的。我试过 list.clear(),它只清除所有列表项,并在 View 持有者中使用“else”语句(其他堆栈溢出答案推荐)。但每次 ListView 仍然重复。另外,除非您还没有注意到我对 android 还很陌生。

这里是双标签 Activity 中的 ListView fragment (Due_Today_Fragment) 的代码。

      import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class Due_Today_Fragment extends Fragment {
private List<homeworkdue> myhomeworkdue;

static class ViewHolder {
public ImageView imageView;
public TextView HomeworkDueText;
public TextView DescriptionText;
public TextView TeacherText;
}


public static final String KEY_HOMEWORK="homework";
public static final String KEY_DESC="desc";

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.activity_main, container, false); }


@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
myhomeworkdue = new ArrayList<homeworkdue>();
populatehomeworkdueList();
populateListView();
registerClickCallback();
}

private void registerClickCallback() {
ListView list = (ListView)getView().findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {

homeworkdue clickedhomeworkdue = myhomeworkdue.get(position);

Intent intent = new Intent(getActivity(), homeworkdueDetailsActivity.class);
intent.putExtra(KEY_HOMEWORK, clickedhomeworkdue.getHomeworkdue());
intent.putExtra(KEY_DESC, clickedhomeworkdue.getDesciption());

startActivity(intent);
}
});
}


private void populatehomeworkdueList() {
myhomeworkdue.add(new homeworkdue("History Homework", "Description 1", R.drawable.global_studies, "Anderson"));
myhomeworkdue.add(new homeworkdue("Math Homework", "Description 2", R.drawable.mathematics, "Klein"));
myhomeworkdue.add(new homeworkdue("English Homework", "Description 3", R.drawable.english, "Reed"));
myhomeworkdue.add(new homeworkdue("Spanish Homework", "Description 4", R.drawable.spanish, "Joya"));
myhomeworkdue.add(new homeworkdue("Science Homework", "Description 5", R.drawable.science, "Poole"));
}

private void populateListView() {
ArrayAdapter<homeworkdue> adapter = new MyListAdapter();
ListView list = (ListView)getView().findViewById(R.id.listView1);
list.setAdapter(adapter);


}


public class MyListAdapter extends ArrayAdapter<homeworkdue> {
public MyListAdapter() {
super(getActivity(), R.layout.item_view, myhomeworkdue);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if(convertView==null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.item_view, parent, false);
holder = new ViewHolder();

holder.imageView = (ImageView)convertView.findViewById(R.id.item_iconclass);

holder.HomeworkDueText = (TextView) convertView.findViewById(R.id.item_texthomeworkdue);

holder.DescriptionText = (TextView) convertView.findViewById(R.id.item_textdescription);

holder.TeacherText = (TextView) convertView.findViewById(R.id.item_textteacher);

convertView.setTag(holder);


}

else {
holder = (ViewHolder) convertView.getTag();
}

homeworkdue currenthomeworkdue = myhomeworkdue.get(position);

holder.imageView.setImageResource(currenthomeworkdue.getIconID());


holder.HomeworkDueText.setText(currenthomeworkdue.getHomeworkdue());


holder.DescriptionText.setText(currenthomeworkdue.getDesciption());


holder.TeacherText.setText(currenthomeworkdue.getTeacher());

return convertView;

}

}


}

这是单击 ListView 项时启动的 Activity 的代码

    import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import static com.bernard.beaconportal.Due_Today_Fragment.KEY_HOMEWORK;
import static com.bernard.beaconportal.Due_Today_Fragment.KEY_DESC;


public class homeworkdueDetailsActivity extends Activity {

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

String homework = "";
String desc = "";

Intent intent = getIntent();
if (null != intent) {
homework = intent.getStringExtra(KEY_HOMEWORK);
desc = intent.getStringExtra(KEY_DESC);
}

TextView headlineTxt = (TextView) findViewById(R.id.texthomeworkdue);
headlineTxt.setText(homework);

TextView pubdateTxt = (TextView) findViewById(R.id.textdescription);
pubdateTxt.setText(desc);

}


}

这是标签 fragment 的代码

import java.lang.reflect.Field;
import com.actionbarsherlock.app.SherlockFragment;
import com.astuetz.PagerSlidingTabStrip;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragments2 extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.viewpager_main, container, false);
// Locate the ViewPager in viewpager_main.xml
ViewPager pager = (ViewPager) view.findViewById(R.id.viewPager);
// Set the ViewPagerAdapter into ViewPager
pager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view.findViewById(R.id.pagerTabStrip);
tabs.setViewPager(pager);

return view;
}

@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}

这里是为到期作业定义字符串的地方

public class homeworkdue {
private String homework;
private String desc;
private int IconID;
private String teacher;



public homeworkdue(String homework, String desc, int IconID, String teacher) {



super();
this.homework = homework;
this.desc = desc;
this.IconID = IconID;
this.teacher = teacher;
}

public String getHomeworkdue() {
return homework;
}
public String getDesciption() {
return desc;
}
public int getIconID() {
return IconID;
}
public String getTeacher() {
return teacher;
}


}

这是包含在五个选项卡中的 fragment 的代码

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class Thursday extends Fragment {



public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.schedule_list_view, container, false); }

private List<schedule> myschedule = new ArrayList<schedule>();



static class ViewHolder {

public TextView HomeworkDueText;
}

public void onStart() {
super.onStart();

populatescheduleList();
populateListView();

}


private void populatescheduleList() {
myschedule.add(new schedule("E Band"));
myschedule.add(new schedule("G Band"));
myschedule.add(new schedule("F Band"));
myschedule.add(new schedule("H Band"));
myschedule.add(new schedule("A Band"));
myschedule.add(new schedule("C Band"));
myschedule.add(new schedule("D Band"));
}

private void populateListView() {
ArrayAdapter<schedule> adapter = new MyListAdapter();
ListView list = (ListView)getView().findViewById(R.id.listView2);
list.setAdapter(adapter);

}


public class MyListAdapter extends ArrayAdapter<schedule> {
public MyListAdapter() {
super(getActivity(), R.layout.item_view, myschedule);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if(convertView==null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.schedule_list_item, parent, false);
holder = new ViewHolder();

holder.HomeworkDueText = (TextView) convertView.findViewById(R.id.bandText);

convertView.setTag(holder);
}

else {
holder = (ViewHolder) convertView.getTag();
}

schedule currenthomeworkdue = myschedule.get(position);

holder.HomeworkDueText.setText(currenthomeworkdue.Band());

return convertView;


}

}

}

这里是为时间表定义字符串的地方

public class schedule {
private String band;




public schedule(String band) {



super();
this.band = band;

}

public String Band() {
return band;


}

}

如果您需要更多信息,请提前询问并感谢您的帮助

我已经尝试修复这个错误两天了,但我已经束手无策了。

最佳答案

我们在创建 fragment 时声明一个新列表的背后原因,所以当您返回到您的 fragment 时,它不会声明一个新列表,而是使用旧列​​表。因此,为了解决这个问题,您需要将以下代码分成两部分:

private List<homeworkdue> myhomeworkdue = new ArrayList<homeworkdue>();

部分进入 onResume 在你的 fragment 中你还需要删除 onStartonStartonResume 中的所有内容.但首先保持这样的声明:

private List<homeworkdue> myhomeworkdue;

并将其余部分移至onResume

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
myhomeworkdue = new ArrayList<homeworkdue>();
populatehomeworkdueList();
populateListView();
registerClickCallback();
}

您的第二个 ListView 中存在同样的问题,因为它们是相同的。

希望这对你有用。如果有效,请给我反馈。

关于Android:当我启动一个新 Activity 并按返回返回时,Listview 会 self 复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21446361/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com