为什么此自定义适配器不显示输出

时间:2015-09-04 14:27:47

标签: android

我是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>

3 个答案:

答案 0 :(得分:0)

在充气和填充列表项目布局后,您缺少convertView = gridView;。我发现做这样的事情更容易:

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

答案 1 :(得分:0)

试试这个;

以下更改;

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

要;

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

答案 2 :(得分: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;
}

您还应该改进方法:

@Override
public Object getItem(int position) {
    return clrValues[position];
}
@Override
public long getItemId(int position){
    return position;
}

查看自定义适配器http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown

的教程