简体   繁体   English

Android onitemclicklistener问题

[英]Android onitemclicklistener issues

Dont know if I am even doing this right as I am new to android programming but I have set an OnItemClickListener to respond to the users listitem selection by beginning a new intent. 不知道我是否正在做这个,因为我是Android编程的新手,但我已经设置了一个OnItemClickListener来通过开始一个新的意图来响应用户列表项目选择。

When the user selects whatever article in the listview they should see the corresponding txt file in the new activity. 当用户选择列表视图中的任何文章时,他们应该在新活动中看到相应的txt文件。

So in the new activity I have tried to find a way to open the corresponding file in the subfolder of the Assets Folder .... 所以在新的活动中我试图找到一种方法来打开资产文件夹子文件夹中的相应文件....

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

    Intent i = getIntent();
    int position = i.getExtras().getInt("position");

    TextView news = (TextView) findViewById(R.id.txtView);
    AssetManager as = getAssets();

    InputStream is;
    try{
        is = as.open(""); <----- !!!
        int bytes = is.available();
        byte[] b = new byte[bytes];
        is.read(b);
        is.close();

        String s = new String (b);
        news.setText(s);
    }catch (IOException e){
        e.printStackTrace();
    }
}

.... however i will only succeed in opening a single txt file. ....但是我只会成功打开一个txt文件。 How can I implement this activity to respond to the OnItemClickListener from the previous activity as shown here... 如何实现此活动以响应上一个活动中的OnItemClickListener,如下所示...

    ls = (ListView) findViewById(R.id.bArt);
    ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int position, long id) {

            Intent i = new Intent (getApplicationContext(), New_Activity.class );
            i.putExtra("id", position);
            startActivity(i);
        }
    });

... to open the correct .txt file from the Assets Folder ...从Assets文件夹中打开正确的.txt文件

I have been stuck on this for a long time now so my Appreciation points (no cash value) to the functional answer will be unending Much Obliged 我已经被困在这一段很长一段时间了所以我对功能性答案的赞赏点(没有现金价值)将是无止境的

Change this 改变这个

 int position = i.getExtras().getInt("id");

Your Id name must same when ever you passing some data between Activity . Activity之间传递一些数据时,您的Id名称必须相同。

Instead of store the position, store the file name inside the intent. 而不是存储位置,将文件名存储在intent中。 Yon can retrieve it with AdapterView.getItemAtPosition Yon可以使用AdapterView.getItemAtPosition检索它

ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> av, View v, int position, long id) {

        Intent i = new Intent (getApplicationContext(), New_Activity.class );
        i.putExtra("id", av.getItemAtPosition(position));
        startActivity(i);
    }
});

In the other activity retrieve the file name and pass this to the AssetsManager 在另一个活动中检索文件名并将其传递给AssetsManager

String  fileName = i.getStringExtra("id");
InputStream is;
    try{
        is = as.open(fileName); <----- !!!
   // other code
   }

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

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