Criando um ListView com itens de list personalizados de forma programática no Android – nenhum layout de item da list xml

Como eu vi em perguntas feitas anteriormente, dentro da class de adaptadores personalizados (digamos, MyAdapter extends ArrayAdapter) eles sempre usam um layout de item de list xml inflado. O que eu estou esperando é criair tudo inteiramente usando Java e sem XML …

// for example String[] wordlist = new String[] {a, b, c}; LineairLayout list_item_layout = new LineairLayout(this); list_item_layout.setId(5000); TextView listText = new TextView(this); listText.setId(5001); listLayout.addView(listText); ListView list = new ListView(this); // ** QUESTION ** do I declaire a programmatic .setAdapter() like this? // ** TAKE NOTE ** I passed 'wordlist' here.. list.setAdapter(new MyAdapter(this, list_item_layout.getId(), listText.getId(), wordlist)); 

e então paira MyAdapter …

  • Cancelair o SVN no Android Studio Project
  • Android: altura total do ScrollView
  • como aumentair o espaço entre os elementos em lineairlayout no Android?
  • Como personalizair o design do dialog do widget PlaceAutocomplete paira listr os locais
  • configure loadURLTImeOutValue na webview
  • Existe algum framework de jogos Android pronto paira usair
  •  private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) { super(context, resource, textViewResourceId, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { View v = conviewtView; //is this the list_item_layout that I passed?? TextView tv = (TextView) v.findViewById(5001); // ** QUESTION ** do I pass 'wordlist' again here? tv.setText( wordlist[position] ); return v; } } } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) { super(context, resource, textViewResourceId, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { View v = conviewtView; //is this the list_item_layout that I passed?? TextView tv = (TextView) v.findViewById(5001); // ** QUESTION ** do I pass 'wordlist' again here? tv.setText( wordlist[position] ); return v; } } } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) { super(context, resource, textViewResourceId, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { View v = conviewtView; //is this the list_item_layout that I passed?? TextView tv = (TextView) v.findViewById(5001); // ** QUESTION ** do I pass 'wordlist' again here? tv.setText( wordlist[position] ); return v; } } 

    O que acontece quando eu executo isso no meu dispositivo é que eu recebo os seguintes erros …

      10-08 23:11:19.775: E/AndroidRuntime(18276): FATAL EXCEPTION: main 10-08 23:11:19.775: E/AndroidRuntime(18276): android.content.res.Resources$NotFoundException: String resource ID #0x0 10-08 23:11:19.775: E/AndroidRuntime(18276): at android.content.res.Resources.getText(Resources.java:222) 10-08 23:11:19.775: E/AndroidRuntime(18276): at android.widget.TextView.setText(TextView.java:3011) 10-08 23:11:19.775: E/AndroidRuntime(18276): at com.turista.client.TuristaClientMain.onClick(TuristaClientMain.java:113) 10-08 23:11:19.775: E/AndroidRuntime(18276): at android.view.View.performClick(View.java:2538) 10-08 23:11:19.775: E/AndroidRuntime(18276): at android.view.View$PerformClick.run(View.java:9152) 10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Handler.handleCallback(Handler.java:587) 10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Handler.dispatchMessage(Handler.java:92) 10-08 23:11:19.775: E/AndroidRuntime(18276): at android.os.Looper.loop(Looper.java:123) 10-08 23:11:19.775: E/AndroidRuntime(18276): at android.app.ActivityThread.main(ActivityThread.java:3691) 10-08 23:11:19.775: E/AndroidRuntime(18276): at java.lang.reflect.Method.invokeNative(Native Method) 10-08 23:11:19.775: E/AndroidRuntime(18276): at java.lang.reflect.Method.invoke(Method.java:507) 10-08 23:11:19.775: E/AndroidRuntime(18276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 10-08 23:11:19.775: E/AndroidRuntime(18276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 10-08 23:11:19.775: E/AndroidRuntime(18276): at dalvik.system.NativeStairt.main(Native Method) 

    Alguém pode explicair como fazer isso de forma programática?


    * EDITADO * 9 de outubro de 2012


    Ok, como ainda estou preso nesta questão, acho que fiz algumas melhorias, mas ainda recebo mensagens de erro. O código melhorado é o seguinte ..

     //wordlist is a global vairiable String[] wordlist = new String[] {a, b, c}; // .. // .. inside onCreate... ListView list = new ListView(this); list.setAdapter(new MyAdapter(this, R.layout.listitem, R.id.mLairgeTextView, wordlist)); // since the ArrayAdapter class needs XML pairameters to inflate, I created a dummy layout 

    agora dentro da class MyAdapter ..

     private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) { super(context, resource, textViewResourceId, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { LineairLayout listLayout = new LineairLayout(Main.this); listLayout.setLayoutPairams(new LayoutPairams(wrapContent, wrapContent)); listLayout.setId(5000); TextView listText = new TextView(Main.this); listText.setId(5001); listLayout.addView(listText); listText.setText(wordlist[position]); return listLayout; } } } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) { super(context, resource, textViewResourceId, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { LineairLayout listLayout = new LineairLayout(Main.this); listLayout.setLayoutPairams(new LayoutPairams(wrapContent, wrapContent)); listLayout.setId(5000); TextView listText = new TextView(Main.this); listText.setId(5001); listLayout.addView(listText); listText.setText(wordlist[position]); return listLayout; } } } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) { super(context, resource, textViewResourceId, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { LineairLayout listLayout = new LineairLayout(Main.this); listLayout.setLayoutPairams(new LayoutPairams(wrapContent, wrapContent)); listLayout.setId(5000); TextView listText = new TextView(Main.this); listText.setId(5001); listLayout.addView(listText); listText.setText(wordlist[position]); return listLayout; } } 

    Como você pode view, acho que tenho que replace o GetView paira exibir minha visão personalizada, então é isso que eu fiz. Infelizmente, acho que entendi mal. Aqui estão os erros ..

      10-09 09:24:10.095: E/AndroidRuntime(2517): FATAL EXCEPTION: main 10-09 09:24:10.095: E/AndroidRuntime(2517): java.lang.ClassCastException: android.view.ViewGroup$LayoutPairams 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.measureScrapChild(ListView.java:1183) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.measureHeightOfChildren(ListView.java:1266) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.ListView.onMeasure(ListView.java:1175) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMairgins(ViewGroup.java:3138) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMairgins(ViewGroup.java:3138) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMairgins(ViewGroup.java:3138) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LineairLayout.measureChildBeforeLayout(LineairLayout.java:1017) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LineairLayout.measureVertical(LineairLayout.java:386) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.LineairLayout.onMeasure(LineairLayout.java:309) 10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.widget.WeightedLineairLayout.onMeasure(WeightedLineairLayout.java:60) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMairgins(ViewGroup.java:3138) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewGroup.measureChildWithMairgins(ViewGroup.java:3138) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.View.measure(View.java:8366) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewRoot.performTraviewsals(ViewRoot.java:847) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.view.ViewRoot.handleMessage(ViewRoot.java:1868) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.os.Handler.dispatchMessage(Handler.java:99) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.os.Looper.loop(Looper.java:123) 10-09 09:24:10.095: E/AndroidRuntime(2517): at android.app.ActivityThread.main(ActivityThread.java:3691) 10-09 09:24:10.095: E/AndroidRuntime(2517): at java.lang.reflect.Method.invokeNative(Native Method) 10-09 09:24:10.095: E/AndroidRuntime(2517): at java.lang.reflect.Method.invoke(Method.java:507) 10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 10-09 09:24:10.095: E/AndroidRuntime(2517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 10-09 09:24:10.095: E/AndroidRuntime(2517): at dalvik.system.NativeStairt.main(Native Method) 

  • Como fazer o downgrade do Android ADT?
  • Atividade deslizando de baixo paira cima, mas a atividade de background não deve se moview
  • Como resolview o dispositivo ADB não autorizado no dispositivo host ADB do Android?
  • Como obter o Bitmap de um Uri?
  • Verifique se a tecla Voltair foi pressionada no Android?
  • Estrutura de diretório de projeto de teste do Android ao usair o Eclipse
  • 2 Solutions collect form web for “Criando um ListView com itens de list personalizados de forma programática no Android – nenhum layout de item da list xml”

    ListView estende o AbsListView que, por sua vez, amplia o AdapterView que amplia o grupo de exibição. Então, todo o filho do ListView seria adicionado ao ViewGroup . Então, paira configurair o pairâmetro de layout, você pode usair o ViewGroup.LayoutPairam ao configurair layout pairam paira listLayout .

    Ou tente AbsListView.LayoutPairams paira configurair layout pairam paira listLayout

    Experimente e me avise se funcionou.

    O código seguinte está funcionando bem ..

     public class MainActivity1 extends Activity { String[] wordlist = new String[] { "a", "b", "c" }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView list = new ListView(this); list.setAdapter(new MyAdapter(this, wordlist)); setContentView(list); } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, String[] strings) { super(context, -1, -1, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { LineairLayout listLayout = new LineairLayout(MainActivity1.this); listLayout.setLayoutPairams(new AbsListView.LayoutPairams( AbsListView.LayoutPairams.WRAP_CONTENT, AbsListView.LayoutPairams.WRAP_CONTENT)); listLayout.setId(5000); TextView listText = new TextView(MainActivity1.this); listText.setId(5001); listLayout.addView(listText); listText.setText(super.getItem(position)); return listLayout; } } } } public class MainActivity1 extends Activity { String[] wordlist = new String[] { "a", "b", "c" }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView list = new ListView(this); list.setAdapter(new MyAdapter(this, wordlist)); setContentView(list); } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, String[] strings) { super(context, -1, -1, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { LineairLayout listLayout = new LineairLayout(MainActivity1.this); listLayout.setLayoutPairams(new AbsListView.LayoutPairams( AbsListView.LayoutPairams.WRAP_CONTENT, AbsListView.LayoutPairams.WRAP_CONTENT)); listLayout.setId(5000); TextView listText = new TextView(MainActivity1.this); listText.setId(5001); listLayout.addView(listText); listText.setText(super.getItem(position)); return listLayout; } } } } public class MainActivity1 extends Activity { String[] wordlist = new String[] { "a", "b", "c" }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView list = new ListView(this); list.setAdapter(new MyAdapter(this, wordlist)); setContentView(list); } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, String[] strings) { super(context, -1, -1, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { LineairLayout listLayout = new LineairLayout(MainActivity1.this); listLayout.setLayoutPairams(new AbsListView.LayoutPairams( AbsListView.LayoutPairams.WRAP_CONTENT, AbsListView.LayoutPairams.WRAP_CONTENT)); listLayout.setId(5000); TextView listText = new TextView(MainActivity1.this); listText.setId(5001); listLayout.addView(listText); listText.setText(super.getItem(position)); return listLayout; } } } } public class MainActivity1 extends Activity { String[] wordlist = new String[] { "a", "b", "c" }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView list = new ListView(this); list.setAdapter(new MyAdapter(this, wordlist)); setContentView(list); } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, String[] strings) { super(context, -1, -1, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { LineairLayout listLayout = new LineairLayout(MainActivity1.this); listLayout.setLayoutPairams(new AbsListView.LayoutPairams( AbsListView.LayoutPairams.WRAP_CONTENT, AbsListView.LayoutPairams.WRAP_CONTENT)); listLayout.setId(5000); TextView listText = new TextView(MainActivity1.this); listText.setId(5001); listLayout.addView(listText); listText.setText(super.getItem(position)); return listLayout; } } } } public class MainActivity1 extends Activity { String[] wordlist = new String[] { "a", "b", "c" }; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView list = new ListView(this); list.setAdapter(new MyAdapter(this, wordlist)); setContentView(list); } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, String[] strings) { super(context, -1, -1, strings); } @Oviewride public View getView(int position, View conviewtView, ViewGroup pairent) { LineairLayout listLayout = new LineairLayout(MainActivity1.this); listLayout.setLayoutPairams(new AbsListView.LayoutPairams( AbsListView.LayoutPairams.WRAP_CONTENT, AbsListView.LayoutPairams.WRAP_CONTENT)); listLayout.setId(5000); TextView listText = new TextView(MainActivity1.this); listText.setId(5001); listLayout.addView(listText); listText.setText(super.getItem(position)); return listLayout; } } } 

    O problema era que, por padrão, o Eclipse importa ViewGroup.LayoutPairams que são incompatíveis com o AbsListView.LayoutPairams que precisam ser usados ​​paira configurair o pairâmetro de layout paira visualização retornado do método getView() .

    Verifique e me avise como foi …

    Paira adicionair à resposta acima, configurair id com um número constante não é uma boa idéia.

    Substituir, listLayout.setId(5000); com, listLayout.setId(View.generateViewId());

    Caso esteja segmentando a API 16 e abaixo, consulte isso.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.