gpt4 book ai didi

android - gridview元素的重复......!

转载 作者:行者123 更新时间:2023-11-29 01:30:39 24 4
gpt4 key购买 nike

代码可以正常工作,但问题在于,某些元素在 TextView 中重复出现,而另一些则被遗漏了。例如,“我们是谁”显示了两次。我也无法将 textview 元素居中。我已经发布了它的屏幕截图。我是 Android 编程的新手,任何人都可以帮助我。

这是主要 Activity

public class MainActivity extends Activity {

GridView grid;
public WebView webView;
public int pos;

String[] desc = { "Who We Are", "What We Do", "Entrepreneur", "Scholarship", "Admission",
"Internship", "Industrial Visit", "Project", "Buy or Sell Projects", "Free Training", "College Registration",
"Information", "Feedback", "Contact" };


int[] imageId = { R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
R.drawable.dummy, R.drawable.dummy, R.drawable.dummy,
R.drawable.dummy, R.drawable.dummy

};

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

GridDesign adapter = new GridDesign(MainActivity.this, desc, imageId);
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) {
pos = position;
Intent intent = new Intent(MainActivity.this, webView.class);
startActivity(intent);
}
});

}

}

这是自定义 GridView 类。

public class GridDesign extends BaseAdapter{
private Context mContext;
private final String[] desc;
private final int[] Imageid;

public GridDesign(Context c, String[] web, int[] Imageid) {
mContext = c;
this.Imageid = Imageid;
this.desc = web;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return desc.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;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {

grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_element, parent, false);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView) grid
.findViewById(R.id.grid_image);
textView.setText(desc[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}

return grid;
}
}

这是主要 Activity 布局

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity" >

<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dip"
android:gravity="center"
android:numColumns="2"
android:stretchMode="spacingWidthUniform" />

</LinearLayout>

这是单个网格元素布局

<?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" >

<ImageView
android:id="@+id/grid_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="@string/im" >
</ImageView>

<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="23"
android:gravity="center"
android:layout_marginTop="10sp"
android:textSize="12sp" >
</TextView>

</LinearLayout>

最佳答案

如果 convertView 不是 null,您的 getView 方法不会重置您的 View 的内容。如果 convertView 不是 null,您需要设置内容,否则它会保留旧 View 中的旧内容,您会看到重复的数据。如果 convertView 不是 null 这意味着您正在重用一个已经滚出屏幕的 View (这使得滚动更平滑,因为您不必重新分配和取消分配 View 当你滚动时。因此,正是这种重用意味着你会看到一些重复的元素和一些缺失的元素,因为在你的 getView 方法中,当 View 被重用时你没有重置内容。

要解决此问题,您可以将设置 View 内容的代码移到 if 语句之外,这样您就可以执行以下操作:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {

grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_element, parent, false);
} else {
grid = (View) convertView;
}

TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView) grid
.findViewById(R.id.grid_image);
textView.setText(desc[position]);
imageView.setImageResource(Imageid[position]);
return grid;
}

关于android - gridview元素的重复......!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31369638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com