我正在使用一個抽屜佈局,它內部有一個ListView和RelativeLayout,用於在我的應用程序上的漢堡菜單。當點擊ListView上的項目時,我想更改爲新的活動。但是,當我單擊ListView上的項目時,什麼都沒有發生。我的代碼:ListView setOnItemClickListener沒有響應
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="b.calvin.com.dirtymoney.MainActivity"
android:focusable="false"
android:focusableInTouchMode="false">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hourlyRate"
android:layout_toRightOf="@+id/enterHourlyRate"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Hourly Rate:"
android:id="@+id/enterHourlyRate"
android:layout_centerVertical="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Make It Rain"
android:id="@+id/start"
android:layout_below="@+id/hourlyRate"
android:layout_toLeftOf="@+id/stop"
android:layout_toStartOf="@+id/stop"
android:layout_marginRight="37dp"
android:layout_marginEnd="37dp"
android:layout_marginTop="77dp"
android:visibility="invisible"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/stop"
android:layout_alignBottom="@+id/start"
android:layout_alignRight="@+id/hourlyRate"
android:layout_alignEnd="@+id/hourlyRate"
android:layout_marginRight="42dp"
android:layout_marginEnd="42dp"
android:layout_alignTop="@+id/start"
android:visibility="invisible"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="@+id/confirmHourlyRate"
android:layout_below="@+id/hourlyRate"
android:layout_alignParentRight="true"/>
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chronometer"
android:layout_above="@+id/stop"
android:layout_alignLeft="@+id/start"
android:layout_alignStart="@+id/start" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="$$ Money"
android:id="@+id/money"
android:layout_below="@+id/start"
android:layout_toLeftOf="@+id/stop"
android:layout_toStartOf="@+id/stop" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:id="@+id/edit"
android:layout_below="@+id/hourlyRate"
android:layout_toLeftOf="@+id/confirmHourlyRate"
android:layout_toStartOf="@+id/confirmHourlyRate"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day: $"
android:id="@+id/dayText"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/edit"
android:layout_alignStart="@+id/edit" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Week: $"
android:id="@+id/weekText"
android:layout_below="@+id/dayText"
android:layout_alignLeft="@+id/dayText"
android:layout_alignStart="@+id/dayText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Month: $"
android:id="@+id/monthText"
android:layout_below="@+id/weekText"
android:layout_alignLeft="@+id/weekText"
android:layout_alignStart="@+id/weekText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Year: $"
android:id="@+id/yearText"
android:layout_below="@+id/monthText"
android:layout_alignLeft="@+id/monthText"
android:layout_alignStart="@+id/monthText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total: $"
android:id="@+id/totalText"
android:layout_below="@+id/yearText"
android:layout_alignLeft="@+id/yearText"
android:layout_alignStart="@+id/yearText"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/dayMoney"
android:layout_toRightOf="@+id/dayText"
android:layout_alignTop="@+id/dayText"
android:layout_alignBottom="@+id/dayText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/weekMoney"
android:layout_toRightOf="@+id/weekText"
android:layout_alignTop="@+id/weekText"
android:layout_alignBottom="@+id/weekText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/monthMoney"
android:layout_toRightOf="@+id/monthText"
android:layout_alignTop="@+id/monthText"
android:layout_alignBottom="@+id/monthText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/yearMoney"
android:layout_toRightOf="@+id/yearText"
android:layout_alignTop="@+id/yearText"
android:layout_alignBottom="@+id/yearText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/totalMoney"
android:layout_toRightOf="@+id/totalText"
android:layout_alignTop="@+id/totalText"
android:layout_alignBottom="@+id/totalText"/>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "BroadcastTest";
private Intent intent;
private Button start;
private Button stop;
private Button confirmRate;
private Button edit;
private EditText hourlyRate;
private TextView money;
private TextView dayMoney;
private TextView weekMoney;
private TextView monthMoney;
private TextView yearMoney;
private TextView totalMoney;
private double moneyEarned;
private double secondsRate;
private double hrlyRate;
private double currentDayMoney;
private double currentWeekMoney;
private double currentMonthMoney;
private double currentYearMoney;
private double currentTotalMoney;
private String savedDayMoney;
private String savedWeekMoney;
private String savedMonthMoney;
private String savedYearMoney;
private String savedTotalMoney;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private String mActivityTitle;
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView)findViewById(R.id.navList);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
confirmRate = (Button) findViewById(R.id.confirmHourlyRate);
edit = (Button) findViewById(R.id.edit);
hourlyRate = (EditText) findViewById(R.id.hourlyRate);
money = (TextView) findViewById(R.id.money);
dayMoney = (TextView) findViewById(R.id.dayMoney);
weekMoney = (TextView) findViewById(R.id.weekMoney);
monthMoney = (TextView) findViewById(R.id.monthMoney);
yearMoney = (TextView) findViewById(R.id.yearMoney);
totalMoney = (TextView) findViewById(R.id.totalMoney);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private void addDrawerItems() {
String[] osArray = { "New Shift", "Poop Slips", "Statistics", "Help"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return true;
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Menu");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
您是否嘗試過在Toast上測試自定義字符串?示例:Toast.makeText(getBaseContext(),「Testing List item click」,Toast.LENGTH_LONG).show(); –
@ Md.NasirUddinBayyan是的,它沒有工作 – BigFatToe