我正在创建一个应用程序,其中有一个活动从另一个活动发送到另一个活动。我正在使用其他ArrayAdapters。我已经创建了一个包含我需要的参数的容器,但是当我在Activity中编写它时,它不起作用,因为它期望别的东西比我写的东西。我从一个活动发送到另一个活动的数据是来自用户的输入。我正在使用输入来过滤我制作的ListView。所以我的问题是,如何修复构造函数,以便它可以使用我需要的参数?我知道我上传了很多代码,但我认为这是相关的。
这是我的容器:
public class Container{
public final String søgeord;
public final String itemname;
public final Integer imgid;
Container(String søgeord, String itemname, Integer imgid){
this.søgeord=søgeord;
this.itemname = itemname;
this.imgid = imgid;
}
}
这是CustomListAdapter:
public class CustomListAdapter extends ArrayAdapter<Container>{
private final Activity context;
LayoutInflater inflater;
public CustomListAdapter(Activity context, Container[] containers) {
super(context, R.layout.mylist, containers);
this.context=context;
}
public View getView(int position,View view,ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylist, null);
TextView txtTitle = (TextView) view.findViewById(R.id.item);
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
TextView extratxt = (TextView) view.findViewById(R.id.textView1);
Container currentItem = getItem(position);
txtTitle.setText(currentItem.itemname);
imageView.setImageResource(currentItem.imgid);
extratxt.setText(currentItem.itemname);
return view;
};
}
这是我发送数据的活动,并在构造函数旁边注释:
public class Forside extends AppCompatActivity implements View.OnClickListener, Liste{
CustomListAdapter listAdapter;
EditText filterText;
ImageButton sog;
EditText sogefelt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forside);
filterText = (EditText)findViewById(R.id.sogefelt);
ListView itemList = (ListView)findViewById(R.id.listView);
listAdapter = new CustomListAdapter (this, annoncer, imageId, null); //this is the constructor
sog = (ImageButton) findViewById(R.id.sog);
sog.setOnClickListener(this);
sogefelt = (EditText) findViewById(R.id.sogefelt);
itemList.setAdapter(listAdapter);
itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
Intent intent = new Intent(Forside.this, Sogeresultater.class);
startActivity(intent);
}
}
});
}
public void onClick (View v){
if (v == sog){
String søgeord = sogefelt.getText().toString();
ArrayList<String> resultater = new ArrayList<String>();
for(String i: annoncer) {
if (i.toUpperCase().contains(søgeord.toUpperCase()))
resultater.add(i);
}
Intent intent = new Intent(this, Sogeresultater.class);
intent.putExtra("søg", resultater.toArray(new String[resultater.size()]));
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.Forside) {
Intent intent = new Intent(this, Forside.class);
startActivity(intent);
}
else if (id == R.id.Logind){
Intent intent = new Intent(this, Log_Ind.class);
startActivity(intent);
}
else if (id==R.id.Opretbruger) {
Intent intent = new Intent(this, Opret_bruger.class);
startActivity(intent);
}
return true;
}
}
最后但并非最不重要的,这是我将数据发送到的活动:
public class Sogeresultater extends AppCompatActivity implements Liste{
CustomListAdapter listAdapter;
EditText filterText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
final String[] søgestreng = getIntent().getExtras().getStringArray("søg");
filterText = (EditText)findViewById(R.id.sogefelt);
ListView itemList = (ListView)findViewById(R.id.listView);
listAdapter = new CustomListAdapter (this, annoncer, imageId, søgestreng); //this also the constructor, here I want "søgestreng", as it's the input from the user
itemList.setAdapter(listAdapter);
itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
}
});
filterText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Sogeresultater.this.listAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.Forside) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
CustomListAdapter
只能处理Container
个数组。每个容器似乎都有您尝试提供给适配器构造函数的属性。
要使其正常工作,您需要将所拥有的数据转换为所需的格式:(即将String[]
数组转换为Container[]
数组)
List<Container> containers = new ArrayList<Container>();
for (String sogething : søgestreng) {
String itemname = "I don't know"; // ?
Integer imgid = 1234; // ?
Container container = new Container(sogething, itemname, imgid);
containers.add(container);
}
Container[] array = containers.toArray(new Container[containers.size()]);
listAdapter = new CustomListAdapter(this, array);
或者您需要更改现有/添加另一个对String[]
数组而不是完整Container
数组感到满意的构造函数。但是,由于您的适配器需要Container
中定义的内容,因此您必须想出一种方法来替换/加载属于该字符串的数据。