質問

私が作るのに苦労しているアプリには、何があっても、単純なリストと下部にボタンが存在します。私の問題は、私のカスタムベースヘアハプターに要素が表示されないことです。私の要素は文字列にすぎないので、ArrayAdapterを使用できますが、割り当てには必要です。コード:

class ListaOrase extends BaseAdapter{
    private Activity context;
    ArrayList<String> orase;

    public ListaOrase(Activity context){
        this.context=context;
        orase=new ArrayList<String>();
    }
    public void add(String string){
        orase.add(string);
    }
public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }


    public View getView (int position, View convertView, ViewGroup list)  {
        View element;
        if (convertView == null)
        {
         LayoutInflater inflater = context.getLayoutInflater();
         element = inflater.inflate(R.layout.lista, null);
        }
        else element = convertView;
        TextView elementLista=(TextView)element.findViewById(R.id.elementLista);    
        elementLista.setText(orase.get(position));
        return element;
    }

}
public class WeatherAppActivity extends ListActivity {

    Button buton;
    ListaOrase lista;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lista=new ListaOrase(this);
        buton=(Button)findViewById(R.id.buton);
        setListAdapter(lista);

        lista.add("Bucuresti");
        lista.add("Sibiu");

    }
}

私のXMLファイルは次のようになります:

main.xml -- for the Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >



<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:layout_alignParentTop="true"/>

<RelativeLayout 
    android:id="@+id/relative"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">
<Button
        android:id="@+id/buton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="add"
        android:text="Add"
        android:layout_gravity=""
        /> 
        </RelativeLayout>

</RelativeLayout>
lista.xml -- for the list
<?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"
    android:orientation="vertical" >
<TextView 
        android:id="@+id/elementLista"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />


</LinearLayout>

ボタン「階下」は、リストにアイテムを追加するダイアログを開きます。それは明らかに機能しませんが、意図が例外を投げかないので、それはベースヘアハプター(listaorase)自体だと思います。私がハードコーディングしたアイテムが現れるかどうか感謝します(BucurestiとSibiu)。

私は何が間違っているのですか?どうもありがとうございました! :)

役に立ちましたか?

解決

コードのほぼ完璧な問題は、getCount()メソッドのreturn orase.size()です。 ListAoraseは次のようにする必要があります:

class ListaOrase extends BaseAdapter{
    private Activity context;
    ArrayList<String> orase;

    public ListaOrase(Activity context){
        this.context=context;
        orase=new ArrayList<String>();
    }
    public void add(String string){
        orase.add(string);
    }

    public View getView (int position, View convertView, ViewGroup list)  {
        View element;
        if (convertView == null)
        {
         LayoutInflater inflater = context.getLayoutInflater();
         element = inflater.inflate(R.layout.lista, null);
        }
        else element = convertView;
        TextView elementLista=(TextView)element.findViewById(R.id.elementLista);    
        elementLista.setText(orase.get(position));
        Log.e("",orase.get(position));
        return element;
    }
    public int getCount() {
        // TODO Auto-generated method stub
        return orase.size();
    }
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

}

お役に立てれば

他のヒント

getCount メソッドは本当に0を非常に慣習的に返していました getView() 呼ばれていませんでした。私が変更したとき:

getcount() {  
    return arrayObject.Size();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top