在自定义Gridview适配器中,单击单个项目时显示不正确的值

时间:2014-02-20 11:00:12

标签: android gridview

我在应用程序中使用了自定义的Gridview适配器。它包含特定顺序的textview项。当我单击单个文本视图时,它始终显示一个常量值。代码如下

Main_activity.java

public class MainActivity extends Activity implements GridAdapter.ItemSelectedListener 
{
private TextView tv;
private GridView gv;
private GridAdapter ga;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.TextMsg);
    gv = (GridView)findViewById(R.id.gridView);
    ga = new GridAdapter(this);
    ga.setItemSelectedListener(this);
    gv.setAdapter(ga);


    GridAdapter.Item item = new GridAdapter.Item();
    item.set("Header1", "Item1_1", "Item1_2", "Item1_3");
    ga.add(item);

    item = new GridAdapter.Item();
    item.set("Header2", "Item2_1", "Item2_2", "Item2_3");
    ga.add(item);

    item = new GridAdapter.Item();
    item.set("Header3", "Item3_1", "Item3_2", "Item3_3");
    ga.add(item);
}


@Override
public void onItemSelected(String strItem) {
    Toast.makeText(this, strItem, Toast.LENGTH_LONG).show();        
}
}

GridAdapter.java

package com.example.testapplication;


import android.content.Context;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridAdapter extends ArrayAdapter<GridAdapter.Item> {
private LayoutInflater m_vi = null;
private GridAdapter.Item m_item = null;
private ViewHolderItem m_holderItem = null;
private ItemSelectedListener mItemSelectedListener = null;

public GridAdapter(Context context) {
    super(context, 0);
    m_vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    m_item = (GridAdapter.Item)getItem(position);
    if (convertView == null || !convertView.getTag().equals(m_holderItem)){
        convertView = m_vi.inflate(R.layout.mygrid, null);
        m_holderItem = new ViewHolderItem();
        convertView.setTag(m_holderItem);
    }
    else {
        m_holderItem = (ViewHolderItem) convertView.getTag();
    }

    m_holderItem.tvHeader = (TextView)convertView.findViewById(R.id.gridhead);
    m_holderItem.tvItem1 = (TextView)convertView.findViewById(R.id.gridtext1);
    m_holderItem.tvItem1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mItemSelectedListener != null )
                mItemSelectedListener.onItemSelected(m_holderItem.tvItem1.getText().toString());
        }
    });

    m_holderItem.tvItem2 = (TextView)convertView.findViewById(R.id.gridtext2);
    m_holderItem.tvItem2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mItemSelectedListener != null )
                mItemSelectedListener.onItemSelected(m_holderItem.tvItem2.getText().toString());
        }
    });

    m_holderItem.tvItem3 = (TextView)convertView.findViewById(R.id.gridtext3);
    m_holderItem.tvItem3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mItemSelectedListener != null )
                mItemSelectedListener.onItemSelected(m_holderItem.tvItem3.getText().toString());
        }
    });

    m_holderItem.tvHeader.setText(m_item.getHeader());
    m_holderItem.tvItem1.setText(m_item.getItem1());
    m_holderItem.tvItem2.setText(m_item.getItem2());
    m_holderItem.tvItem3.setText(m_item.getItem3());
    return convertView;
}

public static class ViewHolderItem {
    public TextView tvHeader;
    public TextView tvItem1;
    public TextView tvItem2;
    public TextView tvItem3;
}

public static class Item {
    public String Header, Item1, Item2, Item3;

    public Item(){
        this.Header = "";
        this.Item1 = "";
        this.Item2 = "";
        this.Item3 = "";
    }

    public void set(String Header_i, String Item1_i, String Item2_i, String Item3_i) {
        this.Header = Header_i;
        this.Item1 = Item1_i;
        this.Item2 = Item2_i;
        this.Item3 = Item3_i;
    }

    public String getHeader(){
        return this.Header;
    }

    public String getItem1(){
        return this.Item1;
    }

    public String getItem2(){
        return this.Item2;
    }

    public String getItem3(){
        return this.Item3;
    }
}

public interface ItemSelectedListener {
    public void onItemSelected(String strItem );
}

public void setItemSelectedListener( ItemSelectedListener _listener){
    mItemSelectedListener = _listener;
}

 }

enter image description here

上图显示了屏幕输出。当我点击item1_1或item2_1或item3_1时,它始终会显示item1_1。

如何解决这个问题?

提前致谢

0 个答案:

没有答案