我创建了一个类活动,之后继承了两个活动。它看起来如下:
public abstract class VlcMediaPlayerList extends RoboActivity {
public final static int AUDIO = 1;
public final static int VIDEO = 2;
@InjectView(R.id.audio_list_view)
ListView listView;
public static ArrayList<ImageWithTwoText> list = new ArrayList<ImageWithTwoText>();
public ImageWithTwoTextArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vlc_media_player_list);
adapter = new ImageWithTwoTextArrayAdapter(this, list);
listView.setAdapter(adapter);
addListenerOnItemList();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (intent != null) {
Bundle extras = intent.getExtras();
ImageWithTwoText file = null;
try {
file = (ImageWithTwoText) extras.getSerializable(FileChooserAcitivity.FILE_TYPE);
} catch (NullPointerException ex) {
}
Log.i("FILECHOOSER", String.valueOf(list.size()));
if (file != null) {
list.add(file);
}
adapter.notifyDataSetChanged();
}
}
/**
* Add File method called when addButton is clicked. Start activity
* FileChooserActivity for result as file
*
* @param ImageButton
* view
*/
public void addFile(View view) {
Intent intent = new Intent(this, FileChooserAcitivity.class);
intent.putExtra(FileChooserAcitivity.FILE_TYPE, getMediaType());
startActivityForResult(intent, getMediaType());
}
/**
* Clear the list when clearButton is clicked
*
* @param View
* view
*/
public void clearList(View view) {
list.clear();
adapter.notifyDataSetChanged();
Toast.makeText(this, getResources().getString(R.string.clear_list), Toast.LENGTH_SHORT).show();
}
/**
* Add listener on list
*/
public void addListenerOnItemList() {
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(view.getContext(),
PlayerActivity.class);
intent.putExtra(PlayerActivity.PLAYER_ELEMENT,
list.get(position).getTitle());
startActivity(intent);
}
});
}
protected abstract int getMediaType();
}
扩展抽象类的一个类如下:
public class VlcMediaPlayerAudio extends VlcMediaPlayerList {
@Override
protected int getMediaType() {
return VlcMediaPlayerList.AUDIO;
}
}
调用onActivityResult()后会出现问题,ImageAithTwoText对象代表列表的元素。调用adapter.notifyDataSetChanged()后,此元素不会添加到列表中。 注意!退出活动(返回键)并重新进入同一活动之前添加的元素已经在列表中
这是ImageWithTwoTextArrayAdapter:
的代码public class ImageWithTwoTextArrayAdapter extends ArrayAdapter<String> {
public final static String TITLE_KEY = "title";
public final static String SUBTITLE_KEY = "subtitle";
public final static String IMAGE_RESOURCE_KEY = "imageResources";
private final List<ImageWithTwoText> imageWithTwoTextList;
private final Context context;
/**
* Class constructor
*
* @param context
*/
public ImageWithTwoTextArrayAdapter(Context context, List<ImageWithTwoText> imageWithTwoTextList) {
super(context, R.layout.row_view, getArrayListOfTitles(imageWithTwoTextList));
this.context = context;
this.imageWithTwoTextList = imageWithTwoTextList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row_view, parent, false);
TextView titleView = (TextView) rowView.findViewById(R.id.title);
TextView subTitleView = (TextView) rowView.findViewById(R.id.subtitle);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
ImageWithTwoText currentElement = imageWithTwoTextList.get(position);
titleView.setText(currentElement.getTitle());
subTitleView.setText(currentElement.getSubTitle());
imageView.setImageResource(currentElement.getImageResource());
return rowView;
}
private static List<String> getArrayListOfTitles(List<ImageWithTwoText> imageWithTwoTextList) {
List<String> listOfTitles = new ArrayList<String>();
for (ImageWithTwoText imageWithTwoText : imageWithTwoTextList) {
listOfTitles.add(imageWithTwoText.getTitle());
}
return listOfTitles;
}
}
答案 0 :(得分:1)
我认为问题出在你的适配器的构造函数中,你正在调用这样的基础构造函数:
super(context, R.layout.row_view, getArrayListOfTitles(imageWithTwoTextList));
这是传递
的结果getArrayListOfTitles()
到适配器存储的基础列表,它是您提供的列表的副本。这意味着您对活动中的列表所做的更改不会发生在适配器所持列表的副本中。
啊,你的评论大约一分钟就击败了我的编辑!很高兴它解决了!