0

My java Code is:

public class MainActivity extends Activity {

    String[] name;
    String[] family;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name=new String[]{"aaa","bbb","ccc","ddd","fff"};
        family=new String[]{"Ahamadi","Ahamadi","Ahamadi","Ahamadi","Ahamadi"};

        ListView list=(ListView) findViewById(R.id.list_View);

        MyAdapter adapter=new MyAdapter(getApplicationContext());

        list.setAdapter(adapter);
    }//end method OnCreate

    private class MyAdapter extends BaseAdapter

    {
        LayoutInflater MyInflater;
        public MyAdapter(Context context)
        {
            MyInflater=LayoutInflater.from(context);
        }




        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return name.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder X=new ViewHolder();

            if(convertView==null)
            {
                convertView=MyInflater.inflate(R.layout.list_item,null);

                X.Name=(TextView) convertView.findViewById(R.id.Text_name);
                X.Family=(TextView) convertView.findViewById(R.id.Text_family);

                convertView.setTag(X);
            }

            else{

                X=(ViewHolder)convertView.getTag();
            }


            X.Name.setText(name[position]);
            X.Family.setText(family[position]);


            // TODO Auto-generated method stub
            return convertView;
        }


        //-class Holder
        public class ViewHolder
        {

            TextView Name;
            TextView Family;
        }
    }
}

and create a listView in Xml. While the programme runs, it shows the massage: "Applecation is stopped".

gung - Reinstate Monica
  • 10,603
  • 7
  • 53
  • 74

2 Answers2

0

I see your code is correct, but probably you don´t have the elements inside your layout files:

for example your ListView:

 ListView list=(ListView) findViewById(R.id.list_View);

must be inside the activity_main.xml (example):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView 
    android:id="@+id/list_View"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" /> 

</RelativeLayout>

and your list_item.xml must contain the TextView´s Text_name and Text_family (example):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"
    android:layout_height="match_parent">

         <TextView
            android:id="@+id/Text_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingTop="10dp"/>
         <TextView
            android:id="@+id/Text_family"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="5dp"
            android:paddingTop="10dp"/>

</LinearLayout>

try using my .xml files, and please try to post the messages displayed in LogCat everytime you ask for a problem here in stackoveflow.com =)

Jorgesys
  • 114,263
  • 22
  • 306
  • 247
0

change this:

public MyAdapter(Context context){
     MyInflater=LayoutInflater.from(context);
}

by this :

public MyAdapter(Context context) {
    MyInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

In your Activity, onCreate()

MyAdapter adapter = new MyAdapter(getBaseContext());
list.setAdapter(adapter);
Anderson K
  • 5,115
  • 5
  • 29
  • 48