2017-03-02 54 views
0

ListAdapter.javaAndroid的 - 在GridView的A卡項目重複兩次

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
在此卡活動被替換服務。當我重新洗牌時,更改會反映到應用程序中,但是當我再次運行應用程序時,Gallery將被服務取代。

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

什麼是「HomeGrid」? – njzk2

+0

'inflater.inflate(...,true)' - >'inflater.inflate(...,false)'您不想將視圖附加到父對象上。網格負責這一點。 – njzk2

回答

0

使用此。

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

謝謝Vaibhavi –

+0

你能解釋一下你改變了什麼,爲什麼你認爲問題出在你以及你認爲你的解決方案如何解決問題? – njzk2

+0

即使我不明白的邏輯,但這解決了我的問題。說明將有助於在未來解決這些問題。 –