如何处理 Listview 中的单击事件?

Posted

技术标签:

【中文标题】如何处理 Listview 中的单击事件?【英文标题】:How can I handle a click event in Listview? 【发布时间】:2021-08-16 09:43:58 【问题描述】:

我的源代码是关于通过爬行制作带有标题的列表。 单击标题,作为单击事件,移动到另一个页面以显示有关标题的内容。 但是点击事件(更改页面)不起作用,我找不到原因。 我是android编程新手,希望能得到一些建议..!

*Main_Activity.java

package com.example.listview_prac;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;


public class MainActivity extends AppCompatActivity 
    ListView listView;
    String title; //title
    //Vector<String> title_str = new Vector<String>(15);
    Vector<String> content_arr = new Vector<String>(15);

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

        listView = findViewById(R.id.list_view);
        listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, new ArrayList<String>()));
        new MyTask().execute();
    

    class MyTask extends AsyncTask<Void, String, String>
    
        ArrayAdapter<String> adapter;
        @Override
        protected void onPreExecute() 
            adapter = (ArrayAdapter<String>)listView.getAdapter();
        

        @Override
        protected String doInBackground(Void... voids)  
            final StringBuilder builder = new StringBuilder();
            String url1 = "https://";
            String my_link = null;

            try 
                Connection con = Jsoup.connect(url1);
                Document doc = con.get();

                Elements mElementDatas = doc.select("#body_line > nobr ");
                String text = mElementDatas.text();

                int i=0;
                for (Element elem : mElementDatas)  
                    String my_title = elem.select("a span b").text();
                    title = my_title;
                    publishProgress(title);

                   
                    my_link = elem.select("a").attr("href");
                    String url2 = my_link;
                    Connection con2 = Jsoup.connect("https://" + url2);
                    Document doc2 = con2.get();

                    Elements EDatas = doc2.select("#articles"); 

                    for (Element elem2 : EDatas) 
                        // span
                        String content1 = elem2.select("span").text();
                        String content2 = elem2.select("p").text();
                        String content = content1 + content2;

                        content_arr.add(content1 + content2);  //Is this wrong?

                        
                    

                    i++;
                    if(i>15) 
                        break;
                    
                
             catch (IOException e) 
                builder.append("Error");
            

            return "All the datas were added successfully";
        

        @Override
        protected void onProgressUpdate(String... values) 
            adapter.add(values[0]);


        

        @Override
        protected void onPostExecute(String result) 

            //content_arr.add(result);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) 
                    Toast.makeText(MainActivity.this, "Clicked! ", Toast.LENGTH_SHORT).show();
                    //String c_list = content_arr.get(position); 
                    Intent intent = new Intent(MainActivity.this, Clicked.class);
                    intent.putExtra("arr_text", content_arr.get(position));
                    startActivity(intent);

                    
                
            );


        
    

*Clicked.java

package com.example.listview_prac;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Clicked extends Activity 
    TextView textView;

    protected void onCreate(Bundle savedInstanceState) 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.clicked);
        textView = (TextView)findViewById(R.id.textView);

        Bundle bundle = getIntent().getExtras();
        String text = bundle.getString("arr_text");

        textView.setText(text);

    


这些是错误信息

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.listview_prac, PID: 6750
    android.content.ActivityNotFoundException: Unable to find explicit activity class com.example.listview_prac/com.example.listview_prac.Clicked; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2065)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
        at android.app.Activity.startActivityForResult(Activity.java:5320)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
        at android.app.Activity.startActivityForResult(Activity.java:5278)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
        at android.app.Activity.startActivity(Activity.java:5664)
        at android.app.Activity.startActivity(Activity.java:5617)
        at com.example.listview_prac.MainActivity$MyTask$1.onItemClick(MainActivity.java:118)
        at android.widget.AdapterView.performItemClick(AdapterView.java:330)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1187)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3179)
        at android.widget.AbsListView$3.run(AbsListView.java:4097)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

我删除了 Main_Acitivity.java 的 url,但如果你需要,我可以把它放回去

【问题讨论】:

【参考方案1】:

您是否在 AndroidManifest.xml 中声明了此活动?

AndroidManifest 文件中的点击活动声明。

【讨论】:

omg...我很惭愧..呵呵..非常感谢!!

以上是关于如何处理 Listview 中的单击事件?的主要内容,如果未能解决你的问题,请参考以下文章

如何处理android中Listview中的点击事件?

如何处理 WPF 中的鼠标滚轮单击事件?

当 ListView 有 ItemTemplate 时如何处理 ListViewItem 的右键单击?

如何处理动态填充 ul li 的点击事件?

如何处理数据表中的复选框单击事件

我们如何处理 Xamarin Forms Picker 的完成按钮单击事件?