にネットワーク json データのパーシャルをロードしたいListView
。
json
オンラインで確認しましたが、ネットワークデータの一部を にロードする方法がわかりませんListView
。
今、私は自分のコードを投稿して、他の人に助けを求めています。どうもありがとう!
public class MainActivity extends ListActivity{
private ListView listView;
File cache = null;
private String path ="Here is the json file on the server address";
Handler handler = new Handler(){
public void handleMessage(Message msg) {
listView.setAdapter(new VideoFileAdapter(MainActivity.this, (List<VideoFile>)msg.obj,
R.layout.listview_item, cache));
}
VideoFileService.java
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
listView = getListView();
cache = new File(Environment.getExternalStorageDirectory(), "cache");
if(!cache.exists()) cache.mkdirs();
new Thread(new Runnable() {
public void run() {
try {
List<VideoFile> data = VideoFileService.getJSONVideoFile(path);
handler.sendMessage(handler.obtainMessage(22, data));
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
protected void onDestroy() {
super.onDestroy();
}
}
VideoFileAdapter.java
public class VideoFileAdapter extends BaseAdapter {
public List<VideoFile> data;
public int listviewItem;
private File cache;
LayoutInflater layoutInflater;
public VideoFileAdapter(Context context, List<VideoFile> data, int listviewItem, File cache) {
this.data = data;
this.listviewItem = listviewItem;
this.cache = cache;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
TextView textView = null;
if(convertView == null){
convertView = layoutInflater.inflate(listviewItem, null);
imageView = (ImageView) convertView.findViewById(R.id.image);
textView = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(new DataWrapper(imageView, textView));
}else{
DataWrapper dataWrapper = (DataWrapper) convertView.getTag();
imageView = dataWrapper.imageView;
textView = dataWrapper.textView;
}
VideoFile videoFile = data.get(position);
textView.setText(videoFile.name);
asyncImageLoad(imageView, videoFile.image);
return convertView;
}
private void asyncImageLoad(ImageView imageView, String path) {
AsyncImageTask asyncImageTask = new AsyncImageTask(imageView);
asyncImageTask.execute(path);
}
private final class AsyncImageTask extends AsyncTask<String, Integer, Uri>{
private ImageView imageView;
public AsyncImageTask(ImageView imageView) {
this.imageView = imageView;
}
protected Uri doInBackground(String... params) {
try {
return VideoFileService.getImage(params[0], cache);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Uri result) {
if(result!=null && imageView!= null)
imageView.setImageURI(result);
}
}
private final class DataWrapper{
public ImageView imageView;
public TextView textView;
public DataWrapper(ImageView imageView, TextView textView) {
this.imageView = imageView;
this.textView = textView;
}
}
public void addItem(VideoFile item) {
data.add(item);
}
}