有人可以给我一个关于如何在片段类中使用listview的示例代码。 我有一个JSON,它有一些新闻的数据。我想在列表视图中显示这些新闻。当您点击列表视图中的项目时,您将被引导至详细的新闻栏目。 我怎样才能做到这一点?我已经有一个适配器类,我用于我的程序中的其他活动
LazyAdapter类
package com.fortuna.cinemalk.adapter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import com.fg.sinhala.Sihala;
import com.fortuna.cinemalk.lazyloading.ImageLoader;
import com.fortuna.cinemalk.R;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.model.Film;
import com.fortuna.cinemalk.model.GridElement;
import com.fortuna.cinemalk.model.News;
import com.fortuna.cinemalk.model.Theater;
import com.fortuna.cinemalk.util.Element;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class LazyAdapter extends BaseAdapter {
private ArrayList<BaseElement> item;
private Activity activity;
private LayoutInflater layoutInflater;
private int type;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
Context context;
public LazyAdapter(ArrayList<BaseElement> item, Activity activity, int type) {
this.item = item;
this.activity = activity;
this.type = type;
this.layoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
@Override
public int getCount() {
return item.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int id) {
return id;
}
@Override
public View getView(int position, View viewCon, ViewGroup viewGroup) {
View view = null;
ListView view1 = null;
if (Element.LEFT_MENU.getType() == type) {
view = layoutInflater.inflate(R.layout.leftmenu_list_layout, null);
GridElement gridElement = (GridElement) item.get(position);
ImageView image = (ImageView) view.findViewById(R.id.leftmenu_icon);
TextView title = (TextView) view.findViewById(R.id.leftmenu_title);
title.setText(gridElement.getTitle());
image.setImageResource(gridElement.getIcon());
}
if (Element.MOVIE_LIST.getType() == type) {
view = layoutInflater.inflate(R.layout.movie_list_layout, null);
Film film = (Film) item.get(position);
ImageView image = (ImageView) view.findViewById(R.id.image);
TextView title = (TextView) view.findViewById(R.id.title);
title.setText(film.getTitle());
// image.setImageResource(film.getImage());
}
/* if (Element.NEWS_LIST.getType() == type) {
view1 = (ListView) layoutInflater.inflate(R.layout.news_list, null);
News news = (News) item.get(position);
ImageView image = (ImageView) view.findViewById(R.id.news_image);
TextView title = (TextView) view.findViewById(R.id.news_title);
title.setText(news.getTitle());
// image.setImageResource(film.getImage());
} */
if (Element.THEATER_LIST.getType() == type) {
view = layoutInflater.inflate(R.layout.theater_list_layout, null);
Theater theater = (Theater) item.get(position);
TextView name = (TextView) view.findViewById(R.id.theater_name);
ImageView image = (ImageView) view.findViewById(R.id.image_theater);
/* name.setText(theater.getName());
imageLoader
.DisplayImage(theater.getTheaterImage(), activity, image);
}
return view;
}
} */
//use this to all elements that have sinhala texts
Sihala sinhla = new Sihala(activity);
Typeface typeface = Typeface.createFromAsset(activity.getAssets(), sinhla.font);
name.setTypeface(typeface);
name.setText(sinhla.getSinhalaString(theater.getName()));
imageLoader
.DisplayImage(theater.getTheaterImage(), activity, image);
}
return view;
}
}
我尝试过的NewsFragment类(不确定这是否正确)
package com.fortuna.cinemalk;
import java.util.ArrayList;
import com.fortuna.cinemalk.TheaterFragment.BackGround;
import com.fortuna.cinemalk.adapter.LazyAdapter;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.service.CommonVariable;
import com.fortuna.cinemalk.service.JSONServices;
import com.fortuna.cinemalk.util.Element;
import android.app.Activity;
import android.os.AsyncTask;
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.GridView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class NewsFramgment extends Fragment{
private ListView listview;
private Activity activity;
private CommonVariable commonVariable;
private LazyAdapter adapter;
private ArrayList<BaseElement> newscolumn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_fragment,
container, false);
activity = this.getActivity();
commonVariable = (CommonVariable) activity.getApplication();
listview = (ListView) view.findViewById(R.id.listView1);
/*listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
android.support.v4.app.Fragment detail = new NewsDetailFragment();
android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.content_frame, detail).addToBackStack("back").commit();
}
}); */
new BackGround().execute();
return view;
}
public class BackGround extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
newscolumn = JSONServices.getNewsDescription();
return null;
}
@Override
/* check again */
protected void onPostExecute(Void result) {
commonVariable.setTheater(newscolumn);
adapter = new LazyAdapter(newscolumn, activity,Element.NEWS_LIST.getType());
listview.setAdapter(adapter);
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}
}
我的JSON服务类
package com.fortuna.cinemalk.service;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.graphics.Paint.Join;
import android.widget.TextView;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.model.Film;
import com.fortuna.cinemalk.model.FilmBanner;
import com.fortuna.cinemalk.model.FilmCategory;
import com.fortuna.cinemalk.model.Home;
import com.fortuna.cinemalk.model.News;
import com.fortuna.cinemalk.model.Theater;
import com.fortuna.cinemalk.util.JSONTag;
import com.fortuna.cinemalk.util.ServiceURL;
public class JSONServices {
public static ArrayList<BaseElement> getCategory() {
ArrayList<BaseElement> catogory = new ArrayList<BaseElement>();
try {
JSONObject jsonObject = new JSONObject(
CommonService.readJsonUrl(ServiceURL.CATEGORY_TOPCAT
.getUrl()));
JSONArray array = jsonObject.getJSONArray(JSONTag.CATEGORY.getTag());
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObj = array.getJSONObject(i);
FilmCategory filmCategory = new FilmCategory();
filmCategory.setCatId(jsonObj.getString(JSONTag.ID.getTag()));
filmCategory.setCategory(jsonObj.getString(JSONTag.NAME
.getTag()));
catogory.add(filmCategory);
// news.put(i, newsCategory);
}
} catch (JSONException e) {
e.printStackTrace();
return null;
}
return catogory;
}
public static ArrayList<BaseElement> getCategoryById(String id) {
ArrayList<BaseElement> films = new ArrayList<BaseElement>();
try {
JSONObject jsonObject = new JSONObject(CommonService.readJsonFromUrl(ServiceURL.MOVE_BY_CATEGORY
.getUrl() + id));
JSONArray array = jsonObject.getJSONArray(JSONTag.MOVIE.getTag());
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObj = array.getJSONObject(i);
Film film = new Film();
film.setActorsActress(jsonObj.getString(JSONTag.ACTOER_ACTRESS
.getTag()));
film.setComposer(jsonObj.getString(JSONTag.COMPOSER.getTag()));
film.setDescrition(jsonObj.getString(JSONTag.DESCRIPTION
.getTag()));
film.setDirecter(jsonObj.getString(JSONTag.DIRECTOR.getTag()));
film.setGenre(jsonObj.getString(JSONTag.GENERE.getTag()));
film.setImage(jsonObj.getString(JSONTag.IMAGE_2.getTag()));
film.setIMDbRating(jsonObj.getString(JSONTag.IMDB_RATING
.getTag()));
film.setLength(jsonObj.getString(JSONTag.LENGTH.getTag()));
film.setNewsId(jsonObj.getString(JSONTag.ID.getTag()));
film.setPlot(jsonObj.getString(JSONTag.PLOT.getTag()));
film.setProducer(jsonObj.getString(JSONTag.PRODUCER.getTag()));
films.add(film);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return films;
}
public static ArrayList<BaseElement> getNewsDescription() {
ArrayList<BaseElement> newsitem = new ArrayList<BaseElement>();
//LinkedHashMap<Integer, BaseElement> news = new LinkedHashMap<Integer, BaseElement>();
try {
JSONObject jsonObject = new JSONObject(
CommonService
.readJsonFromUrl(ServiceURL.NEWS_CONTENT
.getUrl() ));
JSONArray array = jsonObject.getJSONArray("news");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObj = array.getJSONObject(i);
News newsDescription = new News();
newsDescription.setNewsId(jsonObj.getString(JSONTag.ID.getTag()));
newsDescription.setTitle(jsonObj.getString(JSONTag.TITLE.getTag()));
//newsDescription.setShortDescription(jsonObj.getString(JSONTag.SHORTDESCRIPTION.getTag()));
//newsDescription.setDescrition(jsonObj.getString(JSONTag.DESCRIPTION.getTag()));
newsDescription.setThumbImage(jsonObj.getString(JSONTag.THUMBNAIL.getTag()));
//newsDescription.setImage(jsonObj.getString(JSONTag.IMAGE.getTag()));
//newsDescription.setTrailerImage(jsonObj.getString(JSONTag.TRAILER_IMAGE.getTag()));
//newsDescription.setVideo(jsonObj.getString(JSONTag._VIDEO.getTag()));
//news.put(i, newsDescription);
newsitem.add(newsDescription);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return newsitem;
}
public static LinkedHashMap<Integer, BaseElement> getHomeContent() {
LinkedHashMap<Integer, BaseElement> news = new LinkedHashMap<Integer, BaseElement>();
try {
JSONObject jsonObject = new JSONObject(
CommonService.readJsonFromUrl(ServiceURL.HOME_CONTENT
.getUrl()));
JSONArray array = jsonObject.getJSONArray("home");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObj = array.getJSONObject(i);
Home home = new Home();
JSONArray homeArray = jsonObj.getJSONArray("photos");
LinkedHashMap<Integer, BaseElement> banner = new LinkedHashMap<Integer, BaseElement>();
for (int j = 0; j < homeArray.length(); j++) {
JSONObject jsonObject2 = homeArray.getJSONObject(j);
FilmBanner filmBanner = new FilmBanner();
filmBanner.setId(jsonObject2.getString(JSONTag.ID.getTag()));
filmBanner.setTitle(jsonObject2.getString(JSONTag.TITLE.getTag()));
filmBanner.setDescription(jsonObject2.getString(JSONTag.SHORTDESCRIPTION.getTag()));
filmBanner.setImage(jsonObject2.getString(JSONTag.IMAGE.getTag()));
filmBanner.setThumb(jsonObject2.getString(JSONTag.THUMBNAIL.getTag()));
filmBanner.setType(jsonObject2.getString(JSONTag.TYPE.getTag()));
banner.put(j, filmBanner);
}
home.setCategory(jsonObj.getString(JSONTag.CATEGORY.getTag()));
home.setImages(banner);
news.put(i, home);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return news;
}
public static ArrayList<BaseElement> getTheater() {
ArrayList<BaseElement> theaters = new ArrayList<BaseElement>();
try {
JSONObject jsonObject = new JSONObject(
CommonService.readJsonUrl(ServiceURL.THEATER_CONTENT.getUrl()));
JSONArray array = jsonObject.getJSONArray("theater");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObj = array.getJSONObject(i);
Theater theater = new Theater();
theater.setTheaterId(jsonObj.getString(JSONTag.ID.getTag()));
theater.setTheaterImage(jsonObj.getString(JSONTag.IMAGE.getTag()));
theater.setName(jsonObj.getString(JSONTag.NAME.getTag()));
theaters.add(theater);
}
} catch (JSONException e) {
e.printStackTrace();
return null;
}
return theaters;
}
}
答案 0 :(得分:1)
在LazyAdapter类中,在getView()重写中,在返回之前在构建视图上添加OnClickListener:
view.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
activity.getFragmentManager().beginFragmentTransaction()
.replace(
R.id.your_place_for_the_details_fragment,
new DetailsFragment(item.get(position)))
.commit();
}
});
这将提供在单击任何动态生成的视图后显示新片段的方法。请记住,根据项目数组中的JSON信息构建新的Details片段。
您可以考虑使用Retrofit of Square组,以便轻松实现REST JSON通信。 http://square.github.io/retrofit/