コードの特定の部分に問題があり、修正できないようです。Shop1 をクリックすると、ListView に Location、Contact、Appointment の 3 つの項目が表示されます。それらのいずれかを選択したときに別のアクティビティに入る方法を知りたいです。たとえば、[場所] をクリックすると、別のレイアウト ファイルに移動します。助けてください!
ここにいくつかの写真があります:
クリックされていません
クリックした
[場所] をクリックして、別のアクティビティに移動します
コードは次のとおりです。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity
{
private ListView list;
private ArrayAdapter<String> adapter;
private RibbonMenuView rbmView;
private Button test;
private ListView rbmListView, rbmListView2;
private ArrayAdapter<String> adapt, adapter2;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Init our views **/
rbmView = (RibbonMenuView) findViewById(R.id.ribbonMenuView1);
list = (ListView) findViewById(R.id.listView1);
/** Calls "hideMenu()" after each click similiar to the Facebook or Google+ application **/
test = (Button) findViewById(R.id.button);
test.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// shows how to hide the menu
if (rbmView != null)
{
rbmView.hideMenu();
}
}
});
/** This is the most important ListView, updating the main list in the Activity **/
final String[] items_list = { "Shop1", "Shop2", "Shop3" };
adapt = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items_list);
rbmListView = (ListView) findViewById(R.id.rbm_listview);
if (rbmListView != null)
{
rbmListView.setOnItemClickListener(new OnItemClickListener()
{
/*
* (non-Javadoc)
* @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
*/
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
switch (position)
{
case 0:
String[] items = { "Location", "Contact", "Appointment" };
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items);
break;
case 1:
String[] items1 = { "Location", "Contact", "Appointment" };
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items1);
break;
case 2:
String[] items2 = { "Location", "Contact", "Appointment" };
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items2);
break;
default:
String[] itemsd = { "A", "B", "C", "D" };
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, itemsd);
break;
}
// always update these
if (list != null)
{
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch ((int)l)
{
case 0: // Location
//Intent intent = new Intent(getApplicationContext(), NEW_ACTIVITY.class);
//startActivity(intent);
break;
case 1: // Contact
//Intent intent = new Intent(getApplicationContext(), NEW_ACTIVITY.class);
//startActivity(intent);
break;
case 2: // Appointment
//Intent intent = new Intent(getApplicationContext(), NEW_ACTIVITY.class);
//startActivity(intent);
break;
}
}
});
}
if (rbmView != null)
{
rbmView.hideMenu();
}
}
});
rbmListView.setAdapter(adapt);
}
/** This is the second ListView on the menu **/
final String[] items_list2 = { "About", "Deals", "Contact Developer", };
adapter2 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items_list2);
rbmListView2 = (ListView) findViewById(R.id.rbm_listview2);
if (rbmListView2 != null)
{
rbmListView2.setOnItemClickListener(new OnItemClickListener()
{
/*
* (non-Javadoc)
* @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
*/
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
switch (position)
{
case 0:
startActivity(new Intent(MainActivity.this, About.class));
break;
case 1:
startActivity(new Intent(MainActivity.this, DealsCode.class));
break;
case 2:
startActivity(new Intent(MainActivity.this, ContactDev.class));
break;
}
/*Toast.makeText(MainActivity.this, items_list2[position], Toast.LENGTH_SHORT).show();
if (rbmView != null)*/
{
rbmView.hideMenu();
}
}
});
rbmListView2.setAdapter(adapter2);
}
}
/** Allows users, even API < 5, to use the back button **/
/*
* (non-Javadoc)
* @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
*/
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK:
// if view is showing, close it
// all other back button clicks are handled by the phone
if (rbmView != null)
{
if (rbmView.isMenuShowing())
{
new Handler().postDelayed(new Runnable()
{
public void run()
{
rbmView.hideMenu();
}
}, 50);
}
else
{
return super.onKeyDown(keyCode, event);
}
}
break;
default:
return super.onKeyDown(keyCode, event);
}
return true;
}
/*
* (non-Javadoc)
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
/*
* (non-Javadoc)
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.test:
// toggle our menu
if (rbmView != null)
{
rbmView.toggleMenu();
}
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
他に何か必要な場合はお知らせください。