在我的Android应用中,当我点击ListView
中的某个项目时,它应该打开活动FixtureDescription
,每个项目应该打开相同的活动但是每个新项目的空白开始。这是我的.java文件,目前列表是从AlertDialog
正确填充的,但是当它被点击时,它不会从它所在的活动中移出。有什么建议吗?
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import static android.widget.AdapterView.*;
public class RoomDescription extends ActionBarActivity {
public AlertDialog.Builder dialogBuilder;
public ArrayList<String> myFixtures = new ArrayList<String>();
public String listItem;
private void setFixtureName()
{
dialogBuilder = new AlertDialog.Builder(this);
final EditText txtInput = new EditText(this);
dialogBuilder.setTitle("Fixture Name");
dialogBuilder.setMessage("What is the fixture name?");
dialogBuilder.setView(txtInput);
dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
myFixtures.add(txtInput.getText().toString());
populateListView();
Toast toast = Toast.makeText(getApplicationContext(), "Fixture has been added.", Toast.LENGTH_SHORT);
toast.show();
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
AlertDialog dialogFixtureName = dialogBuilder.create();
dialogFixtureName.show();
}
public void onClick(View v)
{
setFixtureName();
}
public void onItemClick()
{
ListView list = (ListView) findViewById(R.id.FixtureList);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
switch (position)
{
default:Intent i = new Intent(RoomDescription.this, FixtureDescription.class);
startActivity(i);
break;
}
}
});
}
private void populateListView()
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.items, myFixtures);
ListView list = (ListView) findViewById(R.id.FixtureList);
list.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room_description);
populateListView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_room_description, 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);
}
}
答案 0 :(得分:0)
您已经介绍了一种从未使用过的方法:onItemClick()
。通过使用一个好的IDE - 我完全鼓励您使用Android Studio - 您将在编辑器中看到这样的警告。
OnItemClickListener应在onCreate()
中创建视图时注册。
(同样,活动&#39; onClick()
永远不会被执行。)
答案 1 :(得分:0)
您的OnItemClickListener
位于您未调用的方法中 - 它不会被Android自动调用,也不会自动调用。
您应该在OnItemClickListener
的{{1}}中添加onCreate()
的代码,以便初始化点击监听器,然后才能对项目点击执行操作。
要明确的是,Activity
方法中的内容是初始化侦听器,以便它开始侦听单击。由于您希望它始终“监听”项目点击,因此您应将代码移至onItemClick()
。