【问题标题】:Android with Intent and Custom Listview带有 Intent 和自定义 Listview 的 Android
【发布时间】:2018-02-19 06:13:19
【问题描述】:

我使用ArrayAdapter 自定义了一个ListView,但是当我在智能手机上运行它时,我点击了该项目但它并不暗,例如没有交互。 还有一个问题: 当我在 ListView 中的项目上使用 Intent 和 TextView ArrayAdapter 时,出现“Error:(64, 71) error: not an enclosing class: PlaylistActivity”错误

MainActivity.java

public class MainActivity extends AppCompatActivity {
...
}

AdapterCustom.java

public class AdapterCustom extends BaseAdapter {
...

@Override
public View getView(int i, View view, ViewGroup viewGroup ) {

        final LayoutInflater inflater= (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    view=inflater.inflate(layout,null);

    TextView tvFeelOnday= (TextView) view.findViewById(R.id.tv_feel);
    TextView tvFeelDate=(TextView) view.findViewById(R.id.tv_feel_date);

    Feel mFeel= FeelList.get(i);

    tvFeelOnday.setText(mFeel.getmFeelOnDay().toString());
    tvFeelDate.setText(mFeel.getmFeelDate()+"");


    tvFeelOnday.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(mcontext, "Hehe", Toast.LENGTH_SHORT).show();

            Intent intent;
            intent = new Intent(this, MainActivity.class);// an error
        }
    });
    return view;
}

【问题讨论】:

  • intent = new Intent(this, MainActivity.class) 这里 this 不涉及上下文。你需要一个上下文作为参数来代替
  • 然后我使用 public class FeelAdapter extends BaseAdapter { private Context mcontext;私有 int 布局;私有列表 FeelList; ..... tvFeelOnday.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent; intent = new Intent(mcontext, com.BKDN.Cellular.MusicActivity.class); Toast.makeText (mcontext, "呵呵", Toast.LENGTH_SHORT).show(); } });返回视图;但我不能 startActivity(intent);

标签: java android listview android-intent


【解决方案1】:

您必须使用您的活动上下文并使用该上下文来创建新活动。 为初始化上下文创建一个构造函数。

public AdapterCustom(Context context) {
          this.context = context;     
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多