在Android中覆盖ArrayAdapter

时间:2012-03-09 07:00:36

标签: android android-arrayadapter override

在将ArrayAdapter重写为自定义适配器(FancyAdapter)时,我遇到了像Undefined getLayoutInflater()这样的错误。另外在自定义适配器(FancyAdapter)类代码super(MainActivity.this,android.R.layout.simple_list_item_1,noteList)的构造函数中;在“MainActivity.this”和“noteList”中收到错误,表明它们无法访问。我的守则如下: ----- ----- MainActivity.java

package my.android.notelist;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {
    ArrayList <String> noteList = new ArrayList<String> ( );
FancyAdapter aa = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final EditText txtTodo = ( EditText ) findViewById ( R.id.txtTodo );
    Button btAdd = ( Button ) findViewById ( R.id.btnAdd );
    ListView lvList = ( ListView ) findViewById ( R.id.lvList );

    aa = new FancyAdapter( );

    lvList.setAdapter( aa );

    btAdd.setOnClickListener(new View.OnClickListener()
    {           
        public void onClick(View v)
        {
            noteList.add( txtTodo.getText().toString());
            aa.notifyDataSetChanged();
            txtTodo.setText("");
        }
    });
    }
}

class FancyAdapter extends ArrayAdapter<String>
{
public FancyAdapter ( )
{
    super ( MainActivity.this, android.R.layout.simple_list_item_1, noteList );
}

public View getView ( int position, View convertView, ViewGroup parent )
{
    View row = convertView;

    if ( row == null )
    {
        final LayoutInflater inflater =  getLayoutInflater ( );
        row = inflater.inflate(R.layout.custom_list_item, null);

        ( ( TextView ) row.findViewById(R.id.noteText)).setText ( noteList.get(position));
        return (row);
    }
    }
}    

- - - - - - - - main.xml中

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

<EditText
    android:id="@+id/txtTodo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add Item" />

<ListView
    android:id="@+id/lvList"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</ListView>

</LinearLayout>

------- ------- custom_list_item.xml

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/noteText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

请帮帮我......

1 个答案:

答案 0 :(得分:0)

得到解决方案,

我没有在MainActivity类中实现FancyAdapter类。正如我所做的那样,所有的错误都消失了......

谢谢: - )