2017-03-02 2 views
0

ListAdapter.javaAndroid - Eine Karte Artikel in gridview wird zweimal wiederholt

public class ListAdapter extends ArrayAdapter<String> 
{ 
    private Activity context; 
    private int[] icon; 
    private String[] title; 

    public ListAdapter(Activity context, int[] icon, String[] title) 
    { 
    super(context, R.layout.list_item, title); 
    this.context = context; 
    this.icon = icon; 
    this.title = title; 
    } 

    public View getView(final int position, View view, final ViewGroup parent) 
    { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View rowView = inflater.inflate(R.layout.list_item, null, true); 
    try 
    { 
     TextView txtTitle = (TextView) rowView.findViewById(R.id.txtTitle); 
     ImageView imgIcon = (ImageView) rowView.findViewById(R.id.imgIcon); 
     txtTitle.setText(title[position]); 
     imgIcon.setBackgroundResource(icon[position]); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 
     return rowView; 
    } 
}} 

MainActivity.java
in dieser Karte Events mit Dienstleistungen ersetzt. Wenn ich die Positionen neu mische, werden die Änderungen in der App angezeigt, aber wenn ich die App erneut starte, wird die Galerie durch Dienste ersetzt.

public class MainActivity extends AppCompatActivity 
{ 
    String[] title = { 
     "Services" , 
     "Solutions", 
     "Customers", 
     "Events", 
     "Gallery", 
     "Contact Us" 
    }; 

int[] icon = { 
     R.drawable.services_icon, 
     R.drawable.solution_icon, 
     R.drawable.customer_icon, 
     R.drawable.event_icon, 
     R.drawable.gallery_icon, 
     R.drawable.contact_us_icon 
}; 

    HomeGrid adapter = new HomeGrid(MainActivity.this, title, icon, back); 
    grid = (GridView) findViewById(R.id.grid); 
    grid.setAdapter(adapter); 
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 

      switch (title[position]) { 
       case "Services": 
        Intent i1 = new Intent(MainActivity.this, Listing.class); 
        startActivity(i1); 
        break; 
       case "Solutions": 
        //Intent i2 = new Intent(MainActivity.this, Listing.class); 
        //startActivity(i2); 
        Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show(); 
        break; 
       case "Customers": 
        Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show(); 
        break; 
       case "Gallery": 
        Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show(); 
        break; 
       case "Events": 
        Intent i3 = new Intent(MainActivity.this, Form.class); 
        startActivity(i3); 
        break; 
       case "Contact Us": 
        Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show(); 
        break; 
     } 
     } 
    }); 
} 
+0

Was ist ein 'HomeGrid'? – njzk2

+0

'inflater.inflate (..., true)' -> 'inflater.inflate (..., false)' Sie möchten die Ansicht noch nicht an die Eltern anhängen. Das Raster kümmert sich darum. – njzk2

Antwort

0

Verwenden Sie dies.

public class ListAdapter extends ArrayAdapter<String>{ 
    private Activity context; 
    private int[] icon; 
    private String[] title; 
    private LayoutInflater inflater; 
    public ListAdapter(Activity context, int[] icon, String[] title) 
    { 
     super(context, R.layout.list_item, title); 
     this.context = context; 
     this.icon = icon; 
     this.title = title; 
     inflater = LayoutInflater.from(context); 
} 
public View getView(final int position, View view, final ViewGroup parent) 
{ 
    view= inflater.inflate(R.layout.list_item, parent, false); 
    try 
    { 
    TextView txtTitle = (TextView) view.findViewById(R.id.txtTitle); 
    ImageView imgIcon = (ImageView) view.findViewById(R.id.imgIcon); 
    txtTitle.setText(title[position]); 
    imgIcon.setBackgroundResource(icon[position]); 
    } 
    catch (Exception e) 
    { 
    e.printStackTrace(); 
    } 
finally 
{ 
    return view; 
    } 
} 
} 
+0

Danke Vaibhavi –

+0

Kannst du erklären, was du geändert hast, warum du denkst, dass das Problem war und wie du denkst, dass deine Lösung das Problem löst? – njzk2

+0

sogar ich konnte die Logik nicht verstehen, aber das hat mein Problem gelöst.Erklärung wird helfen, solche Probleme in Zukunft zu lösen. –

Verwandte Themen