【问题标题】:Dynamic ListAdapter List (TextView) click with extends AsyncTask动态 ListAdapter 列表(TextView)单击扩展 AsyncTask
【发布时间】:2016-08-19 09:44:48
【问题描述】:

嗨,我需要 textview 点击事件

我使用游标从数据库中加载列表数据:

HashMap<String, String> map = new HashMap<String, String>();

也是通过使用完成的数据库获取事件 扩展 AsyncTask

一个tabLayout下的所有上述任务 -> MyListFragment

我在 public void onActivityCreated(Bundle savedInstanceState) 调用 Asynctask

当我尝试写onClick List Item 时没有发生任何事情,所以请找出列表文本点击事件,稍后我将在点击时打开一个新片段

StatusFragment.java

public class StatusFragment extends ListFragment  {


// Progress Dialog
private ProgressDialog pDialog;
private JSONArray mComments = null;
private ArrayList<HashMap<String, String>> mCommentList;

private static final String TAG_CAT = "cat";

public StatusFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.activity_list, container, false);
    return rootView;

}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Intent intent = getActivity().getIntent();
    new Loadfromdb().execute();

}
public class Loadfromdb extends AsyncTask<Void, Void, Boolean> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected Boolean doInBackground(Void... arg0) {

        addlist();
        return null;

    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        updateList();

    }
}

private void updateList() {

    ListAdapter adapter = new SimpleAdapter(getActivity(), mCommentList,
            R.layout.fragment_status, new String[] {
            TAG_CAT}, new int[] {
            R.id.categ });

    // Toast.makeText(getActivity(), "Am Clicked", Toast.LENGTH_LONG).show();

    setListAdapter(adapter);


}


public void addlist() {

    mCommentList = new ArrayList<HashMap<String, String>>();

    DatabaseHandler myDbHelper = new DatabaseHandler(getActivity());
    myDbHelper.open();

    Cursor c = myDbHelper.fetchOption();

    if(c!=null ){
        c.moveToFirst();
        while(  c.isAfterLast() == false )
        {
            HashMap<String, String> map = new HashMap<String, String>();
            String des = c.getString(0).toString();
            map.put(TAG_CAT, des);
            mCommentList.add(map);
            c.moveToNext();
        }
    }
}

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Toast.makeText(getActivity(), "Am Clicked", Toast.LENGTH_LONG).show();
    }
}

activity_list.xml

<!-- language: lang-xml -->
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:divider="@drawable/list_divider"
        android:scrollbars="none" />
</RelativeLayout>

fragment_status.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:stretchColumns="0,1">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:gravity="center_vertical"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/categ"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="left"
        android:gravity="center_vertical" />

</LinearLayout>
</ScrollView>

【问题讨论】:

标签: android listview arraylist android-asynctask


【解决方案1】:

要检测对 listView 行的点击而不是使用 onClickListener,您需要使用 onItemClickListener

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Add action here: e.g. open new fragment.            
    }
});

由于您使用的是ListFragment,因此您可以覆盖onListItemClick()

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
}

【讨论】:

  • 这段代码放在哪里?你看到我的 StatusFragment.java
  • 我已经为您的实施编辑了我的答案。第二部分将是您所需要的。
  • 你能用实现更新你的代码示例吗?你有任何错误吗?
  • 我没有使用自定义适配器
  • 我用你的代码更新了帖子 chk it...仍然无法正常工作
【解决方案2】:

试试这个代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.activity_list, container, false);
    ((ListView)rootView.findViewById(android.R.id.list)).setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Log.d(LOG_TAG, "itemClick: position = " + position + ", id = "
                    + id);
        }
    });
    return rootView;

}

【讨论】:

  • 找不到 OnItemClickListener 所以我导入 AdapterView.OnItemClickListener();
  • 另外它给了我以下错误“java.lang.IllegalStateException: Content view not yet created” on this line“getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {"
  • 也可以写成 ((ListView)rootView.fingViewById(R.id.list)).setOnItemClickListener(...)
  • 当您尝试 find listView by id 时遇到什么错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
相关资源
最近更新 更多