我是android的新程序员。我想创建一个可点击的列表视图。我的意思是当用户点击一个项目时,它会进入一个新页面并开始一个新的活动。请帮我。提前致谢。这是我在 mainactivity.java 中的代码。
package first.app;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterViewFlipper;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new MyAdapter(this,
android.R.layout.simple_list_item_1,R.id.textView1,
getResources().getStringArray(R.array.games)));
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list,parent, false);
String[] items = getResources().getStringArray(R.array.games);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
TextView tv = (TextView) row.findViewById(R.id.textView1);
tv.setText(items[position]);
if (items[position].equals("فوتبال")){
iv.setImageResource(R.drawable.football);
}
else if (items[position].equals("شنا")){
iv.setImageResource(R.drawable.swimming);
}
else if (items[position].equals("شمشیربازی")){
iv.setImageResource(R.drawable.fencing);
}
else if (items[position].equals("شطرنج")){
iv.setImageResource(R.drawable.chess);
}
return row;
}
}
}