在数组适配器中保留空视图

时间:2012-01-01 16:18:56

标签: android

我的数组适配器看起来像这样:

http://www.airbrush-ernst.nl/domo1.jpg

我也没有发布图片:(

我也希望忽略空洞的观点。我刚刚开始使用android,我试图操纵getview,但这不起作用。

任何人都可以帮助我。

TNX

这是我的代码

    public class MyCustomAdapter extends ArrayAdapter<String> {

    public MyCustomAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        // return super.getView(position, convertView, parent);

        View row = convertView;


        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.row, parent, false);

        }

        TextView label = (TextView) row.findViewById(R.id.rowText);
        label.setText(locatie[position]);
        ImageView icon = (ImageView) row.findViewById(R.id.icon);

        if (position == 0 && on1) {
            icon.setImageResource(R.drawable.bulpon);
        } else if (position == 1 && on2) {
            icon.setImageResource(R.drawable.bulpon);
        } else {
            icon.setImageResource(R.drawable.bulpoff);
        }

        return row;

    }
}




String[] locatie = { "Huiskamer - achter TV", "Huiskamer - kast",
        "Huiskamer - achter", "Keuken", "Voordeur", "", "", "", "",
        "", "" };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    haaldata();
    // setListAdapter(new MyCustomAdapter(Binnen.this, R.layout.row,
    // locatie));

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    SharedPreferences getPrefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    String prefM1 = getPrefs.getString("menu1", "");
    if (!prefM1.trim().contentEquals(""))
        locatie[0] = prefM1;
    String prefM2 = getPrefs.getString("menu2", "");
    if (!prefM2.trim().contentEquals(""))
        locatie[1] = prefM2;
    String prefM3 = getPrefs.getString("menu3", "");
    if (!prefM3.trim().contentEquals(""))
        locatie[2] = prefM3;
    String prefM4 = getPrefs.getString("menu4", "");
    if (!prefM4.trim().contentEquals(""))
        locatie[3] = prefM4;
    String prefM5 = getPrefs.getString("menu5", "");
    if (!prefM5.trim().contentEquals(""))
        locatie[4] = prefM5;
    String prefM6 = getPrefs.getString("menu6", "");
    if (!prefM6.trim().contentEquals(""))
        locatie[5] = prefM6;
    String prefM7 = getPrefs.getString("menu7", "");
    if (!prefM7.trim().contentEquals(""))
        locatie[6] = prefM7;
    String prefM8 = getPrefs.getString("menu8", "");
    if (!prefM8.trim().contentEquals(""))
        locatie[7] = prefM8;
    String prefM9 = getPrefs.getString("menu9", "");
    if (!prefM9.trim().contentEquals(""))
        locatie[8] = prefM9;
    String prefM10 = getPrefs.getString("menu10", "");
    if (!prefM10.trim().contentEquals(""))
        locatie[9] = prefM10;



    setListAdapter(new MyCustomAdapter(Binnen.this, R.layout.row, locatie));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);
    MenuInflater blowUp = getMenuInflater();
    blowUp.inflate(R.menu.cool_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    super.onOptionsItemSelected(item);
    switch (item.getItemId()) {
    case R.id.aboutus:
        Intent i = new Intent("nl.ernst.splash.ABOUT");
        startActivity(i);

        break;
    case R.id.preferences:
        Intent p = new Intent("nl.ernst.splash.PREFS");
        startActivity(p);
        break;
    case R.id.exit:
        finish();
        break;

    }
    return false; // onOptionsItemSelected verwacht een boolean terug dus
                    // geef maar wat terug :-)
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    // super.onListItemClick(l, v, position, id);
    String selection = l.getItemAtPosition(position).toString();
    Toast.makeText(this, selection, Toast.LENGTH_SHORT).show();
}

private void haaldata() {
    // TODO Auto-generated method stub
    URL textUrl;
    try {
        textUrl = new URL(outSource);
        BufferedReader bufferReader = new BufferedReader(
                new InputStreamReader(textUrl.openStream()));
        String StringBuffer;
        String stringText = "";
        while ((StringBuffer = bufferReader.readLine()) != null) {
            stringText += StringBuffer;
        }
        bufferReader.close();

        Toast.makeText(this, stringText, Toast.LENGTH_SHORT).show();

        String str[] = stringText.split(",");
        if (str[0].equals("1")) {
            on1 = true;
        } else {
            on1 = false;
        }

        if (str[1].equals("1")) {
            on2 = true;
        } else {
            on2 = false;
        }

        // msg.setText(stringText);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block e.printStackTrace();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
}
    }// class Binnen

1 个答案:

答案 0 :(得分:1)

在你的适配器中

你要创建这个方法

public class MyCustomAdapter extends BaseAdapter{
...
public int getCount() {
    return 5;//or however many you want to have total
}
...
}