【问题标题】:Launch a new activity from ListView从 ListView 启动一个新活动
【发布时间】:2016-01-11 21:01:45
【问题描述】:

所以我设置了一个列表视图,它从 strings.xml 文件中的字符串数组中填充自身,我似乎无法弄清楚如何对其进行编码,以便点击/单击list 开始一个新的活动。我知道这方面的主题已经存在,但似乎我正在以不同的方式设置我的列表视图,因此提供的解决方案不兼容。这是我的 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();
}

}

【问题讨论】:

标签: java android listview


【解决方案1】:

您可以在 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 方法。

【讨论】:

  • 我不认为应该调用onClick(view)。如果ListView 有除TextView 之外的任何其他视图,那将抛出 ClassCastException
  • 哦,没错。我将编辑相应的答案。谢谢。
  • 这个我可能错了,但getContext() 应该是MainActivity.this,因为它在一个匿名类中,而getContext() 不是AdapterView.OnItemClickListener 中的方法
  • getContext() 只是用来替换你想在这种情况下使用的上下文,但是我会改变它。
  • 我需要先实现其他设置代码吗?我无法真正描述为什么我不能让它工作,我在这里是个菜鸟..在 setOnItemClickListener 上得到“无法解析符号”。顺便说一句,感谢您的所有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多