为此,您首先需要一个自定义列表视图,您应该在 main.xml(只是一个 eg)文件中添加一个列表视图,然后创建一个像这样的类
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
DataHelper dh;
public MySimpleArrayAdapter(Context context, int textViewResourceId, String[] values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
dh=new DataHelper(getApplicationContext());
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_name, parent, false);
textView = (TextView) rowView.findViewById(R.id.textname);
textView.setText(values[position]);
// Change the icon for Windows and iPhone
textView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Toast.makeText(this,""+values[position],10000).show();
}
});
return rowView;
}
R.layout.list_name 这将是新的 xml 文件,它将内容加载到列表视图
并且在您的 on create 方法中的最后一步执行此操作
con = (ListView) findViewById(R.id.main_listView);
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(MainActivity.this, R.id.main_listView ,data);// data is String array valu to be added in list view
//setting the adapter
con.setAdapter(adapter);