Ich habe zwei Layoutdateien in meiner App. Auch ich habe Aktivität erweitert ListActivity. Jedes Element dieser Aktivität berücksichtigt die item.xml-Layoutdatei. Ich versuche, das Kontextmenü zu erhalten, wenn ich lange auf den Gegenstand drücke, aber ich sehe es nicht.Benutzerdefiniertes ListView und Kontextmenü. Wie bekomme ich es?
In meiner Tätigkeit versuche ich zu registerForContextMenu (getListView()) und überschreiben zwei Methoden
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getIntent().getExtras();
registerForContextMenu(getListView());
new PopulateAdapterTask().execute(ACTION_SELECT);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
return false;
}
long id = getListAdapter().getItemId(info.position);
Log.d(TAG, "id = " + id);
return true;
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
Item.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="wrap_content"
>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="15sp"
android:singleLine="true"
android:ellipsize="marquee"
android:scrollHorizontally = "true"
android:maxWidth="200dp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
>
<ImageButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@null"
android:paddingRight="10dp"
android:paddingLeft="10dp"
/>
</LinearLayout>
</LinearLayout>
All dies doesn arbeite nicht. Vielleicht liegt der Grund in LinearLayout? Ich finde auch ähnliches Thema Android: Context menu doesn't show up for ListView with members defined by LinearLayout?, aber ich habe komplizierteres Listenelement.
Wie bekomme ich ein Kontextmenü in meinem Fall?
Auch in meiner Tätigkeit habe ich innere Klasse erweitert ArrayAdapter. In dieser Klasse in der Methode "getView" kann ich OnCreateContextMenuListener für jede Ansicht festlegen, nachdem das Kontextmenü angezeigt wird, aber ich weiß nicht, wie mit Elementklicks verfahren werden soll. Wenn ich dies in der Methode onContextItemSelected versuche, ist das Objekt item.getMenuInfo() immer null und ich kann keine Informationen von ihm erhalten.
private class ChannelAdapter extends ArrayAdapter<Channel> {
private List<Channel> channels;
public ChannelAdapter(Context context, int textViewResourceId, List<Channel> objects) {
super(context, textViewResourceId, objects);
this.channels = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.station_item, null);
}
v.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
});
Danke. Hoffe auf deine Hilfe.
Ich habe eine kleine Verbesserung gefunden - 'v.setOnCreateContextMenuListener (null)'. – m039
Danke @Georgy. das hat mir geholfen –
funktioniert wie ein Charme +1 – mprabhat