【问题标题】:Multiple click in list row多次单击列表行
【发布时间】:2016-11-07 09:41:44
【问题描述】:

我在 Android 中有一个 ListView。每个列表项有 2 个文本视图。如果我点击一个TextView,我想打开一个Activity,如果我点击另一个,我想做另一件事。

我的适配器的代码是这样的:

public class LazyAdapterHelpCentersAlava extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    HashMap<String, String> song;
    Typeface tf; 

    Context context;

    public LazyAdapterHelpCentersAlava(Activity a, ArrayList<HashMap<String, String>> d, String font) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        tf = Typeface.createFromAsset(activity.getAssets(), font);
    }

    public int getCount() {
        return data.size();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
        vi = inflater.inflate(R.layout.list_row_help_centers, null);

        //TextView id = (TextView)vi.findViewById(R.id.id); // title
        TextView title = (TextView)vi.findViewById(R.id.nombre); // title
        TextView localizacion = (TextView)vi.findViewById(R.id.localizacion);
        TextView email = (TextView)vi.findViewById(R.id.email);
        TextView web = (TextView)vi.findViewById(R.id.web);

        song = new HashMap<String, String>();
        song = data.get(position);

        // Setting all values in listview
        title.setText(song.get(Help.TAG_NOMBRE));
        web.setText(song.get(Help.TAG_TELEFONO));
        localizacion.setText(song.get(Help.TAG_DIRECCION));
        email.setText(song.get(Help.TAG_EMAIL));

        title.setTypeface(tf);
        web.setTypeface(tf);
        localizacion.setTypeface(tf);
        email.setTypeface(tf);

        web = (TextView)vi.findViewById(R.id.web);
        web.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Need to start an Activity here     
            }
        });

        return vi;
    }
}

我已经在web 上设置了点击监听器,但是,我不知道如何打开一个活动,因为它说这样的错误方法startActivity(Intent) 未定义新类型View.OnClickListener(){}强>

有人可以帮助我吗?非常感谢。

【问题讨论】:

  • 你需要在 Context 上调用 startActivity。在您的情况下,您将其作为活动。调用activity.startActivity(intent);
  • 你可以使用我的代码吗?谢谢
  • 在您需要启动活动的 onClick 中。输入activity.startActivity(intent)

标签: android listview android-adapter


【解决方案1】:

使用适配器的上下文

        public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null){
            context = parent.getContext();
         //other code
        }

然后

        web.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
               Intent redirect = new Intent(context,New.class);
               context.startActivity(redirect);  
                }
           });

【讨论】:

  • 我在“Intent redirect = new Intent(context,New.class);”这一行得到一个错误
  • 替换你的类名而不是 newclass
【解决方案2】:

如果您的 Adapter 是 MainActivity 内的嵌套类,那么您需要执行类似的操作。

web.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent newActivity = new Intent(MainActivity.this, NewActivity.class);
        startActivity(newActivity);  
    }
});

如果您的适配器不是MainActivity 内的嵌套类,您需要先将ActivityContext 传递给LazyAdapterHelpCentersAlava,然后使用Context 启动Activity

public class LazyAdapterHelpCentersAlava extends BaseAdapter {

    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    HashMap<String, String> song;
    Typeface tf; 

    Context context;

    public LazyAdapterHelpCentersAlava(Context ctx, ArrayList<HashMap<String, String>> d, String font) {
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        tf = Typeface.createFromAsset(activity.getAssets(), font);
        context = ctx;   // Initialize the context here. 
    }

    // .. Here are the other functions ... Omitted for clarity

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
        vi = inflater.inflate(R.layout.list_row_help_centers, null);

        // Here are the other lines in getView function. Omitted for clarity

        web = (TextView)vi.findViewById(R.id.web);
        web.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Need to start an Activity here   
                Intent newActivity = new Intent(context, NewActivity.class);
                context.startActivity(newActivity); 
            }
        });

        return vi;
    }
}

【讨论】:

    【解决方案3】:

    在 oncreate() 方法中添加这一行

    this.context=this;
    

    在web Onclick Method下添加这一行

    Intent i = new Intent(context,NextActivity.class);
    context.startActivity(i);
    

    NextActivity是你必须去的Activity

    【讨论】:

      【解决方案4】:

      试试这个,因为你在构造函数中将上下文作为 Activity a

            web.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                 Intent redirect = new Intent(activity,New.class);
                 activity.startActivity(redirect);  
                  }
             });
      

      【讨论】:

        【解决方案5】:

        使用上下文,

        context.startActivity(intent);
        

        【讨论】:

          【解决方案6】:

          在你的 onClick() 方法中,添加:

           Intent startActivity=new Intent("abc.Xyz");
              startActivity(newActivity);
          

          在您的 AndroidManifest.xml 中,添加:

          <activity android:name=".Xyz"
                        android:label="@string/app_name">
                  <intent-filter>
                      <action android:name="abc.XYZ" />
                      <category android:name="android.intent.action.DEFAULT" />
                  </intent-filter>
              </activity>
          

          注意:在上面的代码中,Xyz 是您要启动的 Activity 的 ClassName,abc 是 Activity 文件所在的 PackageName。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-16
            • 1970-01-01
            • 2015-12-08
            • 1970-01-01
            • 1970-01-01
            • 2012-04-29
            相关资源
            最近更新 更多