您好,我试图让我的listview为每个部分打开一个不同的活动。截至目前,我可以打开一个活动,但他们都打开相同的活动。我需要知道改变它,例如:
item1将打开activity1 item2将打开activity2,依此类推。
我们非常感谢任何帮助,这是我的代码(有些评论是我接下来的教程,对我的经验不好意见):
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
//get listview from xml
listView = (ListView)findViewById(R.id.listView);
//defined array to show in listview
String[] values = new String[]{"About the App",
"How to Use",
"Build log",
};
//define a new context
//First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
//Assign adapter to listview
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*//listview clicked item index
int itemPosition = position;
//Listview clicked item value
String itemValue = (String)listView.getItemAtPosition(position);
//show alert
Toast.makeText(getApplicationContext(),
"Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/
Intent appInfo = new Intent(About.this, about_app.class);
startActivity(appInfo);
}
});
}
答案 0 :(得分:3)
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*//listview clicked item index
int itemPosition = position;
//Listview clicked item value
String itemValue = (String)listView.getItemAtPosition(position);
//show alert
Toast.makeText(getApplicationContext(),
"Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/
switch(itemPosition)
case 0 :Intent appInfo = new Intent(About.this, about_app.class);
startActivity(appInfo);
break;
case 1 :Intent appInfo = new Intent(About.this, Activity1.class);
startActivity(appInfo);
break;
case 2 :Intent appInfo = new Intent(About.this, Activity2.class);
startActivity(appInfo);
break;
}
});
答案 1 :(得分:1)
为此你必须提到:
if (position == 1) {
Intent appInfo = new Intent(About.this, about_app.class);
startActivity(appInfo);
}
else if (position == 2) {
Intent appInfo1 = new Intent(About.this, Test.class);
startActivity(appInfo1);
}
答案 2 :(得分:1)
检查一下。
public interface IAction {
public abstract void doAction();
public abstract String getCaption();
}
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
//get listview from xml
listView = (ListView)findViewById(R.id.listView);
//defined array to show in listview
final ArrayList<String> values = new ArrayList<String>();
//define a new context
//First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
final ArrayList<IAction> actions = new ArrayList<IAction>();
prepareActions(actions);
for (int i = 0; i < actions.size(); i++)
values.add(actions.get(i).getCaption());
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
//Assign adapter to listview
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*//listview clicked item index
int itemPosition = position;
//Listview clicked item value
String itemValue = (String)listView.getItemAtPosition(position);
//show alert
Toast.makeText(getApplicationContext(),
"Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/
actions.get(position).doAction();
}
});
}
private void prepareActions(final ArrayList<IAction> actions) {
actions.add(new IAction() {
@Override
public void doAction() {
startActivity(new Intent(About.this, about_app.class));
}
@Override
public String getCaption() {
return "About the App";
}
});
actions.add(new IAction() {
@Override
public void doAction() {
startActivity(new Intent(About.this, HowtoUse.class));
}
@Override
public String getCaption() {
return "How to Use";
}
});
actions.add(new IAction() {
@Override
public void doAction() {
if (NeedToBuildLog)
startActivity(new Intent(About.this, Buildlog.class));
/*else
Toast.makeText(getApplicationContext(),
"Position:"+itemPosition+" ListItem:" + itemValue, Toast.LENGTH_LONG).show();*/
}
@Override
public String getCaption() {
return "Build log";
}
});
}