2012-03-09 3 views
2

ArrayAdapterをカスタムアダプタ(FancyAdapter)にオーバーライドしているときに、Undefined getLayoutInflater()のようなエラーが発生しました。カスタムアダプタ(FancyAdapter)クラスコードのコンストラクタでPlus(MainActivity.this、android.R.layout.simple_list_item_1、noteList); "MainActivity.this"と "noteList"にアクセスできないというエラーが表示されます。私のコードは次の通りである: ----- ----- MainActivity.javaAndroidのArrayAdapterをオーバーライドする

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> 

私を助けてください...

答えて

0

、ソリューションを手に入れました

MainActivityクラスの中にFancyAdapterクラスを実装しませんでした。私がしたようにすべてのエラーは消えました...

ありがとう:-)

関連する問題