Search code examples
androidlistviewandroid-listviewlistadapter

listView doesn't get filled?


I'm trying to do an app with listView, which is need to be filled from another class... I'm basicly follow this site: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ but somehow my adapter doesn't work right... And do not fill my listView...

I create a new object from my main class like: fillLeftMenu lmenu = new fillLeftMenu(menu);

I used this line for having started the activity, if it's not wrong:

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

On my AndroidManifest.xml, i added this following lines:

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

And the fillLeftMenu class is:

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

}   

}

Solution

  • I think you are getting stuck in the loop in returnRes.

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

    The Left variable is the data set in lAdapter and it has already been updated in the getIndex method. You don't need the loop here at all.