最初,我有自己的Adapter extended the BaseAdapter
,并且工作正常。然后,我尝试通过以下代码采用通用软件EndlessAdapter:
public class MyEndlessAdapter extends EndlessAdapter {
private RotateAnimation rotate = null;
private View pendingView = null;
private List<Map<String, Object>> mData = null;
private String link, type;
String[] videoInfo= new String[90];
private Bitmap[] thumbnail = new Bitmap[10];
Context context;
public MyEndlessAdapter(Context ctxt, List<Map<String, Object>> data) {
super(new MyEndlessAdapter(ctxt, data));
new MyAdapter(ctxt, data);
context = ctxt;
rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
public List<Map<String, Object>> get() {
MyAdapter a2 = (MyAdapter) getWrappedAdapter();
return a2.get();
}
@Override
protected View getPendingView(ViewGroup parent) {
View row = LayoutInflater.from(parent.getContext()).inflate(
R.layout.row, null);
pendingView = row.findViewById(android.R.id.text1);
pendingView.setVisibility(View.GONE);
pendingView = row.findViewById(R.id.throbber);
pendingView.setVisibility(View.VISIBLE);
startProgressAnimation();
return (row);
}
public void getInfromation(String type, String link) {
this.link = link;
this.type = type;
}
@Override
protected boolean cacheInBackground() {
SearchYoutube mySearchYoutube = new SearchYoutube();
String jsonString = mySearchYoutube.YoutubeResult(link);
Log.i("in AsycTask", "link= " + link);
Log.i("in AsycTask", "jsonString= " + jsonString);
videoInfo = mySearchYoutube.YoutubeJSONToString(jsonString);
int arrayLength = mySearchYoutube.GetCounter();
Log.i("in AsycTask", "arrayLength= " + arrayLength);
try {
for (int j = 4, i = 0; j < arrayLength; j = j + 9, i++) {
Log.i("Check bitmap", "before, j = " + j + " ,i = " + i);
Log.i("Check bitmap", "after" + videoInfo[j]);
try {
thumbnail[i] = BitmapFactory
.decodeStream((InputStream) new URL(videoInfo[j])
.getContent());
} catch (Exception e) {
thumbnail[i] = BitmapFactory.decodeResource(
context.getResources(), R.drawable.video_default);
}
}
mData = getData(mData);
return (getWrappedAdapter().getCount() < 200);
} catch (Exception anyError) {
return false;
}
}
@Override
protected void appendCachedData() {
if (getWrappedAdapter().getCount() < 200) {
@SuppressWarnings("unchecked")
MyAdapter a = (MyAdapter) getWrappedAdapter();
for (int i = 0; i < mData.size(); i++) {
a.add(mData.get(i));
}
}
}
public void startProgressAnimation() {
if (pendingView != null) {
pendingView.startAnimation(rotate);
}
}
public class MyAdapter extends BaseAdapter {
List<Map<String, Object>> mData;
public final class ViewHolder {
public ImageView thumbnail_view;
public TextView title_view;
public TextView date_view;
public TextView duration_view;
// public Button save_btn;
}
private LayoutInflater mInflater;
ViewHolder holder = null;
public MyAdapter(Context context, List<Map<String, Object>> data) {
this.mInflater = LayoutInflater.from(context);
mData = data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/**
* Original holder
*
*
* */
// ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.vlist, null);
holder.thumbnail_view = (ImageView) convertView
.findViewById(R.id.thumbnail_view);
holder.title_view = (TextView) convertView
.findViewById(R.id.title_view);
holder.date_view = (TextView) convertView
.findViewById(R.id.date_view);
holder.duration_view = (TextView) convertView
.findViewById(R.id.duration_view);
// holder.save_btn =
// (Button)convertView.findViewById(R.id.save_btn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Log.i("position< mData.size()", "count");
holder.thumbnail_view.setImageBitmap((Bitmap) mData.get(position)
.get("thumbnail"));
holder.title_view
.setText((String) mData.get(position).get("title"));
holder.date_view.setText((String) mData.get(position).get(
"uploaded"));
holder.duration_view.setText((String) mData.get(position).get(
"duration"));
// holder.save_btn.setOnClickListener(new
// myButtonListener(position));
return convertView;
}
public void add(Map<String, Object> map) {
mData.add(map);
notifyDataSetChanged();
}
public List<Map<String, Object>> get() {
return mData;
}
class myButtonListener implements OnClickListener {
private int position;
myButtonListener(int pos) {
position = pos;
}
@Override
public void onClick(View v) {
int vid = v.getId();
Log.i("onClick, button on list", "position is : " + position
+ " And vid : " + vid);
}
}
}
private List<Map<String, Object>> getData(List<Map<String, Object>> mList) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if (mList != null||mList == null) {
list = mList;
}
Log.v("In videoInfo List", "length");
Map<String, Object> map;
int arrayLength = videoInfo.length;
for (int j = 0, i = 0; j < arrayLength; j++, i++) {
map = new HashMap<String, Object>();
Date d2 = new Date();
videoInfo[j] = videoInfo[j].replaceAll("Z", "+0800");
SimpleDateFormat dateformat1 = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date youtubeDate = new Date();
try {
youtubeDate = dateformat1.parse(videoInfo[j]);
Log.v("In videoInfo List", "youtubeDate: " + youtubeDate);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String time = "";
String ago = " ago";
int year, month, day, year2, month2, day2 = 0;
int hour, minute, seccond, hour2, minute2, seccond2 = 0;
Calendar calendar = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar.setTime(youtubeDate);
calendar2.setTime(d2);
year = calendar.get(Calendar.YEAR);
;
year2 = calendar2.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
month2 = calendar2.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
day2 = calendar2.get(Calendar.DAY_OF_MONTH);
hour = calendar.get(Calendar.HOUR_OF_DAY);
hour2 = calendar2.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
minute2 = calendar2.get(Calendar.MINUTE);
if (year2 - year > 0) {
time = (year2 - year) + " years ago | ";
map.put("uploaded", time);
} else if (month - month > 0) {
time = (month2 - month) + " months ago | ";
map.put("uploaded", time);
} else if (day2 - day > 0) {
time = (day2 - day) + " days ago | ";
map.put("uploaded", time);
} else if (hour2 - hour > 0) {
time = (hour2 - hour) + " hours ago | ";
map.put("uploaded", time);
} else if (minute2 - minute > 0) {
time = (minute2 - minute) + " minutes ago | ";
map.put("uploaded", time);
}
else {
map.put("uploaded", "a few seconds ago | ");
}
// map.put("uploaded", videoInfo[j]);
j++;
map.put("category", videoInfo[j]);
j++;
map.put("title", videoInfo[j]);
j++;
map.put("description", videoInfo[j]);
j++;
map.put("thumbnail", thumbnail[i]);
map.put("thumbnail_link", videoInfo[j]);
j++;
map.put("share_link", videoInfo[j]);
Log.v("In videoInfo List", "share_link: " + videoInfo[j]);
j++;
map.put("youtube_link", videoInfo[j]);
j++;
map.put("rtsp", videoInfo[j]);
Log.v("In videoInfo List", "rtsp: " + videoInfo[j]);
j++;
float a = Float.parseFloat(videoInfo[j]);
int b = (int) a;
int sec = b % 60;
int min = b / 60;
String duration;
if (sec < 10) {
duration = min + " : 0" + sec;
} else {
duration = min + " : " + sec;
}
// videoInfo[j] = duration;
map.put("duration", duration);
Log.v("In videoInfo List", "duration: " + duration);
list.add(map);
}
Log.v("In videoInfo List", "list: " + list.size());
return list;
}
}
现在,当我在主活动中单击特定项目时,我想从mData中检索数据。
最佳答案
您将像以前一样进行操作。 EndlessAdapter
不会更改在position
的onListItemClick()
之类的方法中提供给您的ListFragment
值。