listView没有填满?

时间:2012-02-20 08:15:45

标签: android listview android-listview listadapter

我正在尝试使用listView做一个应用程序,需要从另一个类填充...我基本上都是关注这个网站:http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/但不知何故我的适配器无法正常运行..并且不要填写我的listView ...

我从我的主类创建一个新对象,如:fillLeftMenu lmenu = new fillLeftMenu(menu);

如果没有错误,我用这行来开始活动:

startActivity(new Intent("com.yahya.training.FILLLEFTMENU"));

在我的AndroidManifest.xml上,我添加了以下几行:

   <activity
        android:name=".fillLeftMenu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.yahya.training.FILLLEFTMENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

fillLeftMenu类是:

public class fillLeftMenu extends ListActivity {

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

public fillLeftMenu(View Lmenu) {
    this.menu = Lmenu;
}

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

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

    Runnable viewOrders = new Runnable(){
        public void run() {
            getIndex();
        }
    };

    Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
    thread.start();
    myProgressDialog = ProgressDialog.show(fillLeftMenu.this,"Please wait...", "Retrieving data ...", true);

    leftList.setOnItemClickListener(new OnItemClickListener() {

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

    });

}

private Runnable returnRes = new Runnable() {
    public void run() {
        if(Left != null && Left.size() > 0){
            lAdapter.notifyDataSetChanged();
            for(int i=0;i<Left.size();i++)
            lAdapter.add(Left.get(i));
        }
        myProgressDialog.dismiss();
        lAdapter.notifyDataSetChanged();
    }
};

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);
        }
    }
    catch (Exception e) {
        Log.e("BACKGROUND_PROC", e.getMessage());
    }
    runOnUiThread(returnRes);       
}

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 inflater = LayoutInflater.from(ctx);
            // v = inflater.inflate(R.layout.list_item, 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;
    }

}   

}

2 个答案:

答案 0 :(得分:0)

我认为你在returnRes中陷入了循环。

for(int i=0;i<Left.size();i++)
    lAdapter.add(Left.get(i));

Left变量 lAdapter中的数据集,并且已在getIndex方法中更新。你根本不需要循环。

答案 1 :(得分:0)

您不必为Activity创建对象。您应该首先了解Activity的工作原理。 Activity Fundamental

看看这个 - Creating an object of Android Activity class

请尝试按照教程完全

以下是您可以开始的其他一些教程。 http://www.vogella.de/articles/AndroidListView/article.html

您可以在Android SDK目录中找到示例。