I am trying out this code to understand Android-Adapter.But every time I run this,it shows unfortunately stopped and I can't figure out why.
Here is the MainActivity.java class :
package com.example.adapterexample;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
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 MainActivity extends ActionBarActivity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listcomeon);
lv.setAdapter(new AdapterEx(this));
}
}
class Single_Row {
String title;
int image;
public Single_Row(String title, int image) {
this.title = title;
this.image = image;
}
}
class AdapterEx extends BaseAdapter {
ArrayList<Single_Row> lister;
Context con;
public AdapterEx(Context c) {
con = c;
lister = new ArrayList<Single_Row>();
Resources res = c.getResources();
String[] title = res.getStringArray(R.id.title);
int[] image = { R.drawable.earth, R.drawable.mars, R.drawable.mercury,
R.drawable.urenus, R.drawable.neptune, R.drawable.pluto,
R.drawable.venus };
for (int i = 0; i <= lister.size(); i++)
{
lister.add(new Single_Row(title[i], image[i]));
}
}
@Override
public int getCount() {
return lister.size();
}
@Override
public Object getItem(int i) {
return lister.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewgrp) {
LayoutInflater inflater = (LayoutInflater) con
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, viewgrp, true);
TextView title = (TextView) row.findViewById(R.id.Titlear);
ImageView image = (ImageView) row.findViewById(R.id.ImageShow);
Single_Row temp = lister.get(i);
title.setText(temp.title);
image.setImageResource(temp.image);
return row;
}
}
Xml code :
single_row.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/ImageShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#d0d3d5"
android:textSize="16dp" />
<TextView
android:id="@+id/Titlear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#d0d3d5"
android:textSize="16dp" />
</LinearLayout>
activity_main.xml :
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.adapterexample.MainActivity" >
<ListView android:id="@+id/listcomeon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="1dp" />
</RelativeLayout>
 
No comments:
Post a Comment