Android 适配器不工作
Android Adapter not working
我已关注 this tutorial for the creation of a custom adapter.This link is mentioned in the comments to this SO 问题。
给出代码如下:
MyMainView.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.context.GlobalSearch">
<AutoCompleteTextView
android:id="@+id/globalSearch_editBrandData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="Enter Name"
android:imeOptions="actionDone"
android:singleLine="true"
/>
<ListView
android:id="@+id/globalSearch_listResult"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/globalSearch_editBrandData"
android:visibility="visible"
>
</ListView>
<TextView
android:id="@+id/globalSearch_errorMessage"
android:layout_below="@+id/globalSearch_editBrandData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Please enter the correct data"
android:gravity="center"
android:layout_marginBottom="100dp"
android:visibility="gone"
/>
在每个元素的列表中,我想显示一个图像和一些文本。所以我编辑的 listview
字段定义为:
MyEdited.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/rounded_corners"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:elevation="1dp" >
<ImageView
android:id="@+id/globalSearch_imageView"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
/>
<TextView
android:id="@+id/globalSearch_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/globalSearch_imageView"
android:text = "Puma"
android:gravity="center"
/>
</RelativeLayout>
现在列表视图的适配器定义为:
GlobalSearchInformation.java
import android.util.Log;
public class GlobalSearchInformation {
private int drawableID;
private String textDetails;
public GlobalSearchInformation(int drawbaleID, String textDetails)
{
super();
this.drawableID = drawableID;
this.textDetails = textDetails;
}
public void setTextDetails(String textDetails)
{
this.textDetails = textDetails;
}
public String getTextDetails()
{
return textDetails;
}
public void setDrawableID(int drawableID)
{
this.drawableID = drawableID;
}
public int getDrawableID()
{
return drawableID;
}
}
GlobalSearchAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class GlobalSearchAdapter extends BaseAdapter {
Context context;
protected List<GlobalSearchInformation> globalSearchList;
LayoutInflater layoutInflater;
public GlobalSearchAdapter(Context context, List<GlobalSearchInformation> globalSearchList)
{
this.globalSearchList = globalSearchList;
this.layoutInflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount() {
return globalSearchList.size();
}
@Override
public Object getItem(int position) {
return globalSearchList.get(position);
}
@Override
public long getItemId(int position) {
return globalSearchList.get(position).getDrawableID();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = this.layoutInflater.inflate(R.layout.globalsearch_listview, parent, false);
holder.globalsearchadapter_text = (TextView) convertView.findViewById(R.id.globalSearch_textView);
holder.globalsearchadapter_image = (ImageView) convertView.findViewById(R.id.globalSearch_imageView);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
GlobalSearchInformation gsi = globalSearchList.get(position);
holder.globalsearchadapter_image.setImageResource(gsi.getDrawableID());
holder.globalsearchadapter_text.setText(gsi.getTextDetails());
return convertView;
}
private class ViewHolder
{
ImageView globalsearchadapter_image;
TextView globalsearchadapter_text;
}
}
GlobalSearch.java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import APP.infrastructure.models.Merchant;
import java.util.ArrayList;
import java.util.List;
public class GlobalSearch extends ActionBarActivity {
ListView listViewGlobal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_global_search);
final AutoCompleteTextView actSearch = (AutoCompleteTextView) findViewById(R.id.globalSearch_editBrandSearch);
final ListView lvSearch = (ListView) findViewById(R.id.globalSearch_listResult);
final TextView tvSearch = (TextView) findViewById(R.id.globalSearch_errorMessage);
List<Merchant> merchants = Merchant.findWithQuery(Merchant.class,"select * from MERCHANT");
ArrayList <String> al = new ArrayList<String>();
for(int i=0;i<merchants.size(); i++)
{
al.add(i, merchants.get(i).getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, al);
actSearch.setAdapter(adapter);
actSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE)
{
String mEdit = actSearch.getText().toString();
List<Merchant> merchants = Merchant.findWithQuery(Merchant.class,"select * from MERCHANT where UPPER(NAME) LIKE \'"+mEdit.toUpperCase()+"%\'");
if(merchants.size()==0)
{
Log.d("debug", "Alert dialog: does not exist");
lvSearch.setVisibility(View.GONE);
tvSearch.setVisibility(View.VISIBLE);
}
else
{
ArrayList <GlobalSearchInformation> arrayInformation = new ArrayList<GlobalSearchInformation>();
Log.d("global search", "merchant size != 0");
GlobalSearchInformation globalSearch_Page = new GlobalSearchInformation(R.drawable.image_id, merchants.get(0).getName());
GlobalSearchInformation globalSearch_Feed = new GlobalSearchInformation(R.drawable.image_id, "Posts by "+merchants.get(0).getName());
GlobalSearchInformation globalSearch_Store = new GlobalSearchInformation(R.drawable.image_id, merchants.get(0).getName()+" stores around you");
GlobalSearchInformation globalSearch_Alerts = new GlobalSearchInformation(R.drawable.image_id, merchants.get(0).getName()+" Events");
arrayInformation.add(globalSearch_Page);
arrayInformation.add(globalSearch_Feed);
arrayInformation.add(globalSearch_Store);
arrayInformation.add(globalSearch_Alerts);
GlobalSearchAdapter adapter = new GlobalSearchAdapter(getApplicationContext(), arrayInformation);
lvSearch.setAdapter(adapter);
}
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_global_search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
所有这些都在编译并且 运行 没有错误但是在 autocomplete textview
中输入数据后查看 listview
时,正在检索数据库中的元素但是listview
仍然是空的。我哪里错了?
不要使用应用程序上下文来扩充列表视图。请改用 activity 上下文。
GlobalSearchAdapter 适配器 = new GlobalSearchAdapter(GlobalSearch.this, arrayInformation);
lvSearch.setAdapter(适配器);
不要在每次调用 onEditorAction
函数时都初始化适配器,而只是更新适配器中的数据并调用 notifyDataSetChanged()
.
globalSearch_textView
与 ListView
重叠,因此列表视图将不可见。更改文本视图的高度以包裹内容。
<TextView
android:id="@+id/globalSearch_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/globalSearch_imageView"
android:gravity="center"
android:text="Puma" />
我已关注 this tutorial for the creation of a custom adapter.This link is mentioned in the comments to this SO 问题。
给出代码如下:
MyMainView.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.context.GlobalSearch">
<AutoCompleteTextView
android:id="@+id/globalSearch_editBrandData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="Enter Name"
android:imeOptions="actionDone"
android:singleLine="true"
/>
<ListView
android:id="@+id/globalSearch_listResult"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/globalSearch_editBrandData"
android:visibility="visible"
>
</ListView>
<TextView
android:id="@+id/globalSearch_errorMessage"
android:layout_below="@+id/globalSearch_editBrandData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Please enter the correct data"
android:gravity="center"
android:layout_marginBottom="100dp"
android:visibility="gone"
/>
在每个元素的列表中,我想显示一个图像和一些文本。所以我编辑的 listview
字段定义为:
MyEdited.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/rounded_corners"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:elevation="1dp" >
<ImageView
android:id="@+id/globalSearch_imageView"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
/>
<TextView
android:id="@+id/globalSearch_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/globalSearch_imageView"
android:text = "Puma"
android:gravity="center"
/>
</RelativeLayout>
现在列表视图的适配器定义为:
GlobalSearchInformation.java
import android.util.Log;
public class GlobalSearchInformation {
private int drawableID;
private String textDetails;
public GlobalSearchInformation(int drawbaleID, String textDetails)
{
super();
this.drawableID = drawableID;
this.textDetails = textDetails;
}
public void setTextDetails(String textDetails)
{
this.textDetails = textDetails;
}
public String getTextDetails()
{
return textDetails;
}
public void setDrawableID(int drawableID)
{
this.drawableID = drawableID;
}
public int getDrawableID()
{
return drawableID;
}
}
GlobalSearchAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class GlobalSearchAdapter extends BaseAdapter {
Context context;
protected List<GlobalSearchInformation> globalSearchList;
LayoutInflater layoutInflater;
public GlobalSearchAdapter(Context context, List<GlobalSearchInformation> globalSearchList)
{
this.globalSearchList = globalSearchList;
this.layoutInflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount() {
return globalSearchList.size();
}
@Override
public Object getItem(int position) {
return globalSearchList.get(position);
}
@Override
public long getItemId(int position) {
return globalSearchList.get(position).getDrawableID();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = this.layoutInflater.inflate(R.layout.globalsearch_listview, parent, false);
holder.globalsearchadapter_text = (TextView) convertView.findViewById(R.id.globalSearch_textView);
holder.globalsearchadapter_image = (ImageView) convertView.findViewById(R.id.globalSearch_imageView);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
GlobalSearchInformation gsi = globalSearchList.get(position);
holder.globalsearchadapter_image.setImageResource(gsi.getDrawableID());
holder.globalsearchadapter_text.setText(gsi.getTextDetails());
return convertView;
}
private class ViewHolder
{
ImageView globalsearchadapter_image;
TextView globalsearchadapter_text;
}
}
GlobalSearch.java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import APP.infrastructure.models.Merchant;
import java.util.ArrayList;
import java.util.List;
public class GlobalSearch extends ActionBarActivity {
ListView listViewGlobal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_global_search);
final AutoCompleteTextView actSearch = (AutoCompleteTextView) findViewById(R.id.globalSearch_editBrandSearch);
final ListView lvSearch = (ListView) findViewById(R.id.globalSearch_listResult);
final TextView tvSearch = (TextView) findViewById(R.id.globalSearch_errorMessage);
List<Merchant> merchants = Merchant.findWithQuery(Merchant.class,"select * from MERCHANT");
ArrayList <String> al = new ArrayList<String>();
for(int i=0;i<merchants.size(); i++)
{
al.add(i, merchants.get(i).getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, al);
actSearch.setAdapter(adapter);
actSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE)
{
String mEdit = actSearch.getText().toString();
List<Merchant> merchants = Merchant.findWithQuery(Merchant.class,"select * from MERCHANT where UPPER(NAME) LIKE \'"+mEdit.toUpperCase()+"%\'");
if(merchants.size()==0)
{
Log.d("debug", "Alert dialog: does not exist");
lvSearch.setVisibility(View.GONE);
tvSearch.setVisibility(View.VISIBLE);
}
else
{
ArrayList <GlobalSearchInformation> arrayInformation = new ArrayList<GlobalSearchInformation>();
Log.d("global search", "merchant size != 0");
GlobalSearchInformation globalSearch_Page = new GlobalSearchInformation(R.drawable.image_id, merchants.get(0).getName());
GlobalSearchInformation globalSearch_Feed = new GlobalSearchInformation(R.drawable.image_id, "Posts by "+merchants.get(0).getName());
GlobalSearchInformation globalSearch_Store = new GlobalSearchInformation(R.drawable.image_id, merchants.get(0).getName()+" stores around you");
GlobalSearchInformation globalSearch_Alerts = new GlobalSearchInformation(R.drawable.image_id, merchants.get(0).getName()+" Events");
arrayInformation.add(globalSearch_Page);
arrayInformation.add(globalSearch_Feed);
arrayInformation.add(globalSearch_Store);
arrayInformation.add(globalSearch_Alerts);
GlobalSearchAdapter adapter = new GlobalSearchAdapter(getApplicationContext(), arrayInformation);
lvSearch.setAdapter(adapter);
}
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_global_search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
所有这些都在编译并且 运行 没有错误但是在 autocomplete textview
中输入数据后查看 listview
时,正在检索数据库中的元素但是listview
仍然是空的。我哪里错了?
不要使用应用程序上下文来扩充列表视图。请改用 activity 上下文。
GlobalSearchAdapter 适配器 = new GlobalSearchAdapter(GlobalSearch.this, arrayInformation); lvSearch.setAdapter(适配器);
不要在每次调用
onEditorAction
函数时都初始化适配器,而只是更新适配器中的数据并调用notifyDataSetChanged()
.globalSearch_textView
与ListView
重叠,因此列表视图将不可见。更改文本视图的高度以包裹内容。<TextView android:id="@+id/globalSearch_textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/globalSearch_imageView" android:gravity="center" android:text="Puma" />