开发者

Problem with id of ListView

I have a problem my ListActivity.

I have this xml:

<LinearLayout>
    <ListView 
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
</LinearLayout>

and inside I have this:

<LinearLayout>
    <ImageView android:id="@+id/flechas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <TextView android:id="@+id/textoName"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
    />
    <TextView android:id="@+id/textoPrice"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
    />
    <Button android:id="@+id/boton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BUY"
        android:layout_marginRight="0dip"
        android:onClick="buy"
    />
<开发者_开发百科;/LinearLayout>

I use a BaseAdapter to show it, and this is the main file:

public class Info  extends ListActivity{

static ArrayList<Quotes> objeto= null;
static final ArrayList<HashMap<String, String>> lista= new ArrayList<HashMap<String,String>>();
private ListView mainListView;
private InfoModel [] model;
private CustomAdapterInfo listAdapter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mainListView= (ListView)findViewById(R.id.);
    setContentView(R.layout.info);


    objeto= new ArrayList<Quotes>();
    Bundle extras=getIntent().getExtras();
    objeto= extras.getParcelableArrayList("datos");

    if ((objeto != null) && (objeto.size() != 0)){
        model= new InfoModel[objeto.size()];
        for (int i=0;i< objeto.size(); i++) {
            model[i]= new InfoModel(0, objeto.get(i).getName(), objeto.get(i).getTrade());
        }
    }

    ArrayList<InfoModel> datos= new ArrayList<InfoModel>();
    datos.addAll(Arrays.asList(model));

    listAdapter= new CustomAdapterInfo(this, datos);
    mainListView.setAdapter(listAdapter);
}

But my mainListView is null. It doesn't find the R.id.

Any ideas?


android:id="@android:id/list"

I guess this should be your own ID. Like this:

android:id="@+id/main_list_view"

After that you can access it using something like:

mainListView= (ListView)findViewById(R.id.main_list_view);


If you follow this example.

If your activity extends the ListActivity, your XML should have a ListView with:

android:id="@android:id/list"

That's correct. But, the way you initialize the ListView is different:

ListView lv = getListView();

This would do the trick.


"@android:id/list" means it is part of android package you can't call it using R.id.list( try this android.R.id.list) as R class generated for your package by ADT,instead you can get ListView object, as ListView lView=getListView() use setContentView method at top before you finding any View objects from XML(layouts),otherwise you will get NullPointerException.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜