您需要创建自己的自定义 Listview 来显示更多的字符串。这是一段您可以参考的代码:
电影适配器.java:
public class MovieAdaptor extends ArrayAdapter<Movie> {
Context context;
int layoutResourceId;
ArrayList<Movie> movieArrayList = new ArrayList<Movie>();
public MovieAdaptor(Context context, int layoutResourceId, ArrayList<Movie> movies) {
super(context, layoutResourceId, movies);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.movieArrayList = movies;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ContactHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ContactHolder();
holder.thumbnail = (ImageView) row.findViewById(R.id.imageThumbnail);
holder.title = (TextView) row.findViewById(R.id.MovieTitle);
holder.synopsis = (TextView) row.findViewById(R.id.synopsis);
row.setTag(holder);
} else {
holder = (ContactHolder) row.getTag();
}
holder.title.setText(movieArrayList.get(position).title);
holder.synopsis.setText(movieArrayList.get(position).synopsis);
newAsyncDownloadImage(holder.thumbnail).execute(movieArrayList.get(position).posters.get("thumbnail"));
return row;
}
static class ContactHolder {
ImageView thumbnail;
TextView title;
TextView synopsis;
}
}
活动类:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
moviesListView = (ListView) findViewById(R.id.list_movies);
moviesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position > HEADER) {
Intent intent = new Intent(getBaseContext(), MovieActivity.class);
intent.putExtra(MOVIE_OBJECT, movieArrayList.get(position - 1));
startActivity(intent);
}
}
});
View header = (View) getLayoutInflater().inflate(R.layout.listview_row_header, null);
moviesListView.addHeaderView(header);
movieAdaptor = new MovieAdaptor(this, R.layout.listview_row_item, movieArrayList);
moviesListView.setAdapter(movieAdaptor);
AsyncDownloadImage.java:
public class AsyncDownloadImage extends AsyncTask<Object, Object, Object> {
ImageView iv;
private HttpURLConnection connection;
private InputStream is;
private Bitmap bitmap;
public AsyncDownloadImage(ImageView mImageView) {
iv = mImageView;
}
@Override
protected Object doInBackground(Object... params) {
URL url;
try {
url = new URL((String) params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
is = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (connection != null) {
connection.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
if (null != result) {
iv.setImageBitmap((Bitmap) result);
} else {
iv.setBackgroundResource(R.drawable.ic_launcher);
}
}
}
}