如何制作某个自定义列表视图项,将您发送到某个活动?

时间:2016-03-01 04:56:03

标签: java android listview android-intent android-activity

这是我的Java:

public class Cantos extends AppCompatActivity {

ListView lv;
Context context;

ArrayList cantoList;
public static String[] cantos = {"1: Abre Tu Oido", "2: A Cristo Quiero", "3: Acerquese Mi Clamor", "4: A Cristo Yo Alabare",
        "5: Acude Dios", "6: Adelante", "7: A Dios Canto", "8: Adios Para Siempre", "9: Ahora Senor", "10: A Jesucristo Ven",
        "11: Alabad A Dios"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cantos);
    initTypeface();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);
    toolbar.setNavigationIcon(getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Cantos.this, MainMenu.class);
            startActivity(i);
        }
    });



}

private void initTypeface() {

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/AftaSerifThin-Regular.otf");
    TextView text = (TextView) findViewById(R.id.toolbarCantos);
    text.setTypeface(myTypeface);

}
}

这是我的另一个带有BaseAdapter的Java(自定义ListView):

public class CustomAdapter extends BaseAdapter {

String[] result;
Context context;
private static LayoutInflater inflater = null;


public CustomAdapter(Cantos cantos, String[] cantos1) {

    result = cantos1;
    context = cantos;
    inflater = (LayoutInflater)context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


@Override
public int getCount() {
    return result.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public class Holder
{
    TextView tv;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Holder holder = new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.cantos_list, null);
    holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
    holder.tv.setText(result[position]);


    Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf");
    holder.tv.setTypeface(myFont);


    return rowView;
}


}

如何让某件物品送我去另一项活动? 先感谢您! P.S抱歉,如果我发送垃圾邮件太多了!我差点完成学习一些基础知识!求求你应付我!

3 个答案:

答案 0 :(得分:1)

如果要在列表视图中选择项目时调用另一个活动,可以在自定义适配器中调用OnClick监听器textview

代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

Holder holder = new Holder();
View rowView;

rowView = inflater.inflate(R.layout.cantos_list, null);
holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
holder.tv.setText(result[position]);

holder.tv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(context,NewAcivity.class);
                    ((Activity) context).startActivity(intent);
                }
            });


Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf");
holder.tv.setTypeface(myFont);


return rowView;
}

答案 1 :(得分:0)

使用intent.putExtra(“key”,value)& getExtra()将数据从一个活动发送到另一个活动

//try this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            Intent intent;
            intent = new Intent(CurrentActivity.this, NextActivity.class);

                startActivity(intent)

        }
    });

答案 2 :(得分:0)

首先为列表视图设置适配器。然后执行onItem点击事件。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                displayAnotherListView(position);
            }
        });


 public void displayAnotherListView(int position) {


        switch (position) {
            case 0: {
                Intent i=new Intent(YourActivity.this,Activity_one.class);
               startActivity(ii);
                break;
            }
            case 1: {

                Intent one=new Intent(YourActivity.this,Activity_two.class);
                startActivity(one);
                break;
            }
            case 2: {
                Intent two = new Intent(YourActivity.this, Activity_three.class);
               startActivity(two);
                break;
            }
}
}

你可以为每个项目执行此操作,但是如果你想为所有项目传递相同的意图而不需要让方法找到位置,只需在项目点击上传递意图。