2015-01-28 6 views
0

나는 사용자 정의 어댑터의 생성에 대한 this 튜토리얼을 따라갔습니다.이 링크는 this에 대한 의견에 언급되어 있습니다. 다음과 같이안드로이드 어댑터가 작동하지 않습니다

코드가 주어진다 : 목록에서

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 필드는 다음과 같이 정의된다 :

GlobalSearchInformation.java

:

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> 

지금 목록보기 용 어댑터는 다음과 같이 정의된다
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); 
} 

}

이 모든 컴파일되고 에러없이하지만 입력 후 listview 시청 실행되고 autocomplete textview의 데이터를 검색하면 데이터베이스의 요소가 검색되지만 listview은 비어 있습니다. 내가 어디로 잘못 가고 있니?

+0

확인 날씨의 getView가 호출됩니다? 그렇다면 데이터 할당을 확인하십시오. –

+0

@IchigoKurosaki getView 메소드가 호출되고 있습니다. 데이터 할당을 확인하려면 어떻게합니까? –

+0

이것이 정확히 무엇을 원하는지보십시오 .http : //www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html –

답변

1
  • 목록보기를 확장하는 데 응용 프로그램 컨텍스트를 사용하지 마십시오. 대신 활동 컨텍스트를 사용하십시오.

    GlobalSearchAdapter adapter = new GlobalSearchAdapter (GlobalSearch.this, arrayInformation); lvSearch.setAdapter (adapter);

  • onEditorAction fuction이 호출 될 때마다 어댑터를 초기화하지 말고 대신 어댑터의 데이터를 업데이트하고 notifyDataSetChanged()을 호출하십시오.

  • 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" /> 
    
+0

이제 이미지가 아닌 텍스트가 표시됩니다. 이것이 작동하지 않는 이유는 무엇입니까? 두 문맥의 차이점과 언제 사용합니까? –

+0

요청한대로 변경했습니다. 이미지는 여전히 보이지 않습니다. –

+0

일반적으로 활동 컨텍스트가 뷰를 확장하는 데 사용됩니다. http://possiblemobile.com/2013/06/context/ -이 글은 다른 문맥의 차이점에 대한 좋은 설명입니다. –

관련 문제