2015-09-04 72 views
0

我是Android新手。我嘗試過一個自定義適配器,但輸出不可見。顯然沒有錯誤。謹慎提及我在做錯的地方。爲什麼這個自定義適配器不顯示輸出

這裏是代碼

package com.example.customadapter; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.GridView; 
import android.widget.Toast; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

GridView gridview; 
static final String[] Box_Clrs=new String[]{ 
     "pink" , "red" , "blue" 
}; 


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

    gridview= (GridView) findViewById(R.id.grid_view); 
    gridview.setAdapter(new ImageAdapter(this, Box_Clrs)); 
    gridview.setOnItemClickListener(new OnItemClickListener() 
      { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id){ 
      Toast.makeText(
        getApplicationContext(),((TextView) v.findViewById(R.id.grid_text)) 
         .getText(), Toast.LENGTH_SHORT).show(); 



     } 
      }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

ImageAdapter.java

package com.example.customadapter; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

    public class ImageAdapter extends BaseAdapter { 

Context context; 
final String[] clrValues; 

public ImageAdapter(Context context, String[] clrValues) 
{ 
    this.context=context; 
    this.clrValues=clrValues; 
} 
    public View getView(int position, View convertView, ViewGroup parent){ 

    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View gridView; 
     if(convertView==null) 
     { 
      gridView = new View(context); 
      gridView = inflater.inflate(R.layout.mobile,null); 

      TextView textview=(TextView) gridView.findViewById(R.id.grid_text); 

      textview.setText(clrValues[position]); 

      ImageView imageview=(ImageView) gridView.findViewById(R.id.image); 

      String clr= clrValues[position]; 
      if(clr.equals("pink")) 
       imageview.setImageResource(R.drawable.pink); 
      else if(clr.equals("red")) 
       imageview.setImageResource(R.drawable.red); 
      else 
       imageview.setImageResource(R.drawable.blue); 

     } 

      else 
      { 
      gridView=(View) convertView; 
      } 



    return convertView; 


} 
    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return clrValues.length; 
    } 
    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public long getItemId(int position){ 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    } 

activity_main.xml中

<GridView 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:id="@+id/grid_view" 
android:gravity="center" 
android:columnWidth="100dp" 
android:stretchMode="columnWidth" 
android:numColumns="auto_fit" 
tools:context="com.example.customadapter.MainActivity" > 

mobile.xml

<LinearLayout 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="5dp" 
tools:context="com.example.customadapter.MainActivity" > 

<ImageView 
    android:id="@+id/image" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_marginRight="10dp" 
    android:src="@drawable/blue"> 
    </ImageView> 

<TextView 
    android:id="@+id/grid_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp"> 

</TextView> 

+0

它看起來像你想'返回gridView'而不是'convertView' – codeMagic

+0

謝謝。現在工作 – user5300251

回答

0

你膨脹和填充您的列表項目佈局後失蹤convertView = gridView;。我覺得更容易做這樣的事情:

if (convertView == null) { 
    convertView = layoutInflater.inflate(R.layout.list_item, parent, false); 
} 
// do your setup here 
return convertView; 
0

試試這個;

改變如下;

View gridView; 
if(convertView==null) 
{ 
gridView = new View(context); 
gridView = inflater.inflate(R.layout.mobile,null); 
………… 
} 

To;

View gridView = convertView; 
if(gridView==null) 
{ 
gridView = inflater.inflate(R.layout.mobile,null); 
……………… 
} 
0

getView方法應該返回網格視圖,而不是GridView控件本身的項目 嘗試使用這樣的:

public View getView(int position, View convertView, ViewGroup parent){ 
    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) 
    View view 
    if(convertView==null) 
    { 
     view = inflater.inflate(R.layout.mobile,parent, false); 
    } else { 
     view = convertView; 
    } 

    TextView textview=(TextView) view.findViewById(R.id.grid_text); 
    textview.setText(clrValues[position]); 

    ImageView imageview=(ImageView) view.findViewById(R.id.image); 

    String clr= clrValues[position]; 
    if(clr.equals("pink")) 
     imageview.setImageResource(R.drawable.pink); 
    else if(clr.equals("red")) 
     imageview.setImageResource(R.drawable.red); 
    else 
     imageview.setImageResource(R.drawable.blue); 
    return view; 
} 

你也應該改善方法:

​​

看到一個定製適配器教程http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown

相關問題