2017-03-13 3 views
0

MainActivity.javaIch versuche, eine benutzerdefinierte Listenansicht mit ArrayAdapter zu implementieren, aber alle Elemente sind nicht im Bildschirm

public class MainActivity extends Activity { 

ListView list; 
String[] titles; 
String[] description; 
int imgs[]={R.drawable.facebook,R.drawable.instagram,R.drawable.twitter,R.drawable.google}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Resources res = getResources(); 

    titles = res.getStringArray(R.array.titles); 
    description = res.getStringArray(R.array.description); 

    list = (ListView) findViewById(R.id.list1); 

    MyAdapter adapter = new MyAdapter(this,titles,imgs,description); 
    list.setAdapter(adapter); 

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Toast.makeText(getApplicationContext(),Integer.toString(position),Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 


class MyAdapter extends ArrayAdapter<String> { 

    Context context; 
    String myTitles[]; 
    String myDescription[]; 
    int[] imgss; 

    MyAdapter(Context c, String[] titles, int[] img, String[] description) { 
     super(c,R.layout.row,R.id.text1,titles); 
     this.context=c; 
     this.imgss=img; 
     this.myTitles=titles; 
     this.myDescription=description; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View row = layoutInflater.inflate(R.layout.row, parent, false); 
    ImageView images = (ImageView) findViewById(R.id.logo); 
    TextView myTitle = (TextView) findViewById(R.id.text1); 
    TextView myDescription = (TextView) findViewById(R.id.text2); 
    images.setImageResource(R.drawable.facebook); 
    images.setImageResource(imgs[position]); 
    myTitle.setText(titles[position]); 
    myDescription.setText(description[position]); 
    return row; 
    } 
    } 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="16dp" 
tools:context="com.apakdroid.customlistview.MainActivity" > 

    <ListView 
     android:id="@+id/list1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" /> 

</RelativeLayout> 

row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:padding="5dp" 
    android:id="@+id/logo" 
    android:src="@mipmap/ic_launcher"/> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="2dp" 
     android:textColor="#33CC33" 
     android:id="@+id/text1" 
     android:layout_marginTop="5dp" 
     android:layout_marginLeft="10dp" 
     android:text="Medium Text"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/text2" 
     android:layout_marginLeft="10dp" 
     android:text="TextView" /> 

</LinearLayout> 
zeigt

strings.x ml

<resources> 
<string name="app_name">Custom ListView</string> 
<string-array name="titles"> 
    <item>Facebook</item> 
    <item>Instagram</item> 
    <item>Twitter</item> 
    <item>Google</item> 
</string-array> 

<string-array name="description"> 
    <item>Facebook Description</item> 
    <item>Instagram Description</item> 
    <item>Twitter Description</item> 
    <item>Google Description</item> 
</string-array> 

Screenshots (Android und Logcat): - http://imgur.com/a/816Q1

Die ArrayAdapter nur aus img Array den letzten Wert von strings.xml und das letzte Bild zeigt. Es können nicht alle Bilder und Texte angezeigt werden.

In Zeile ist ein Fehler aufgetreten. Images.setImageResource (imgs [position]) als NullPointerException. Logcat ist im Screenshot beigefügt.

Antwort

0

Ich denke, dass Ihr Problem in der getView-Methode oder Ihrem Adapter sein könnte. Sie sollten die Zeilenansicht verwenden, um untergeordnete Steuerelemente zu finden.

View row = layoutInflater.inflate(R.layout.row, parent, false); 
ImageView images = (ImageView) row.findViewById(R.id.logo); 
TextView myTitle = (TextView) row.findViewById(R.id.text1); 
TextView myDescription = (TextView) row.findViewById(R.id.text2); 
+0

Thx für Ihre Info .. Es hat funktioniert .. –

+0

Eine Abstimmung oder die Antwort zu akzeptieren tut nicht weh :) – Luci

+0

Sorry, da dies ein neues Konto der Abstimmung auftaucht ist nicht öffentlich, sondern wird gespeichert .. –

0

Sie hinzufügen müssen Layout Sie Listview/RecyclerView. Vor dem Einstellen des Adapters.

wie tun:

rvCategories.setHasFixedSize(true); 
rvCategories.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)); 

Erinnern Sie sich, wir mehr Layouts Manager haben ..

Und Sie brauchen Initialisierung Ihrer Komponenten. Detail: row.findViewById.

View row = layoutInflater.inflate(R.layout.row, parent, false); 
ImageView images = (ImageView) row.findViewById(R.id.logo); 
TextView myTitle = (TextView) row.findViewById(R.id.text1); 
TextView myDescription = (TextView) row.findViewById(R.id.text2); 
+0

Ich kann nicht verstehen, was du sagst. Ich bin ein Neuling. Kannst du bitte etwas ausarbeiten? Danke –

+0

Thx es hat funktioniert ... –

0

Ich denke, das Problem ist in Ihrer Methode getView(). Sie haben vergessen, ein Objekt Ihrer Ansicht für IDs zu suchen.

View row = layoutInflater.inflate(R.layout.row, parent, false); 
ImageView images = (ImageView) row.findViewById(R.id.logo); 
TextView myTitle = (TextView) row.findViewById(R.id.text1); 
TextView myDescription = (TextView) row.findViewById(R.id.text2); 
Verwandte Themen