我正在为我的应用创建一种登录系统,我让它工作到用户可以添加其详细信息并从列表中选择它们的位置。
用户将其信息输入UserInput类,然后将此信息存储在Firebase数据库中。然后,数据显示在ListView项中,但首先通过CustomAdapter类。这堂课是我遇到问题的地方。
目前,用户可以通过setOnClickListener()与此列表视图进行交互。当选择列表中的项目时,我让程序显示Toast并显示没问题。我现在要弄清楚的是如何获取它以便当用户点击listview项目(包括其名称和详细信息)时,它会立即将它们带到名为Menu_Activity.java的主菜单活动
消息Gradle Build
Error:(71, 44) error: no suitable constructor found for Intent(<anonymous OnClickListener>,Class<Menu_Activity>)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Context)
error:cannot find symbol method startActivity(Intent)
CustomAdapter.java
package uk.ac.napier.newsreader.Details_UI;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import uk.ac.napier.newsreader.Home_Screen_Activity;
import uk.ac.napier.newsreader.Menu_Activity;
import uk.ac.napier.newsreader.R;
import uk.ac.napier.newsreader.Details_UserInput.User;
import uk.ac.napier.newsreader.Routes_Activity;
/**
* Created by MarkB on 14/03/2017.
*/
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<User> users;
public CustomAdapter (Context c, ArrayList<User> users) {
this.c = c;
this.users = users;
}
@Override
public int getCount() {
return users.size();
}
@Override
public Object getItem(int position) {
return users.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null)
{
convertView = LayoutInflater.from(c).inflate(R.layout.model,parent,false);
}
TextView nameTxt = (TextView) convertView.findViewById(R.id.nameTxt);
TextView ageTxt = (TextView) convertView.findViewById(R.id.ageTxt);
TextView weightTxt = (TextView) convertView.findViewById(R.id.weightTxt);
final User u = (User) this.getItem(position);
nameTxt.setText(u.getName());
ageTxt.setText(u.getAge());
weightTxt.setText(u.getWeight());
//ONITEMCLICK
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(c,u.getName(), Toast.LENGTH_SHORT).show();
Intent startMenuActivity = new Intent(this, Menu_Activity.class);
startActivity(startMenuActivity);
}
});
return convertView;
}
}
使用&#34;(this,Menu_Activity.class)创建Intent时会出现问题;&#34;标记错误说
Cannot resolve constructor 'Intent(anonymous android.view.View.OnClickListener, java.lang.Class<uk.ac.napier.newsreader.Menu_Activity>)'
答案 0 :(得分:0)
Intent startMenuActivity = new Intent(c, Menu_Activity.class);
此处c
是您的上下文变量。
说明:
您应该将活动上下文作为第一个参数传递。
答案 1 :(得分:0)
这是我在我的一个申请中使用的。
LocationAdapter adapter = new LocationAdapter(getApplicationContext(), R.layout.row, result);
//set the locationList (listview) to use adapter initialised above
locationList.setAdapter(adapter);
//Set up onClickListner
locationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get the position of the row. So which row was clicked
LocationModel locationModel = result.get(position);
//initialise intent, this click event takes the user to locationDetails Activity
Intent intent = new Intent(ListActivity.this, LocationDetails.class);
//passing extra value. in this case passing of locationModel
intent.putExtra("locationModel", new Gson().toJson(locationModel));
//start activity
startActivity(intent);
}
});
答案 2 :(得分:0)
在适配器类中“this”不会返回类对象。 使用列表对象
设置OnItemClickListener listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent startMenuActivity = new Intent(MyActivity.this, Menu_Activity.class);
startActivity(startMenuActivity);
}
});
答案 3 :(得分:0)
改变这个:
//ONITEMCLICK
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startMenuActivity = new Intent(this, Menu_Activity.class);
startActivity(startMenuActivity);
}
});
为此:
//ONITEMCLICK
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startMenuActivity = new Intent(c, Menu_Activity.class);
startActivity(startMenuActivity);
}
});
性差异:
Intent startMenuActivity = new Intent( this ,Menu_Activity.class);
Intent startMenuActivity = new Intent( c ,Menu_Activity.class);
<强>记住强>:
当您将此“说”为匿名类时,您将引用她(匿名对象类),而不是您的“主类”。