我试图创建一个搜索抛出listview项目的活动。我创建了+---------+--------+--------+
| name | value1 | value2 |
+---------+--------+--------+
| Abel | a | NULL |
| Baker | b | x |
| Charlie | NULL | y |
+---------+--------+--------+
switch
case
,其中Category
是Category
main
中的一个项目,但是当我点击时项目没有执行任何操作,LogCat显示此消息 - > 04-29 20:20:00.380:D / ViewRootImpl(30869):ViewPostImeInputStage ACTION_DOWN
任何人都可以告诉我解决方案吗?
Search.java
<string-array>
ListEntry.java
public class Search extends Activity implements TextWatcher/*,AdapterView.OnItemClickListener*/{
private String[] main,category,manufacturer,Chanel,Gucci,DandG,DKNY,MK;
private ListView listView;
private ArrayList<ListEntry> data_normal;
private ArrayList<ListEntry> data_search;
private EditText main_listview_edittext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
//get data
main=getResources().getStringArray(R.array.main);
manufacturer=getResources().getStringArray(R.array.Manufacturer);
category=getResources().getStringArray(R.array.Category);
Chanel=getResources().getStringArray(R.array.Chanel);
Gucci=getResources().getStringArray(R.array.Gucci);
DandG=getResources().getStringArray(R.array.DandG);
DKNY=getResources().getStringArray(R.array.DKNY);
MK=getResources().getStringArray(R.array.MK);
//loop data
data_normal = new ArrayList<ListEntry>();
for(int i=0; i<main.length-1; i++){
ListEntry listEntry1=new ListEntry();
listEntry1.setTitle(main[i]);
data_normal.add(listEntry1);
}
for(int i=0; i<category.length; i++){
ListEntry listEntry1=new ListEntry();
listEntry1.setTitle(category[i]);
data_normal.add(listEntry1);
}
for(int i=0; i<manufacturer.length; i++){
ListEntry listEntry=new ListEntry();
listEntry.setTitle(manufacturer[i]);
data_normal.add(listEntry);
}
for(int i=0; i<Chanel.length; i++){
ListEntry listEntry1=new ListEntry();
listEntry1.setTitle(Chanel[i]);
data_normal.add(listEntry1);
}
for(int i=0; i<Gucci.length; i++){
ListEntry listEntry1=new ListEntry();
listEntry1.setTitle(Gucci[i]);
data_normal.add(listEntry1);
}
for(int i=0; i<DandG.length; i++){
ListEntry listEntry1=new ListEntry();
listEntry1.setTitle(DandG[i]);
data_normal.add(listEntry1);
}
for(int i=0; i<DKNY.length; i++){
ListEntry listEntry1=new ListEntry();
listEntry1.setTitle(DKNY[i]);
data_normal.add(listEntry1);
}
for(int i=0; i<MK.length; i++){
ListEntry listEntry1=new ListEntry();
listEntry1.setTitle(MK[i]);
data_normal.add(listEntry1);
}
//view matching..
main_listview_edittext=(EditText)findViewById(R.id.main_listview_edittext);
listView=(ListView)findViewById(R.id.main_listView);
doListNormal();
//event EditText
main_listview_edittext.addTextChangedListener(this); //TextWatcher
}
private void doListSearch() {
listView.setAdapter(new CustomAdapter(data_search));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch(listView.getItemAtPosition(position).toString()){
case "Category":
Intent i=new Intent(Search.this,MainActivity.class);
startActivity(i);
//get.lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.Category)));
break;}
//whatever you want to happen when an item is clicked
}
});
}
private void doListNormal() {
listView.setAdapter(new CustomAdapter(data_normal));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch(listView.getItemAtPosition(position).toString()){
case "Category":
Intent i=new Intent(Search.this,MainActivity.class);
startActivity(i);
//get.lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.Category)));
break;}
//whatever you want to happen when an item is clicked
}
});
}
/*@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Intent i=new Intent(Search.this,SwipeActivity.class);
switch(listView.getItemAtPosition(position).toString()){
case "Category":
Intent i=new Intent(Search.this,MainActivity.class);
startActivity(i);
//get.lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.Category)));
break;
}
}*/
//TextWatchet implement method 1
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
//TextWatchet implement method 2
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(main_listview_edittext.length() != 0){
data_search = new ArrayList<ListEntry>();
for(int i=0; i<data_normal.size(); i++){
//Check if the Text is Match the Contents
if(data_normal.get(i).getTitle().contains(s)||data_normal.get(i).getTitle().toLowerCase().contains(s)||data_normal.get(i).getTitle().toUpperCase().contains(s)){
ListEntry listEntry=new ListEntry();
listEntry.setTitle(data_normal.get(i).getTitle());
data_search.add(listEntry);
}
}
doListSearch();
}else {
doListNormal();
}
}
//TextWatchet implement method 3
@Override
public void afterTextChanged(Editable s) {
}
public class CustomAdapter extends BaseAdapter{
private ArrayList<ListEntry> mData;
public Holder holder=new Holder();
public CustomAdapter(ArrayList<ListEntry> data){
this.mData=data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return data_normal.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=View.inflate(Search.this,R.layout.listview_item_layout,null);
if(convertView!=null){
//view matching child
holder.title = (TextView) convertView.findViewById(R.id.listview_item_title);
holder.title.setText(mData.get(position).getTitle());
convertView.setTag(holder);
}else{
convertView=(View) convertView.getTag();
}
return convertView;
}
public class Holder{
public TextView title;
}
}
}
}
logcat的
public class ListEntry {
private String title;
private String address;
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title=title;
}
@Override
public String toString() {
return super.toString();
}
listview_item_layout.xml
04-30 22:11:22.610: D/SecWifiDisplayUtil(18274): Metadata value : none
04-30 22:11:22.645: D/PhoneWindow(18274): *FMB* installDecor mIsFloating : false
04-30 22:11:22.645: D/PhoneWindow(18274): *FMB* installDecor flags : 8454400
04-30 22:11:22.650: D/AbsListView(18274): Get MotionRecognitionManager
04-30 22:11:22.650: E/MotionRecognitionManager(18274): mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@aacd083
04-30 22:11:22.650: E/MotionRecognitionManager(18274): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@3b6ee200
04-30 22:11:22.650: E/MotionRecognitionManager(18274): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@3b6ee200
04-30 22:11:22.660: D/OpenGLRenderer(18274): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-30 22:11:22.670: D/PhoneWindow(18274): *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
04-30 22:11:22.670: D/PhoneWindow(18274): *FMB* isFloatingMenuEnabled return false
04-30 22:11:22.675: D/SRIB_DCS(18274): log_dcs ThreadedRenderer::initialize entered!
04-30 22:11:22.680: I/OpenGLRenderer(18274): Initialized EGL, version 1.4
04-30 22:11:22.685: I/OpenGLRenderer(18274): HWUI protection enabled for context , &this =0xb4627190 ,&mEglDisplay = 1 , &mEglConfig = -1267322608
04-30 22:11:22.685: D/OpenGLRenderer(18274): Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192
04-30 22:11:22.685: D/OpenGLRenderer(18274): Enabling debug mode 0
04-30 22:11:22.685: D/mali_winsys(18274): new_window_surface returns 0x3000, [1440x2560]-format:1
04-30 22:11:22.700: D/ListView(18274): change accessibility focus position = 0
04-30 22:11:22.795: I/Timeline(18274): Timeline: Activity_idle id: android.os.BinderProxy@23126a39 time:66657037
04-30 22:11:26.315: D/ViewRootImpl(18274): ViewPostImeInputStage ACTION_DOWN
04-30 22:11:26.425: I/Timeline(18274): Timeline: Activity_launch_request id:com.example.shaymatest time:66660669
04-30 22:11:26.455: D/PhoneWindow(18274): *FMB* installDecor mIsFloating : false
04-30 22:11:26.455: D/PhoneWindow(18274): *FMB* installDecor flags : 8454400
04-30 22:11:26.460: D/AbsListView(18274): Get MotionRecognitionManager
04-30 22:11:26.460: E/MotionRecognitionManager(18274): mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@c41da89
04-30 22:11:26.460: E/MotionRecognitionManager(18274): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@1795fa8e
04-30 22:11:26.460: E/MotionRecognitionManager(18274): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@1795fa8e
04-30 22:11:26.465: D/SecWifiDisplayUtil(18274): Metadata value : none
04-30 22:11:26.470: D/PhoneWindow(18274): *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
04-30 22:11:26.470: D/PhoneWindow(18274): *FMB* isFloatingMenuEnabled return false
04-30 22:11:26.485: D/SRIB_DCS(18274): log_dcs ThreadedRenderer::initialize entered!
04-30 22:11:26.485: D/mali_winsys(18274): new_window_surface returns 0x3000, [1440x2560]-format:1
04-30 22:11:26.540: D/ListView(18274): change accessibility focus position = 0
04-30 22:11:26.565: E/SpannableStringBuilder(18274): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-30 22:11:26.565: E/SpannableStringBuilder(18274): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-30 22:11:26.565: I/Timeline(18274): Timeline: Activity_idle id: android.os.BinderProxy@150722af time:66660805
04-30 22:11:26.565: E/SpannableStringBuilder(18274): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-30 22:11:26.565: E/SpannableStringBuilder(18274): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-30 22:11:26.840: V/ActivityThread(18274): updateVisibility : ActivityRecord{27683843 token=android.os.BinderProxy@23126a39 {com.example.shaymatest/com.example.shaymatest.MainActivity}} show : false
04-30 22:11:27.425: D/ViewRootImpl(18274): ViewPostImeInputStage ACTION_DOWN
04-30 22:11:27.555: D/AndroidRuntime(18274): Shutting down VM
04-30 22:11:27.555: E/AndroidRuntime(18274): FATAL EXCEPTION: main
04-30 22:11:27.555: E/AndroidRuntime(18274): Process: com.example.shaymatest, PID: 18274
04-30 22:11:27.555: E/AndroidRuntime(18274): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
04-30 22:11:27.555: E/AndroidRuntime(18274): at com.example.shaymatest.Search.onItemClick(Search.java:126)
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.widget.AdapterView.performItemClick(AdapterView.java:339)
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.widget.AbsListView.performItemClick(AbsListView.java:1548)
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3737)
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.widget.AbsListView$3.run(AbsListView.java:5676)
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.os.Handler.handleCallback(Handler.java:739)
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.os.Handler.dispatchMessage(Handler.java:95)
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.os.Looper.loop(Looper.java:145)
04-30 22:11:27.555: E/AndroidRuntime(18274): at android.app.ActivityThread.main(ActivityThread.java:6872)
04-30 22:11:27.555: E/AndroidRuntime(18274): at java.lang.reflect.Method.invoke(Native Method)
04-30 22:11:27.555: E/AndroidRuntime(18274): at java.lang.reflect.Method.invoke(Method.java:372)
04-30 22:11:27.555: E/AndroidRuntime(18274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
04-30 22:11:27.555: E/AndroidRuntime(18274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
答案 0 :(得分:1)
好吧,看起来你的switch-case没有被调用。尝试将switch语句替换为if语句。使用.equals()
是比较字符串的更可靠方法。
TextView thisTextView = (TextView)view.findViewById(R.id.listview_item_title);
String text = (String)thisTextView.getText();
if(text.equals("Category")){
Intent i=new Intent(Search.this, MainActivity.class);
startActivity(i);
}else{
Toast.makeText(Search.this,"What's my text: " + text, Toast.LENGTH_LONG).show();
}
答案 1 :(得分:0)
您有onItemClick
但是在ListView上设置Click Listener时没有设置它。把它放在你的setOnItemClickListener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//whatever you want to happen when an item is clicked
}
});