0

我的活动包含一个 textview 并且在它之下它有 gridview 元素。阶段 1:活动开始时,它会按照我设置的顺序调整元素(如 item1、item2、..)stage1 图片第 2 阶段:但是当我单击 edittext 时,gridview 元素会被调整为为软键盘stage2 图片分配空间。第 3 阶段:所以当我按下后退按钮时,软键盘消失(像往常一样),但 gridview 元素的顺序正在改变stage3 图片。gridview 子项的顺序发生变化。谁能建议我如何避免订单中的这种不确定性?下面是我的整个代码和 xml 文件,尽管我认为这些对于解决这个问题并不是那么必要。

public class PresentActivity extends Activity {

GridView gridView;

    static final String[] TEXT = new String[] { 
        "item1", "item2","item3", "item4", "item5","item6","item7","item8" };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setGridView();        
    }


    public void setGridView()
    {
        gridView = (GridView)findViewById(R.id.gridView1);

        gridView.setAdapter(new ImageAdapter(this, TEXT));

        gridView.setNumColumns(3);

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                String currentText=(String) ((TextView)v.findViewById(R.id.grid_item_label)).getText();

    Toast.makeText(getApplicationContext(), currentText, Toast.LENGTH_SHORT).show();


            }
        });  

    }


}

这是我的自定义适配器类。

public class ImageAdapter extends BaseAdapter{

    private Context context;
    private final String[] textValues;

    public ImageAdapter(Context context, String[] textValues) {
        this.context = context;
        this.textValues = textValues;
    }

    @Override
    public int getCount() {

        return textValues.length;
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @Override
    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);

            // get layout from imagewithtext.xml
            gridView = inflater.inflate(R.layout.imagewithtext, null);

            // set value into textview
            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);
            textView.setText(textValues[position]);

            // set image based on selected text
            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);

            String extractedText = textValues[position];

            System.out.println("the position value is>>>>>>"+position);

            if (extractedText.equals("item1")) {
                imageView.setImageResource(R.drawable.icon);
            }
            else if (extractedText.equals("item2")) {
                imageView.setImageResource(R.drawable.icon);
            } else if (extractedText.equals("item3")) {
                imageView.setImageResource(R.drawable.icon);
            }else if(extractedText.equals("item4")){
                imageView.setImageResource(R.drawable.icon);
            } else if (extractedText.equals("item5")) {
                imageView.setImageResource(R.drawable.icon);
            } else if (extractedText.equals("item6")) {
                imageView.setImageResource(R.drawable.pic1);
            }
            else if(extractedText.equals("item7")){
                imageView.setImageResource(R.drawable.pic1);
            }
            else{
                imageView.setImageResource(R.drawable.pic1);
            }

        } else {
            gridView = (View) convertView;
        }

        return gridView;    


    }

}

包含gridview的xml文件:

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >


     <Button 
        android:id="@+id/button1"
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="@string/button"
        android:onClick="button1"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:text="@string/present_location"
      />



</RelativeLayout>

<EditText 
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/editText1_hint"
    />

<GridView
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</GridView>


</LinearLayout>
4

2 回答 2

1

假设您的项目仅包含 1 个 ImageView 和 1 个 TextView(为简单起见)。

您在适配器中的 getView 方法应该这样做:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder vh;
    if (convertView == null) {
        final LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.imagewithtext,null);
        //Custom ViewHolder, you have to create it in the same class.
        vh = new ViewHolder();
        vh.imageView = convertView.findViewById(R.id.imageView1);
        vh.textView = convertView.findViewById(R.id.textView1);
        convertView.setTag(vh);
    } else {
        vh = (ViewHolder) convertView.getTag();
    }
    vh.imageView.setImageResource(R.drawable.whatever);
    vh.textView.setText("Whatever text you want to set here");
    return gridView;    
}

在您的适配器中,只需添加以下内容:

private class ViewHolder{

    public ViewHolder(){}

    public ImageView imageView;
    public TextView textView;
    //Add any views you want to use in getView here
}
于 2012-12-04T10:28:54.687 回答
0

GridViewActivity:-(这是主要活动)

public class GridVieweActivity extends Activity {

  GridView gridView;

  static final String[] TEXT = new String[] { "item1", "item2", "item3",
      "item4", "item5", "item6", "item7", "item8" };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setGridView();
  }

  public void setGridView() {
    gridView = (GridView) findViewById(R.id.gridView1);

    gridView.setAdapter(new ImageAdapter(this, TEXT));

    gridView.setNumColumns(3);

    gridView.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View v,
          int position, long id) {

        String currentText = (String) ((TextView) v
            .findViewById(R.id.textView1)).getText();

        Toast.makeText(getApplicationContext(), currentText,
            Toast.LENGTH_SHORT).show();

      }
    });

  }

}

图像适配器:-

public class ImageAdapter extends BaseAdapter {

  private Context context;
  private final String[] textValues;

  public ImageAdapter(Context context, String[] textValues) {
    this.context = context;
    this.textValues = textValues;
  }

  @Override
  public int getCount() {

    return textValues.length;
  }

  @Override
  public Object getItem(int arg0) {

    return null;
  }

  @Override
  public long getItemId(int position) {

    return 0;
  }

  @Override
  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);

      // get layout from imagewithtext.xml
      gridView = inflater.inflate(R.layout.imagewithtext, null);

      // set value into textview
      TextView textView = (TextView) gridView
          .findViewById(R.id.textView1);
      textView.setText(textValues[position]);

      // set image based on selected text
      ImageView imageView = (ImageView) gridView
          .findViewById(R.id.imageView1);

      String extractedText = textValues[position];

      System.out.println("the position value is>>>>>>" + position);

      if (extractedText.equals("item1")) {
        imageView.setImageResource(R.drawable.ic_launcher);
      } else if (extractedText.equals("item2")) {
        imageView.setImageResource(R.drawable.ic_launcher);
      } else if (extractedText.equals("item3")) {
        imageView.setImageResource(R.drawable.ic_launcher);
      } else if (extractedText.equals("item4")) {
        imageView.setImageResource(R.drawable.ic_launcher);
      } else if (extractedText.equals("item5")) {
        imageView.setImageResource(R.drawable.ic_launcher);
      } else if (extractedText.equals("item6")) {
        imageView.setImageResource(R.drawable.ic_launcher);
      } else if (extractedText.equals("item7")) {
        imageView.setImageResource(R.drawable.ic_launcher);
      } else {
        imageView.setImageResource(R.drawable.ic_launcher);
      }

    } else {
      gridView = (View) convertView;
    }

    return gridView;

  }

}

main.xml:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="200px"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:onClick="button1"
            android:text="Button" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Present Location" />
    </RelativeLayout>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Hint" />

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="100dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>

</LinearLayout>

带文字的图片:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >
    </ImageView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="2dp"
        android:ellipsize="marquee"
        android:text="TextView"
        android:textSize="18sp" >
    </TextView>

</RelativeLayout>

上面的代码工作正常。

于 2012-12-04T11:22:09.443 回答