无法填写listView?

时间:2012-02-20 14:38:00

标签: android listview android-listview

我有一个listview,我开始意图填写它。但列表显示默认图像而没有文本。它应该显示不同的图像和文字。

我有两个数组,一个是字符串类型,另一个是drawable类型......但我的列表并没有告诉我这些我想要的......

我的ListActivity:

public class fillLeftMenu extends ListActivity {

private View menu;
ImageView findLocation;
Spinner cityList;
ListView leftList;
String [] textIndex;
Drawable [] imageIndex;
ArrayList<LeftListItems> Left;
listAdapter lAdapter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     

    dataTransfer dt = BonubonActivity.deneme;
    menu = dt.getView();


    findLocation = (ImageView) menu.findViewById(R.id.image_findLocation);
    leftList = (ListView) menu.findViewById(R.id.list);

    // add items to listView
    Left = new ArrayList<LeftListItems>();
    textIndex = new String[] {"Bugünün Fırsatları", "Son Dakika Bonusları", "Kadın", "Aile", "Çocuk", "Alışveriş", "Şehirden Kaçış", "Kışa Özel"};
    imageIndex = new Drawable[] {leftList.getResources().getDrawable(R.drawable.kucukicon_01),leftList.getResources().getDrawable(R.drawable.kucukicon_02),leftList.getResources().getDrawable(R.drawable.kucukicon_03),leftList.getResources().getDrawable(R.drawable.kucukicon_04),leftList.getResources().getDrawable(R.drawable.kucukicon_05),leftList.getResources().getDrawable(R.drawable.kucukicon_06),leftList.getResources().getDrawable(R.drawable.kucukicon_07),leftList.getResources().getDrawable(R.drawable.kucukicon_08)};

    lAdapter = new listAdapter(menu.getContext(), R.layout.list_item, Left);
    leftList.setAdapter(lAdapter);

    getIndex();

    lAdapter.notifyDataSetChanged();

    leftList.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
        }

    });

    finish();

}

private void getIndex() {
    try{
        LeftListItems item = new LeftListItems();
        for(int i=0; i<textIndex.length; i++) {
            item.setText(textIndex[i]);
            item.setImage(imageIndex[i]);
            Left.add(item);
            lAdapter.add(Left.get(i));
        }
    }
    catch (Exception e) {
        Log.e("BACKGROUND_PROC", e.getMessage());
    }   
}

private class listAdapter extends ArrayAdapter<LeftListItems> {

    private ArrayList<LeftListItems> items;
    private Context ctx;

    public listAdapter(Context ctx, int textViewResourceId, ArrayList<LeftListItems> items) {
        super(ctx, textViewResourceId, items);
        this.ctx = ctx;
        this.items = items;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }

        LeftListItems index = items.get(position);
        if(index != null) {
            TextView text = (TextView) v.findViewById(R.id.text);
            ImageView img = (ImageView) v.findViewById(R.id.icon);
            if(text != null)
                text.setText(index.getText());
            if(img != null)
                img.setBackgroundDrawable(index.getImage());
        }

        return v;
    }       
}

 }

1 个答案:

答案 0 :(得分:0)

在创建适配器并将其设置为列表之前,请尝试调用getIndex()方法。像这样:

getIndex();

//then create new adapter

lAdapter = new listAdapter(menu.getContext(), R.layout.list_item, Left);
leftList.setAdapter(lAdapter);