Java Listview/arrayadapter事件错误
我正在android studio中进行一个项目,该项目有一个带有阵列适配器的列表。如果我单击一个列表项,您必须发送到车间页面,该页面显示车间的所有信息。我使用了一个SetonicClick监听器,但不知为什么它不起作用。它抛出了这个错误 尝试在com.example.jmuts.tikcitv2.scedule$1.onItemClick(scedule.java:68)* 我不知道如何解决这个问题,所以我现在陷入困境。我希望有人能帮助我 Schedule.java(这是包含单击事件的代码)Java Listview/arrayadapter事件错误,java,android,listview,onclicklistener,Java,Android,Listview,Onclicklistener,我正在android studio中进行一个项目,该项目有一个带有阵列适配器的列表。如果我单击一个列表项,您必须发送到车间页面,该页面显示车间的所有信息。我使用了一个SetonicClick监听器,但不知为什么它不起作用。它抛出了这个错误 尝试在com.example.jmuts.tikcitv2.scedule$1.onItemClick(scedule.java:68)* 我不知道如何解决这个问题,所以我现在陷入困境。我希望有人能帮助我 Schedule.java(这是包含单击事件的代码)
公共类scedule扩展AppCompative实现NavigationView.OnNavigationItemSelectedListener{
ListView lv;
阵列适配器;
字符串[]名称、开始、结束;
字符串eventName;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
ActionBarDrawerToggle切换=新建ActionBarDrawerToggle(
这,抽屉,工具栏,R.string.navigation\u drawer\u open,R.string.navigation\u drawer\u close);
抽屉。添加抽屉链接器(切换);
toggle.syncState();
NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图);
navigationView.setNavigationItemSelectedListener(此);
lv=findViewById(R.id.listView);
Bundle Bundle=getIntent().getExtras();
if(bundle!=null){
eventName=bundle.getString(“eventName”);
}
listner(事件名称);
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
意向意向=新意向(scedule.this,workshop.class);
intent.putExtra(“eventname”,lv.getItemAtPosition(position.toString());
星触觉(意向);
}
});
}
@凌驾
public void onBackPressed(){
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
if(抽屉isDrawerOpen(重力压缩机启动)){
抽屉。关闭抽屉(重力压缩机启动);
}否则{
super.onBackPressed();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.home,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@SuppressWarnings(“StatementWithEmptyBody”)
@凌驾
公共布尔值onNavigationItemSelected(MenuItem项){
//处理导航视图项单击此处。
int id=item.getItemId();
如果(id==R.id.nav_变化){
意图c=新意图(scedule.this,MainActivity.class);
c、 setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
星触觉(c);
完成();
}else if(id==R.id.nav_home){
完成();
}else if(id==R.id.nav_时间表){
//意向意向=新意向(scedule.this,scedule.class);
//intent.putExtra(“eventname”,eventname);
//星触觉(意向);
}else if(id==R.id.nav_map){
意向意向=新意向(scedule.this,map.class);
intent.putExtra(“eventname”,eventname);
星触觉(意向);
完成();
}else if(id==R.id.nav_常见问题){
意图=新意图(scedule.this,faq.class);
intent.putExtra(“eventname”,eventname);
星触觉(意向);
完成();
}
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
抽屉。关闭抽屉(重力压缩机启动);
返回true;
}
私有void列表器(字符串项){
字符串getData=null;
字符串dbResult=“empty”;
dbConnect数据库=新的dbConnect(此);
试一试{
日期c=Calendar.getInstance().getTime();
System.out.println(“当前时间=>”+c);
SimpleDataFormat df=新SimpleDataFormat(“yyyy-MM-dd”);
字符串formattedDate=df.format(c);
String query=“从事件中选择c.workshopname、b.starttime、b.endtime作为内部联接,在a.eventid=b.eventid中选择b.eventid作为内部联接,在b.workshopid=c.workshopid中选择c.workshopid,其中b.workshopid=c.workshopid和b.eventid=(从事件中选择eventid,其中eventname='“+item+”)按b.starttime排序;”;
getData=“?query=“+urlcoder.encode(查询,“UTF-8”);
//数据+=”&username=“+urlcoder.encode(用户名,“UTF-8”);
字符串链接=”https://jackstest.000webhostapp.com/androiddbconnect.php“+getData;
dbResult=database.execute(link.get();
}
捕获(例外e){
e、 printStackTrace();
}
试一试{
JSONArray ja=新的JSONArray(dbResult);
JSONObject jo=null;
name=新字符串[ja.length()];
开始=新字符串[ja.length()];
end=新字符串[ja.length()];
对于(int i=0;ipublic class scedule extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
ListView lv;
ArrayAdapter<String> adapter;
String[] name, beginning, end;
String eventName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scedule);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
lv = findViewById(R.id.listView);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
eventName = bundle.getString("eventname");
}
listner(eventName);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(scedule.this, workshop.class);
intent.putExtra("eventname", lv.getItemAtPosition(position).toString());
startActivity(intent);
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Change) {
Intent c = new Intent(scedule.this, MainActivity.class);
c.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(c);
finish();
} else if (id == R.id.nav_home) {
finish();
} else if (id == R.id.nav_scedule) {
//Intent intent = new Intent(scedule.this, scedule.class);
//intent.putExtra("eventname", eventName);
//startActivity(intent);
} else if (id == R.id.nav_map) {
Intent intent = new Intent(scedule.this, map.class);
intent.putExtra("eventname", eventName);
startActivity(intent);
finish();
} else if (id == R.id.nav_faq) {
Intent intent = new Intent(scedule.this, faq.class);
intent.putExtra("eventname", eventName);
startActivity(intent);
finish();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void listner(String item){
String getData = null;
String dbResult = "empty";
dbConnect database = new dbConnect(this);
try {
Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = df.format(c);
String query = "SELECT c.workshopname, b.starttime, b.endtime FROM events as a inner join scedules as b on a.eventid = b.eventid inner join workshops as c on b.workshopid = c.workshopid where b.workshopid = c.workshopid and b.eventid = (select eventid from events where eventname = '" + item + "') order by b.starttime;";
getData = "?query=" + URLEncoder.encode(query, "UTF-8");
//data += "&username=" + URLEncoder.encode(userName, "UTF-8");
String link = "https://jackstest.000webhostapp.com/androiddbconnect.php" + getData;
dbResult = database.execute(link).get();
}
catch (Exception e){
e.printStackTrace();
}
try{
JSONArray ja = new JSONArray(dbResult);
JSONObject jo = null;
name = new String[ja.length()];
beginning = new String[ja.length()];
end = new String[ja.length()];
for(int i = 0; i < ja.length(); i++){
jo = ja.getJSONObject(i);
name[i] = jo.getString("workshopname");
beginning[i] = "Start: " + jo.getString("starttime");
end[i] = "end: " + jo.getString("endtime");
}
lv.setAdapter(new dataListAdapter(name, beginning, end));
}
catch (Exception e){
e.printStackTrace();
}
}
class dataListAdapter extends BaseAdapter {
String[] name, beginning, end;
dataListAdapter() {
name = null;
beginning = null;
end = null;
}
public dataListAdapter(String[] text, String[] text1,String[] text3) {
name = text;
beginning = text1;
end = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return name.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.list_view_scedule_layout, parent, false);
TextView t1, t2, t3;
t1 = (TextView) row.findViewById(R.id.list_item1);
t2 = (TextView) row.findViewById(R.id.list_item2);
t3 = (TextView) row.findViewById(R.id.list_item3);
t1.setText(name[position]);
t2.setText(beginning[position]);
t3.setText(end[position]);
return (row);
}
}
public class workshop extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
String eventName;
TextView Header;
TextView Content;
TextView Time;
TextView Date;
TextView Location;
ImageView View;
ArrayAdapter<String> adapter;
String[] data;
String [] image;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workshop);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Header = (TextView) findViewById(R.id.header);
Content = (TextView) findViewById(R.id.content);
Time = (TextView) findViewById(R.id.start_end_time);
Date = (TextView) findViewById(R.id.date);
Location = (TextView) findViewById(R.id.location);
View = (ImageView) findViewById(R.id.imageView3);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
eventName = bundle.getString("eventname");
}
GetInfo(eventName);
}
private void GetInfo(String eventName) {
String GetInfo = null;
String dbResult = "empty";
dbConnect database = new dbConnect(this);
try{
String query = "";
GetInfo = "?query=" + URLEncoder.encode(query, "UTF-8");
String link = "https://jackstest.000webhostapp.com/androiddbconnect.php" + GetInfo;
dbResult = database.execute(link).get();
}
catch(Exception e){
Header.setText("Workshop");
}
try{
JSONArray ja = new JSONArray(dbResult);
JSONObject jo = null;
data = new String[ja.length()];
image = new String[ja.length()];
for (int i = 0;i < 1 ; i++){
jo = ja.getJSONObject(i);
data[i] = jo.getString("workshopname");
image[i] = jo.getString("imagelink");
}
bitmap = getBitmapFromURL(image[0]);
View.setImageBitmap(bitmap);
Header.setText(data[0]);
}
catch(Exception e){
e.printStackTrace();
}
}
private Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_Change) {
Intent c = new Intent(workshop.this, MainActivity.class);
c.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(c);
finish();
} else if (id == R.id.nav_home) {
finish();
} else if (id == R.id.nav_scedule) {
Intent intent = new Intent(workshop.this, scedule.class);
intent.putExtra("eventname", eventName);
startActivity(intent);
finish();
} else if (id == R.id.nav_map) {
Intent intent = new Intent(workshop.this, map.class);
intent.putExtra("eventname", eventName);
startActivity(intent);
finish();
} else if (id == R.id.nav_faq) {
Intent intent = new Intent(workshop.this, faq.class);
intent.putExtra("eventname", eventName);
startActivity(intent);
finish();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return myDataObjectArray[arg0];
}