机器人:BaseAdapter没有显示元素 [英] Android: BaseAdapter not showing elements

查看:150
本文介绍了机器人:BaseAdapter没有显示元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力使该应用程序有一个简单的列表,并在底部的一个按钮present不管。我的问题是我的自定义BaseAdapter不显示的元素。我知道,因为我的元素只是一个字符串,我可以使用一个ArrayAdapter,但分配需要它。在code:

The app I'm struggling to make has a simple list and a button present in the bottom no matter what. My problem is that my custom BaseAdapter doesn't show elements. I know that since my elements are only a string I could use an ArrayAdapter, but the assignment requires it. The code:

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文件是这样的:

My XML files go like this:

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>

按钮楼下打开一个对话框,增加了项目到我的清单。这显然​​是行不通的,但我认为这是BaseAdapter(ListaOrase)本身,作为意图抛出没有例外。我会感激,如果我的项目很难codeD中会出现(布加勒斯特和锡比乌)。

The button "downstairs" opens a dialog that adds items to my list. It obviously doesn't work, but I think it's the BaseAdapter (ListaOrase) itself, as the intents throw no exceptions. I would be thankful if the items I hardcoded in would show up (Bucuresti and Sibiu).

我是什么做错了吗?
非常感谢你! :)

What am I doing wrong? Thank you very much! :)

推荐答案

您code几乎完美的问题是回报orase.size()中的getCount()方法,你正在返回0,加入ListaOrase类未实现的方法。
您ListaOrase应该是这样的:

Your code almost perfect issue is return orase.size() in getcount() method,you are returning 0, add unimplemented methods in ListaOrase class. your ListaOrase should be like this:

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;
    }

}

希望这有助于

这篇关于机器人:BaseAdapter没有显示元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆