我正在学习为android和java编程一般,需要一些关于android的“导航抽屉”的帮助。
我正在努力为抽屉项目的点击监听器添加一个switch语句。我正在使用的代码来自这里的示例:http://hmkcode.com/android-creating-a-navigation-drawer/
我应该如何处理switch语句,以便通过触摸其中一个项目来启动新活动?
谢谢
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(drawerListView);
}
}
编辑...
public void onItemClick(AdapterView parent,View view,int position,long id){
switch (position){
case 0:
new DataTask(this).execute();
MainActivity.this.finish();//Set this Activity to Finish so no loop back
Intent intent=new Intent(MainActivity.this,SplashScreen.class);
startActivity(intent);
System.out.println("Click working");
case 1:
//do stuff
default:
break;
}
新DataTask(this).execute();
正在发出此警告....构造函数DataTask(MainActivity.DrawerItemClickListener)未定义。我不确定为什么?
DataTask Class ...
public class DataTask extends AsyncTask<Void, Void, Integer> {
Context context;
DataTask(Context context) {
this.context = context.getApplicationContext();
}
// Global Int for counting how many Tasks have been completed
int asynCount = 0;
ArrayList<String> arr_dataVts=new ArrayList<String>();
ArrayList<String> arr_dataNtm=new ArrayList<String>();
ArrayList<String> arr_dataOdas=new ArrayList<String>();
ArrayList<String> arr_dataMetAll=new ArrayList<String>();
ArrayList<String> arr_dataMet3HrTask=new ArrayList<String>();
ArrayList<String> arr_dataTideTask=new ArrayList<String>();
@Override
protected Integer doInBackground(Void... params) {
//VtsAsyncTask
VtsTask task1 = new VtsTask();
task1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//NtmAsyncTask
NtmTask task2 = new NtmTask();
task2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//OdasAsyncTask
OdasTask task3 = new OdasTask();
task3.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//MetAllTask
MetAllTask task4 = new MetAllTask();
task4.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//Met3HrTask
Met3HrTask task5 = new Met3HrTask();
task5.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
//TideTask
TideTask task6 = new TideTask();
task6.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return 1;
}
private class VtsTask extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(Void... params) {
Document docVTS;
try {
Connection.Response response = Jsoup.connect("https://vts.mhpa.co.uk/main_movelistb.asp")
.timeout(10000)
.ignoreHttpErrors(true)
.execute();
int statusCode = response.statusCode();
if(statusCode == 200) {
docVTS = Jsoup.connect("https://vts.mhpa.co.uk/main_movelistb.asp").timeout(10000).get();
Elements tableRows = docVTS.select("table.dynlist td:eq(0),td:eq(1),td:eq(3),td:eq(4),td:eq(7),td:eq(8)");
tableRows.size();
for(int i = 1; i < 80; i++){// Only allows x results from VTS list, from 1 not 0. 0 produces needless results
String shippingList = tableRows.get(i).text() +"\n";//new line
arr_dataVts.add(shippingList);// Add value to ArrayList
};
} else {
//If can't connect for what ever reason
System.out.println("Received error code for VTS list Data : " + statusCode + " Adding Null values");
for(int i = 1; i < 80; i++){
arr_dataVts.add("No Data" + i);
}
}
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Received timeout error code for VTS list Data : Adding Null values ");
for(int i = 1; i < 80; i++){
arr_dataVts.add("No Data" + i);
}
}
return arr_dataVts;
}
@Override
protected void onPostExecute(ArrayList<String> Param) {
asynCount++;
System.out.println("Vts list Captured" + arr_dataVts + " asynCount= " + asynCount);
if (asynCount == 6){
//Start intents for main activity
System.out.println("asynCount has reached= " + asynCount + " so now starting MainActivity");
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putStringArrayListExtra("data1", arr_dataVts);
intent.putStringArrayListExtra("data2", arr_dataNtm);
intent.putStringArrayListExtra("data3", arr_dataOdas);
intent.putStringArrayListExtra("data4", arr_dataMetAll);
intent.putStringArrayListExtra("data5", arr_dataMet3HrTask);
intent.putStringArrayListExtra("data6", arr_dataTideTask);
context.startActivity(intent);
}else{
//update dialogue
}
}
}
private class NtmTask extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(Void... params) {
Document docNTM;
try {
Connection.Response response = Jsoup.connect("http://www.milfordfishdocks.com/notices-to-mariners/")
.timeout(10000)
.ignoreHttpErrors(true)
.execute();
int statusCode = response.statusCode();
if(statusCode == 200) {
docNTM = Jsoup.connect("http://www.milfordfishdocks.com/notices-to-mariners/").timeout(10000).get();
Elements elements = docNTM.select("div.news-item-left");
int NtmAmount = elements.size();
String NtmAmt = Integer.toString(NtmAmount);//convert the Int to a string for adding into array
arr_dataNtm.add(NtmAmt);
} else {
System.out.println("Received error code for NTM Data : " + statusCode + " Adding Null values");
arr_dataNtm.add("0");
}
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Received timeout error code for NTM Data : Adding Null values ");
arr_dataNtm.add("0");
}
return arr_dataNtm;
}
@Override
protected void onPostExecute(ArrayList<String> Param) {
asynCount++;
System.out.println("Ntm list Captured" + arr_dataNtm + " asynCount= " + asynCount);
if (asynCount == 6){
//Start intents for main activity
System.out.println("asynCount has reached= " + asynCount + " so now starting MainActivity");
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putStringArrayListExtra("data1", arr_dataVts);
intent.putStringArrayListExtra("data2", arr_dataNtm);
intent.putStringArrayListExtra("data3", arr_dataOdas);
intent.putStringArrayListExtra("data4", arr_dataMetAll);
intent.putStringArrayListExtra("data5", arr_dataMet3HrTask);
intent.putStringArrayListExtra("data6", arr_dataTideTask);
context.startActivity(intent);
}else{
//update dialogue
}
}
}
@Override
protected void onPostExecute(Integer result) {
System.out.println("Data Task Has Executed");
}
}
答案 0 :(得分:0)
可以这样做:
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
switch (position){
case 0:
//do stuff
case 1:
//do stuff
default:
break;
}
drawerListView.setItemChecked(position, true);
drawerListView.setSelection(position);
drawerLayout.closeDrawer(drawerListView);
}
}
然后将此侦听器附加到您的NavList:
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
顺便说一句,你建议你切换片段而不是切换活动,"Creating a Navigation Drawer"教程解释了如何使用它们
编辑处理case 0
,替换为以下内容:
new DataTask(MainActivity.this).execute();
Intent intent=new Intent(MainActivity.this,SplashScreen.class);
startActivity(intent);
Log.d("Click working");
MainActivity.this.finish();//Set this Activity to Finish so no loop back
答案 1 :(得分:0)
Switch=
(Switch)navigationView.getMenu().findItem(R.id.vibrate).getActionView();
s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked){
if(isChecked)
//do whatever you want to do
}
});
这应该有效