ListView没有扩展ListActivity

时间:2014-03-16 16:44:41

标签: java android listview import

我有一个扩展ActionBarActivity的应用程序,但是我使用ListView并且我需要实现OnItemClickListener,而且我不确定如何在不扩展ListActivity的情况下执行此操作。另外我想问你:此外我有一个按钮,如果单击按钮或单击列表中的项目,我需要收听。我不太清楚如何做到这一点。 如果你帮助我,我将非常感激:)

这是我的代码:

package com.src.vicnote;

import java.io.File;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.os.Build;

public class MainActivity extends ActionBarActivity  {

        Button newButton;

        String path = Environment.getExternalStorageDirectory().toString()+"/VICNote";
        String lastOfPath;
        File f = new File(path);
    File files[] = f.listFiles();
    String[] theNamesOfFiles = new String[files.length + 1];


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

        theNamesOfFiles[0] = "Create new note";

        for(int i = 1; i < theNamesOfFiles.length; i++) {

                lastOfPath = files[i-1].toString().split("/")[files[i-1].toString().split("/").length-1];
                theNamesOfFiles[i] = lastOfPath.replace(".txt","");
                Log.d("Files", "String: " + theNamesOfFiles[i]);
        }

        ListView lv = (ListView) findViewById(R.id.notesList);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                                                                                                                                theNamesOfFiles);
        lv.setAdapter(adapter);

        newButton = (Button) findViewById(R.id.buttonNew);
        newButton.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub

                                Intent newNote = new Intent(MainActivity.this, NewNoteActivity.class);
                                startActivity(newNote);
                        }
                });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

2 个答案:

答案 0 :(得分:2)

您可以实施OnItemClickListener界面并覆盖onItemClick

lv.setAdapter(adapter);
lv.setOnItemClickListener( new OnItemClickListener()
{

    public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Toast.makeText(MainActivity.this.this, "List View row Clicked at"+position,Toast.LENGTH_SHORT).show();
    }
});

如果该按钮属于activity_main.xml,那么你有什么

newButton.setOnClickListener(new View.OnClickListener()

是对的

答案 1 :(得分:1)

您可以使用此代码为ListView设置项目单击侦听器。

lv.setAdapter(adapter);
   lv.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      { 
        // it is used to get the clicked string             
      String theNamesOfFile = adapter.getItem(position);
      }
   });

我想它可以帮到你。