繁体   English   中英

从ListView启动新活动

[英]Launch a new activity from ListView

因此,我设置了一个列表视图,该列表视图从strings.xml文件中的字符串数组填充自身,但似乎无法弄清楚如何对其进行编码,因此点击/单击列表中的一个选项可启动一个新活动。 我知道已经存在有关此主题的信息,但似乎我以不同的方式设置了列表视图,因此提出的解决方案不兼容。 这是我的mainactivity.java代码:

public class MainActivity extends AppCompatActivity implements OnClickListener {
Map<String, Integer> mapIndex;
ListView consoleList;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] consoles = getResources().getStringArray(R.array.consoles_array);

    Arrays.asList(consoles);

    consoleList = (ListView) findViewById(R.id.list_consoles);
    consoleList.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, consoles));

    getIndexList(consoles);

    displayIndex();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

private void getIndexList(String[] consoles) {
    mapIndex = new LinkedHashMap<String, Integer>();
    for (int i = 0; i < consoles.length; i++) {
        String console = consoles[i];
        String index = console.substring(0, 1);

        if (mapIndex.get(index) == null)
            mapIndex.put(index, i);
    }
}

private void displayIndex() {
    LinearLayout indexLayout = (LinearLayout) findViewById(R.id.side_index);

    TextView textView;
    List<String> indexList = new ArrayList<String>(mapIndex.keySet());
    for (String index : indexList) {
        textView = (TextView) getLayoutInflater().inflate(
                R.layout.side_index_item, null);
        textView.setText(index);
        textView.setOnClickListener(this);
        indexLayout.addView(textView);
    }
}

public void onClick(View view) {
    TextView selectedIndex = (TextView) view;
    consoleList.setSelection(mapIndex.get(selectedIndex.getText()));
}


@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.tylerfrisbee.gamerguide/http/host/path")
    );
    AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.tylerfrisbee.gamerguide/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
}

}

您可以在ListView上调用setOnItemClickListener

consoleList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    //do whatever you want to do if you want to start a new Activity:
    Intent intent = new Intent(MainActivity.this, MyActivity.class);
    startActivity(intent);
  }
});

单击ListView中的项目后,这将立即填充onItemClick方法;如果您要执行此操作,则应调用onClick方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM