0

我在编码什么?

一个有 10 个字符串的网格,只是一个 Activity 中的一个显示网格。

问题是什么?

我知道我调用了适配器两次,但我不知道如何调用一次来加载我的数据

我阅读了 Android 文档,发现我可以使用 GridView 来接收我的 ListView 数组来创建我需要的视图。但是,我看到了 ImageView 的示例(在 getView 中有一个 set 方法来加载图像),但我无法加载我的 ListView。我不知道在哪里设置数组,如果它在我的适配器的 getView() 方法中或在主要活动中。(可能是 getView())。

这是我的代码和异常:

课程;

活动;

public class ConsultaSinistroActivity extends Activity {

private static Context context;
private static String idEvento;
private GridView gridView;  

private List<String[]> lstStrings;
static final String[] cabecalho = new String[] { "No. Sinistro",
    "Data Ocorrencia", "Tipo de Sinistro", "Sucursal", "Ramo",
    "Apolice", "Tipo", "Endereco", "Nro", "Complemento", "Bairro",
    "CEP", "Cidade", "Estado" };
static final String[] sinistroTesteUm = new String[] { "12345678",
    "01/09/2012", "Acidente Carro", "499", "1391", "823752", "C",
    "Rua Aberte Douglas", "33", "Complemento", "Vila Vera",
    "04272-000", "São Paulo", "SP" };


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_consulta_sinistro);
    context = this;

    lstStrings = new ArrayList<String[]>();
    lstStrings.add(sinistroTesteUm);


    gridView = (GridView) findViewById(R.id.gridViewSinistro);
    gridView.setAdapter(new ConsultaSinistroAdapter(this, lstStrings)); 

}

/**
 * @return the idEvento
 */
public static String getIdEvento() {
    return idEvento;
}

/**
 * @param idEvento
 *            the idEvento to set
 */
public static void setIdEvento(String idEvento) {
    ConsultaSinistroActivity.idEvento = idEvento;
}

}

适配器:

public class ConsultaSinistroAdapter extends BaseAdapter {

private Context context;
private List<String[]> lstStrings;
static final String[] sinistroTesteUm = new String[] { "12345678",
    "01/09/2012", "Acidente Carro", "499", "1391", "823752", "C",
    "Rua Aberte Douglas", "33", "Complemento", "Vila Vera",
    "04272-000", "São Paulo", "SP" };
static final String[] cabecalho = new String[] { "No. Sinistro",
    "Data Ocorrencia", "Tipo de Sinistro", "Sucursal", "Ramo",
    "Apolice", "Tipo", "Endereco", "Nro", "Complemento", "Bairro",
    "CEP", "Cidade", "Estado" };

/**
 * Construtor.
 * 
 * @param context
 * @param lstStrings
 */
public ConsultaSinistroAdapter(final Context context, List<String[]> lstStrings) {
    super();
    this.context = context;
    this.lstStrings = lstStrings;
}

@Override
public int getCount() {
    return lstStrings.size();
}

@Override
public Object getItem(int position) {
    return lstStrings.get(position);
}

@Override
public long getItemId(int arg0) {
    return 0;
}

@Override
public View getView(int position, View view, ViewGroup arg2) {

    lstStrings = new ArrayList<String[]>();
    lstStrings.add(sinistroTesteUm);

        ListView listView;
        if (view == null) { 
            listView = new ListView(context);
            listView.setLayoutParams(new GridView.LayoutParams(85, 85));

        } else {
            listView = (ListView) view;
        }

        listView.setAdapter(new ConsultaSinistroAdapter(context, lstStrings));

        return listView;            

}

}

xml:

只是一个简单的GridView。

例外:

08-20 22:34:12.187: E/AndroidRuntime(1641): FATAL EXCEPTION: main
08-20 22:34:12.187: E/AndroidRuntime(1641): java.lang.StackOverflowError
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.content.res.Resources.getValue(Resources.java:1010)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.content.res.Resources.getDrawable(Resources.java:659)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.EdgeEffect.<init>(EdgeEffect.java:137)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.AbsListView.setOverScrollMode(AbsListView.java:832)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.view.View.<init>(View.java:3237)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.view.View.<init>(View.java:3281)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.view.ViewGroup.<init>(ViewGroup.java:431)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.AdapterView.<init>(AdapterView.java:235)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.AbsListView.<init>(AbsListView.java:766)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.ListView.<init>(ListView.java:143)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.ListView.<init>(ListView.java:139)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.ListView.<init>(ListView.java:135)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at com.porto.questionario.adapter.ConsultaSinistroAdapter.getView(ConsultaSinistroAdapter.java:61)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.AbsListView.obtainView(AbsListView.java:2159)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.ListView.makeAndAddView(ListView.java:1831)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.ListView.fillDown(ListView.java:674)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.ListView.fillFromTop(ListView.java:735)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.ListView.layoutChildren(ListView.java:1652)
08-20 22:34:12.187: E/AndroidRuntime(1641):     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
4

1 回答 1

0

您不能在getView(). getView()为 中的一行创建视图ListView。如果你想交换ListAdapter,那么你需要找到另一种方式。

于 2013-08-20T23:58:14.657 回答