使用extends AsyncTask单击动态ListAdapter列表(TextView)

时间:2016-08-19 09:44:49

标签: android listview arraylist android-asynctask

您好我需要textview点击事件

我使用cursor:

从数据库加载列表数据

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

此外,数据库获取事件通过使用完成 扩展AsyncTask

tabLayout下的所有上述任务 - &gt; MyListFragment

我在AsctivityCreated(Bundle savedInstanceState)上将公共虚空中的Asynctask称为

当我尝试编写onClick列表项时没有任何事情发生,所以请找到列表文本点击事件,稍后我将点击

打开一个新片段

Need Text click

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>

2 个答案:

答案 0 :(得分:0)

要检测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);
}

答案 1 :(得分:0)

试试此代码

@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;

}