我已经定义了我的DatabaseHelper类并且可以查询数据库。我想将数据库中的3列(名称,地点,日期)填充到ListView
,其中包含3 TextViews
,不知怎的,我无法将CustomAdapter
设为正确。
这是错误:
CustomAdapter adapter = new CustomAdapter(this, myItems_name, myItems_venue, myItems_date);
请参阅以下完整代码:
public class ActivityLayout extends MainActivity implements AdapterView.OnItemClickListener {
List<String> myItems_name;
List<String> myItems_venue;
List<String> myItems_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
list = (ListView) findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter(this, myItems_name, myItems_venue, myItems_date);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textNameSingle);
Toast.makeText(this,tv.getText()+"", Toast.LENGTH_SHORT).show();
}
}
////////////////////////////////////////////////////////
class CustomAdapter extends ArrayAdapter<String> {
DatabaseHelper dbHeplper;
private Context context;
private List<String> myItems_name = dbHeplper.getDBData();
private List<String> myItems_venue = dbHeplper.getDBData();
private List<String> myItems_date = dbHeplper.getDBData();
public CustomAdapter(Context context, int resources, List<String> objects) {
super(context, resources, objects);
this.context=context;
this.myItems_name=objects;
this.myItems_venue=objects;
this.myItems_date=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row=convertView;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.my_layout_single, parent, false);
}
TextView txtArtist = (TextView) row.findViewById(R.id.textNameSingle);
TextView txtVenue = (TextView) row.findViewById(R.id.textVenueSingle);
TextView txtDate = (TextView) row.findViewById(R.id.textDateSingle);
txtArtist.setText(myItems_name[position]);
txtVenue.setText(myItems_venue[position]);
txtDate.setText(myItems_date[position]);
return row;
}
}
更新:#01
@ρяσѕρєяK - 我已更改我的活动和自定义适配器,如下所示:(问题仍然存在):
活动:
public class ActivityLayout extends MainActivity implements AdapterView.OnItemClickListener {
List<String> myItems_name;
List<String> myItems_venue;
List<String> myItems_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
list = (ListView) findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter(this, R.layout.my_layout, myItems_name, myItems_venue, myItems_date);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
适配器:
class CustomAdapter extends ArrayAdapter<String> {
DatabaseHelper dbHeplper;
private Context context;
private List<String> myItems_name = dbHeplper.getDBData();
private List<String> myItems_venue = dbHeplper.getDBData();
private List<String> myItems_date = dbHeplper.getDBData()
public CustomAdapter(Context context, int resources,
List<String> myItems_name,
List<String> myItems_venue,
List<String> myItems_date) {
super(context, resources, myItems_name);
this.context=context;
this.myItems_name=myItems_name;
this.myItems_venue=myItems_venue;
this.myItems_date=myItems_date;
}
答案 0 :(得分:1)
您需要初始化这些值:
List<String> myItems_name;
List<String> myItems_venue;
List<String> myItems_date;
答案 1 :(得分:0)
下面:
CustomAdapter adapter = new CustomAdapter(this,myItems_name, myItems_venue,myItems_date);
导致问题的行,因为CustomAdapter
中没有可用的构造函数,其中包含4个参数
向CustomAdapter类构造函数添加两个参数以获取myItems_name
和myItems_venue
:
public CustomAdapter(Context context, int resources,
List<String> myItems_name,
List<String> myItems_venue,
List<String> myItems_date) {
super(context, resources, myItems_name);
//.... your code here
}
还将想要用于ListView行的布局作为第二个参数传递给CustomAdapter
类构造函数:
CustomAdapter adapter = new CustomAdapter(this,R.layout.my_layout_single,
myItems_name, myItems_venue, myItems_date);
答案 2 :(得分:0)
@ρяσѕρєяK
感谢您的回答已被选中,错误现在不再存在。我补充道 dbHeplper = new DatabaseHelper(getApplicationContext());在onCreate下。先生,谢谢你的帮助。
@ρяσѕρєяK(见下面的更新代码) 更新代码:
public class ActivityLayout extends MainActivity implements AdapterView.OnItemClickListener {
DatabaseHelper dbHeplper;
ListView list;
List<String> myItems_name;
List<String> myItems_venue;
List<String> myItems_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
dbHeplper = new DatabaseHelper(getApplicationContext());
myItems_name = dbHeplper.getDBName();
myItems_venue = dbHeplper.getDBVenue();
myItems_date = dbHeplper.getDBDate();
list = (ListView) findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter(this, myItems_name, myItems_venue, myItems_date);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
}