代码工作正常,直到我上下两次滚动gridview。当我开始上下滚动时,gridview的内容开始从顶部消失,逐渐变为空白。我知道这是一个循环问题但是我的头脑中只有零编程知识,我无法解决这个问题。我在这里搜索了答案,但我没有发现我的代码问题。这就是为什么我决定问问自己。
我已经在下面给出了代码,我希望有人来找出我的问题。
主要课程
public class NEWS extends AppCompatActivity {
GridView gridView;
static final String[] MOBILE_OS = new String[] { "Mathrubhumi", "Malayala Manorama","Madhyamam","Deshabhimani", "One India","Marunadan Malayali","Janayugom",
"Janmabhumi","Kvartha","Bignews Live"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new adapter_news(this, MOBILE_OS));
}
适配器类
public class adapter_news extends BaseAdapter {
private ImageView flag;
private Context context;
private final String[] countries;
public adapter_news(Context context, String[] countries) {
this.context = context;
this.countries = countries;
}
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.adapter_news, null);
TextView textView = (TextView) gridView.findViewById(R.id.label);
textView.setText(countries[position]);
flag = (ImageView) gridView.findViewById(R.id.flag);
String mobile = countries[position];
if (mobile.equals("Mathrubhumi")) {
flag.setImageResource(R.drawable.mathrubhumi);
flag.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent("");
v.getContext().startActivity(intent);
}
}
);
} else if (mobile.equals("Malayala Manorama")) {
flag.setImageResource(R.drawable.malayala_manorama);
flag.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("");
v.getContext().startActivity(intent);
}
}
);
} else if (mobile.equals("Madhyamam")) {
flag.setImageResource(R.drawable.madhyamam);
flag.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("");
v.getContext().startActivity(intent);
}
}
);
} else if (mobile.equals("Deshabhimani")){
flag.setImageResource(R.drawable.deshabhimani);
flag.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("");
v.getContext().startActivity(intent);
}
}
);
} else if (mobile.equals("One India")){
flag.setImageResource(R.drawable.oneindia);
flag.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("");
v.getContext().startActivity(intent);
}
}
);
} else if (mobile.equals("Marunadan Malayali")){
flag.setImageResource(R.drawable.marunadan);
flag.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("");
v.getContext().startActivity(intent);
}
}
);
} else if (mobile.equals("Janayugom")){
flag.setImageResource(R.drawable.janayugom);
flag.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("");
v.getContext().startActivity(intent);
}
}
);
} else if (mobile.equals("Janmabhumi")){
flag.setImageResource(R.drawable.janmabhumi);
flag.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("");
v.getContext().startActivity(intent);
}
}
);
} else if (mobile.equals("Kvartha")){
flag.setImageResource(R.drawable.kvartha);
flag.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("");
v.getContext().startActivity(intent);
}
}
);
} else {
flag.setImageResource(R.drawable.bignews);
flag.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("");
v.getContext().startActivity(intent);
}
}
);
}
} else {
gridView = convertView;
}
return gridView;
}
@Override
public int getCount() {
return countries.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
主要xml
<GridView
android:id="@+id/gridView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:numColumns="auto_fit"
android:columnWidth="140dp"
android:stretchMode="columnWidth"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</GridView>
适配器xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="center">
<ImageView
android:id="@+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="@+id/label"
android:textSize="15sp" >
</TextView>
答案 0 :(得分:1)
这是该方法的描述:
/**
* Get the row id associated with the specified position in the list.
*
* @param position The position of the item within the adapter's data set whose row id we want.
* @return The id of the item at the specified position.
*/
@Override
public long getItemId(int position) {
return position; // return 0 here means All items are the same;
}
基于此,从这里返回0可能会导致listView混淆。你可能永远不会这样做。 return 0
语句由IDE生成,与方法的逻辑无关。