我创建了一个名为“AndRoot!”的应用程序。
嗨!如何通过单击ListView中的“App Developer's Concern”进入名为“ConditionsActivity”的活动
这是我的“TopicActivity”代码
package com.devcon.androot;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TopicActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic);
String[] myStringArray={"App Developers' Concern","What is Root?","Pros of Rooted Device","Cons of Rooted Device",
"How to Root","How Root can do that things?","Cheating and Hacking when Rooted?","Unrooted" +
" vs. Rooted","Manufacturer vs. Root","Bring my warranty back!","How to Unroot","Do Unrooting" +
" will bring my phone back to Original?","Credits"};
ArrayAdapter<String> myAdapter=new
ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
myStringArray);
ListView myList=(ListView)
findViewById(R.id.listView);
myList.setAdapter(myAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
这是我的内容topic.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.devcon.androot.TopicActivity"
tools:showIn="@layout/activity_topic">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
答案 0 :(得分:2)
您必须为列表设置一个侦听器
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this,ConditionsActivity.class);
startActivity(intent);
}
});
答案 1 :(得分:0)
尝试以下代码。
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent intent = new Intent(TopicActivity.this, ConditionsActivity.class);
startActivity(intent);
}
}
});
答案 2 :(得分:0)
在OnItemClickListener
上拨打Listview
,如下所示:
// your code to set adaptor on Listview
myList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// call intent here to move to another Activity on listview item click
}
});
答案 3 :(得分:0)
解决方案是使用OnItemClickListener
myList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
if (myStringArray[position].equals("App Developers' Concern") {
Intent intent = new Intent(TopicActivity.this, ConditionsActivity.class);
TopicActivity.this.startActivity(intent);
}
}
});
答案 4 :(得分:0)
您需要为listView设置onItemClickListener以处理click事件。我对你的代码做了一些改动。请检查。
package com.devcon.androot;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TopicActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic);
String[] myStringArray={"App Developers' Concern","What is Root?","Pros of Rooted Device","Cons of Rooted Device",
"How to Root","How Root can do that things?","Cheating and Hacking when Rooted?","Unrooted" +
" vs. Rooted","Manufacturer vs. Root","Bring my warranty back!","How to Unroot","Do Unrooting" +
" will bring my phone back to Original?","Credits"};
ArrayAdapter<String> myAdapter=new
ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
myStringArray);
ListView myList=(ListView)
findViewById(R.id.listView);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
switch(position) {
case 0:
startActivity(new Intent(TopicActivity.this, ConditionsActivity.class);
break;
}
});
myList.setAdapter(myAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
答案 5 :(得分:0)
您需要将onItemClickListener设置为ListView。 这是你如何做到的:
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Start new activity "YourSecondActivity"
Intent intent = new Intent(this, YourSecondActivity.class);
//Here you can send e.g. the items position by putting it as an extra into the intent
int itemClicked = position;
intent.putExtra("com.mycompany.myfirstapp.MESSAGE", itemClicked);
startActivity(intent);
}
});
顺便说一下:我不认为你的问题与javascript有关。你应该删除这个标签。
答案 6 :(得分:0)
myListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent intent = new Intent(TopicActivity.this, ConditionsActivity.class);
startActivity(intent);
}
}
});
答案 7 :(得分:0)
package com.devcon.androot;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TopicActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic);
String[] myStringArray={"App Developers' Concern","What is Root?","Pros of Rooted Device","Cons of Rooted Device",
"How to Root","How Root can do that things?","Cheating and Hacking when Rooted?","Unrooted" +
" vs. Rooted","Manufacturer vs. Root","Bring my warranty back!","How to Unroot","Do Unrooting" +
" will bring my phone back to Original?","Credits"};
ArrayAdapter<String> myAdapter=new
ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
myStringArray);
ListView myList=(ListView)
findViewById(R.id.listView);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
switch(position) {
case 0:
startActivity(new Intent(TopicActivity.this, ConditionsActivity.class);
break;
}
});
myList.setAdapter(myAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}