【问题标题】:listView doesn't get filled?listView 没有被填满?
【发布时间】:2012-02-20 08:15:45
【问题描述】:

我正在尝试使用 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;
    }

}   

}

【问题讨论】:

  • 发布您的主要方法和清单。 fillLeftMenu lmenu = new fillLeftMenu(menu); 这是什么意思。
  • 按照教程完全正确。并尝试将您的班级名称大写。
  • 你可以关注这个链接vogella.de/articles/AndroidListView/…
  • @Shaiful 你是对的,也许我在 main 上做错了......我如何在创建 fillLeftMenu 对象后开始这个活动?
  • 您想在应用程序启动时启动活动吗?或者想从另一个活动开始。

标签: android listview android-listview listadapter


【解决方案1】:

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

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

Left 变量是lAdapter 中的数据集,它已经在getIndex 方法中更新。你根本不需要这里的循环。

【讨论】:

    【解决方案2】:

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

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

    请尝试完全按照教程进行操作。

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

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

    【讨论】:

      猜你喜欢
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 2012-10-03
      • 2011-02-10
      • 2018-11-30
      相关资源
      最近更新 更多