我使用以下教程链接http://www.ezzylearning.com/tutorial.aspx?tid=1763429
创建了一个列表视图结果是,活动清单如下图所示
现在,我想通过单击单个行导航到不同的活动屏幕。我确实想要导航到下一个屏幕,但是我无法找到每一行的正确位置。我对android没有太多的了解,所以如果它太愚蠢的话,请提出我的问题。
虽然代码在上面的链接上可用,但我附加了.xml文件和.java。在这里。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
listview_item_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="@+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<TextView android:id="@+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
Weather.java
public class Weather {
public int icon;
public String title;
public Weather(){
super();
}
public Weather(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}
}
WeatherAdapter.java
public class WeatherAdapter extends ArrayAdapter<Weather>{
Context context;
int layoutResourceId;
Weather data[] = null;
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
MainActivity.java
public class MainActivity extends Activity {
private ListView listView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Weather weather_data[] = new Weather[]
{
new Weather(R.drawable.weather_cloudy, "Cloudy"),
new Weather(R.drawable.weather_showers, "Showers"),
new Weather(R.drawable.weather_snow, "Snow"),
new Weather(R.drawable.weather_storm, "Storm"),
new Weather(R.drawable.weather_sunny, "Sunny")
};
WeatherAdapter adapter = new WeatherAdapter(this,
R.layout.listview_item_row, weather_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
任何帮助将不胜感激 感谢
答案 0 :(得分:4)
您需要附上OnItemClickListener
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView <?> adapterView, View view, int position, long id) {
// position is index of the row that was clicked
}
});
答案 1 :(得分:1)
listView1.setOnItemClickListener
在哪里?你设置setOnItemClickListener to listView1?.
并在下面的代码中onItemClick有position
。
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
}
});
答案 2 :(得分:1)
你应该做这样的事情
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //do something
}
});
答案 3 :(得分:1)
将setOnItemSelectedListener与列表http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html
一起使用listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemSelectedListener(
new OnItemSelectedListener() {
@Override
public void onItemSelected(
AdapterView<?> parent, View view, int pos, long id
) {
//where pos is your index
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
}
);
答案 4 :(得分:1)
试试这种方式......
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Toast.makeText(context, position+"", Toast.LENGTH_LONG).show();
Intent go_detail_Activity_Intent= new Intent(context,Target_Activity.class);
go_detail_Activity_Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(go_detail_Activity_Intent);
}
});
我认为它会对你有所帮助。