2013-12-17 2 views
0

Нажмите на элемент поиска внутри edittext redirect на неправильном действии, он не открывает связанную с ним деятельность. Он открывает другие действия, связанные с другими списками, но не то, что я нажимаю один.щелкните не работает на searchitem из списка viewroid android

Вот мой полный код:

public class Tabtwo extends Activity implements OnItemClickListener { 

    ListView listView; 
    TextView txt; 

    ArrayAdapter<String> adapter; 

    // Search EditText 
    EditText edtSearch; 

    // Array of strings storing country names 
    String[] countries = new String[] { "Admin Cost", "Affinity Diagram", 
      "Analyse", "Apprasal Costs", "Assessment of Stakeholders", 
       }; 

    // Array of integers points to images stored in /res/drawable-ldpi/ 
    int[] flags = new int[] { R.drawable.admin, R.drawable.affinity, 
      R.drawable.analysis, R.drawable.appraisal, R.drawable.assessment, 
       }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabtwo); 

     // Each row in the list stores country name, currency and flag 
     List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>(); 

     for (int i = 0; i < 4; i++) { 
      HashMap<String, String> hm = new HashMap<String, String>(); 
      hm.put("txt", countries[i]); 

      hm.put("flag", Integer.toString(flags[i])); 
      aList.add(hm); 
     } 

     // Keys used in Hashmap 
     String[] from = { "flag", "txt" }; 

     // Ids of views in listview_layout 
     int[] to = { R.id.flag, R.id.txt }; 

     // Instantiating an adapter to store each items 
     // R.layout.listview_layout defines the layout of each item 
     final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), 
       aList, R.layout.listview_layout, from, to); 

     // Getting a reference to listview of main.xml layout file 
     ListView listView = (ListView) findViewById(R.id.listview); 
     edtSearch = (EditText) findViewById(R.id.Search_box); 
     txt = (TextView) findViewById(R.id.txt); 
     listView.setOnItemClickListener(this); 
     // Setting the adapter to the listView 
     listView.setAdapter(adapter); 

     listView.setTextFilterEnabled(true); 
     listView.setOnItemClickListener(this); 
     edtSearch.addTextChangedListener(new TextWatcher() { 

      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
       adapter.getFilter().filter(s); 
       adapter.notifyDataSetChanged(); 
      } 

      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
      } 

      public void afterTextChanged(Editable s) { 
      } 
     }); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
      long arg3) { 
     // TODO Auto-generated method stub 
     if (position == 0) { 
      Intent int0 = new Intent(getApplicationContext(), Admincost.class); 
      startActivity(int0); 
     } 

     if (position == 1) { 
      Intent int1 = new Intent(getApplicationContext(), Affinity.class); 
      startActivity(int1); 
     } 
     if (position == 2) { 
      Intent int2 = new Intent(getApplicationContext(), Analyse.class); 
      startActivity(int2); 
     } 

     if (position == 3) { 
      Intent int3 = new Intent(getApplicationContext(), 
        ApprasalCosts.class); 
      startActivity(int3); 
     } 
     if (position == 4) { 
      Intent int1 = new Intent(getApplicationContext(), Assessment.class); 
      startActivity(int1); 
     } } 
    } 
} 

Вот мой файл tabtwo XML.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/Search_box" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:hint="Search a Item from ListView" 
     android:inputType="textVisiblePassword" /> 
     /> 

    <TextView 
     android:id="@+id/List_item" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="12dip" 
     android:textSize="17sp" 
     android:textStyle="bold" /> 

    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="-50dp" /> 

</LinearLayout> 

Вот listview_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/flag" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="10dp" 
     android:paddingRight="10dp" 
     android:paddingTop="10dp" /> 

    <TextView 
     android:id="@+id/txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="21dp" /> 

</LinearLayout> 
+0

У вас есть пользовательский адаптер? –

+1

@niteshgoel 'final Адаптер SimpleAdapter = новый SimpleAdapter (getBaseContext(), aList, R.layout.listview_layout, from, to);' найти эту строку в коде. внимательно прочитайте вопрос. – SilentKiller

+0

@RobiKumarTomar сначала покажет ваш пользовательский адаптер. а также XML-файл этого адаптера «R.layout.listview_layout». – SilentKiller

ответ

0

Замените метод onItemClick с ниже и попробовать .. Надеюсь, что это работает ..

@Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
      long arg3) { 

     TextView tv = (TextView) arg1.findViewById(R.id.txt); 
     String str = tv.getText().toString().trim(); 

     if (str.equals(countries[0])) { 
      Intent int0 = new Intent(Tabtwo.this, Admincost.class); 
      startActivity(int0); 
     }else if(str.equals(countries[1])) { 
      Intent int1 = new Intent(Tabtwo.this, Affinity.class); 
      startActivity(int1); 
     }else if(str.equals(countries[2])) { 
      Intent int2 = new Intent(Tabtwo.this, Analyse.class); 
      startActivity(int2); 
     }else if(str.equals(countries[3])) { 
      Intent int3 = new Intent(Tabtwo.this, ApprasalCosts.class); 
      startActivity(int3); 
     }else if(str.equals(countries[4])) { 
      Intent int1 = new Intent(Tabtwo.this, Assessment.class); 
      startActivity(int1); 
     } 
} 
+0

@RobiKumarTomar вы попробовали мой ответ .. Это должно сработать .. я пробовал, и он отвечает вашим требованиям без каких-либо недостатков. – Hariharan

+0

@RobiKumarTomar Рад, что это помогло :) – Hariharan

0

Добавьте этот код в ваш TextView, а также до ImageView

android:focusableInTouchMode="false" 
android:clickable="false" 
android:focusable="false" 

и ItemClick вы можете использовать ответ Tamilan

+0

Его не работает друг, по каждому клику он берет на себя сначала активность или другой бесполезный. –

+0

@RobiKumarTomar на моей стороне работает очень хорошо .. – SilentKiller

0

я встречаю тот же вопрос с вами. Попробуйте выполнить следующее:

lv.setOnItemClickListener(new OnItemClickListener() { 
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
          // When clicked, show a toast with the TextView text 
          //(String) getListAdapter().getItem(position); 
          String city_id=map.get((String) adapter.getItem(position)); 
          Toast.makeText(getApplicationContext(),city_id, Toast.LENGTH_SHORT).show(); 

         Intent i=new Intent(MainActivity.this,DetailActivity.class); 
          i.putExtra("city_id",city_id); 
          startActivity(i); 
         }    
        }); 
Смежные вопросы