【发布时间】: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 新手,您可以发布一个示例 ans 而不是建议
标签: android listview arraylist android-asynctask