2016-02-12 80 views
0

我有一個自定義列表視圖。但問題是,可點擊的列表視圖的唯一部分是文本(無法在右側/空白處)。我還添加了一些我的java代碼。爲什麼我的listview只在文本被點擊時纔會響應?

這是xml文件我一個ArrayAdapter使用:

?xml version="1.0" encoding="utf-8"?> 

<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/text" 
    android:maxLines="1" 
    android:ellipsize="end" 
android:textSize="50sp"> 

</TextView> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 
static ArrayList<String> note; 
ListView listView; 
static ArrayList<String> notes = new ArrayList<>(); 
static ArrayAdapter arrayAdapter; 
static Set<String> set; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 

setTitle("Notely"); 
fab.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 





     notes.add(""); 

     SharedPreferences sharedPreferences = getSharedPreferences("mypackage", Context.MODE_PRIVATE); 

     if (set == null) { 

      set = new HashSet<String>(); 

     } else { 

      set.clear(); 

     } 

     set.addAll(notes); 
     arrayAdapter.notifyDataSetChanged(); 

     sharedPreferences.edit().remove("notes").apply(); 
     sharedPreferences.edit().putStringSet("notes", set).apply(); 

     Intent i = new Intent(getApplicationContext(), EditNote.class); 
     i.putExtra("noteId", notes.size() - 1); 
     startActivity(i); 






    } 
}); 


ListView listView = (ListView) findViewById(R.id.listView); 

SharedPreferences sharedPreferences = this.getSharedPreferences("mypackage", Context.MODE_PRIVATE); 

set = sharedPreferences.getStringSet("notes", null); 

notes.clear(); 

if (set != null) { 

    notes.addAll(set); 

} else { 

    notes.add("Example note"); 
    set = new HashSet<String>(); 
    set.addAll(notes); 
    sharedPreferences.edit().putStringSet("notes", set).apply(); 

} 


arrayAdapter = new ArrayAdapter(this, R.liststyle, notes); 

listView.setAdapter(arrayAdapter); 

listView.setAdapter(arrayAdapter); 

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     Intent i = new Intent(getApplicationContext(), EditNote.class); 
     i.putExtra("noteId", position); 
     startActivity(i); 


    } 

}); 



listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
    @Override 
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { 

     new AlertDialog.Builder(MainActivity.this) 
       .setIcon(android.R.drawable.ic_dialog_alert) 
       .setTitle("Are you sure?") 
       .setMessage("Do you want to delete this note?") 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         notes.remove(position); 

         SharedPreferences sharedPreferences =   MainActivity.this.getSharedPreferences("mypackage", Context.MODE_PRIVATE); 

         if (set == null) { 

          set = new HashSet<String>(); 

         } else { 

          set.clear(); 

         } 

         set.addAll(notes); 
         sharedPreferences.edit().remove("notes").apply(); 
         sharedPreferences.edit().putStringSet("notes", set).apply(); 
         arrayAdapter.notifyDataSetChanged(); 

        } 
       }) 
       .setNegativeButton("No", null) 
       .show(); 

     return true; 
    } 
}); 



} 





@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_main, 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 


return super.onOptionsItemSelected(item); 
} 
} 
+0

你曾經使用過的點擊監聽器? listviewItemclick? –

+0

你是什麼意思的自定義'ListView'?你的適配器是自定義的還是你正在擴展'ListView'本身?你如何收聽列表項目上的點擊事件?請分享一些更相關的代碼 – Mithun

+0

ArrayAdapter arrayAdapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,notes); –

回答

0

設置TextView的寬度match_parent

1

要在列表中反應選擇,設置OnItemClickListenerListView。如下圖所示,對於更多關於ListView

listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
    Toast.makeText(getApplicationContext(), 
     "Click ListItem Number " + position, Toast.LENGTH_LONG) 
     .show(); 
    } 
}); 
0

嗨,你可以使用onItemclicklistener,如果你看看其他的解決辦法,那麼你可以在適配器點擊主佈局。

listView.setOnItemClickListener(新OnItemClickListener(){ @Override 公共無效onItemClick(適配器視圖父,瀏覽視圖, INT位置,長ID){

} });

相關問題