我有这个问题我无法访问另一个布局或活动表单列表视图,我试图调用Intent但我总是得到“确认透视切换”,找不到源。
这是代码
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class HomeTab extends Activity {
private List<home_items> myItems= new ArrayList<home_items>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
populateHomeIteams();
populateListView();
registerClickCallback();
}
private void registerClickCallback() {
ListView list=(ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
//Toast.makeText(HomeTab.this, "positiom is"+position, Toast.LENGTH_LONG).show();
if(position==0)
{
Intent intt=new Intent(HomeTab.this, Beauty_Tab.class);
startActivity(intt);
}
}
});
}
private void populateHomeIteams() {
myItems.add(new home_items("Beauty", R.drawable.bg_list_view));
myItems.add(new home_items("Healthy food", R.drawable.bg_list_view));
myItems.add(new home_items("Family Health", R.drawable.bg_list_view));
myItems.add(new home_items("Moda", R.drawable.bg_list_view));
}
private void populateListView() {
ArrayAdapter<home_items> adapter= new MyListAdapter();
ListView list= (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<home_items> {
public MyListAdapter(){
super(HomeTab.this,R.layout.iteam_view,myItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView=getLayoutInflater().inflate(R.layout.iteam_view,parent ,false);
}
home_items currentitem=myItems.get(position);
ImageView imageview=(ImageView)itemView.findViewById(R.id.iteam_icon);
imageview.setImageResource(currentitem.get_iconID());
TextView maketxt=(TextView)itemView.findViewById(R.id.iteam_txt);
maketxt.setText(currentitem.get_iteam_name());
return itemView;
}
}
}
问题在于函数registerclickCallnack() 吐司正在工作但意图不起作用
private void registerClickCallback() {
ListView list=(ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
//Toast.makeText(HomeTab.this, "positiom is"+position, Toast.LENGTH_LONG).show();
if(position==0)
{
Intent intt=new Intent(HomeTab.this, Beauty_Tab.class);
startActivity(intt);
}
}
});
}
答案 0 :(得分:0)
据我记得onItemClick()
方法position
从1开始
答案 1 :(得分:0)
你的问题是你实例化了2个listView对象,一个在populateListView中,另一个在registerClickCallback中;您实例化一个listView但在另一个上添加单击回调,确保您使用一个listView对象。