我在holder.text.setText
(项目)时遇到错误。请给我一些建议。
package com.example.dipen.custom_listview;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
GridView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
`setContentView`(R.layout.activity_main);
list = (GridView) findViewById(R.id.gridView);
list.setAdapter(new customadapter(this));
}
class singlerow{
String name;
int image;
singlerow(String name,int image){
this.name=name;
this.image=image;
}
}
class customadapter extends BaseAdapter{
ArrayList<singlerow> list;
Context context;
customadapter(Context c){
context=c;
list=new ArrayList<singlerow>();
Resources res=c.getResources();
String[] str=res.getStringArray(R.array.name);
int[] imag={R.drawable.autocomplete_java,R.drawable.autocomplete_output,R.drawable.autocomplete_xml};
for(int i=0;i<3;i++){
list.add(new singlerow(str[i],imag[i]));
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
class viewholder{
TextView text;
ImageView image;
viewholder(View view){
text= (TextView) view.findViewById(R.id.textView);
image=(ImageView)view.findViewById(R.id.imageView);
}
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View row=view;
viewholder holder;
if(row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_row, viewGroup, false);
holder=new viewholder(row);
}
else{
holder=(viewholder)row.getTag();
}
TextView text= (TextView) row.findViewById(R.id.textView);
ImageView image=(ImageView)row.findViewById(R.id.imageView);
singlerow temp=list.get(i);
holder.text.setText(temp.name);
holder.text.setTag(temp);
holder.image.setImageResource(temp.image);
holder.image.setTag(temp);
holder.image.setTag(temp);
if (i % 2 == 1) {
row.setBackgroundColor(Color.YELLOW);
} else {
row.setBackgroundColor(Color.CYAN);
}
return row;
}
}
}