我想通过使用PullToRefreshListView类向上拖动来更新我的列表视图。当我使用自己的ListView BaseAdapter时,Click Listener无法正常工作。
mycode的:
MainActivity.java
package com.androidsurya.pulltorefresh;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import com.androidsurya.customviews.PullToRefreshListView;
import com.androidsurya.customviews.PullToRefreshListView.OnRefreshListener;
public class MainActivity extends ListActivity {
private ArrayList<String> mListItems;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pulltorefresh);
// Set a listener to be invoked when the list should be refreshed.
((PullToRefreshListView) getListView())
.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
mListItems = new ArrayList<String>();
mListItems.addAll(Arrays.asList(mStrings));
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mListItems);
MyListAdapter adapter = new MyListAdapter(MainActivity.this, MainActivity.this, mListItems);
setListAdapter(adapter);
}
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void... params) {
// Simulates a background job.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
mListItems.add(0, "Added new item after refresh...");
// Call onRefreshComplete when the list has been refreshed.
((PullToRefreshListView) getListView()).onRefreshComplete();
super.onPostExecute(result);
}
}
private String[] mStrings = { "Andaman and Nicobar Islands",
"Andhra Pradesh", "Arunachal Pradesh", "Assam", "Bihar",
"Chhattisgarh", "Goa", "Gujarat", "Haryana", "Himachal Pradesh",
"Jammu and Kashmir", "Jharkhand", "Karnataka", "Kerala",
"Madhya Pradesh", "Maharashtra", "Manipur" }; }
MyListAdapter.java
package com.androidsurya.pulltorefresh;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MyListAdapter extends BaseAdapter{
private ArrayList<String> list;
private Context context;
private Activity activity;
public MyListAdapter(Activity activity,Context context, ArrayList<String> list) {
this.activity = activity;
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return list.size();
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
LayoutInflater inflator = activity.getLayoutInflater();
arg1 = inflator.inflate(R.layout.test_list, null);
TextView txt = (TextView) arg1.findViewById(R.id.list_text);
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(activity, "Clicked "+arg0, Toast.LENGTH_LONG).show();
}
});
return arg1;
}
}
这是上面的代码,我试图实现txt.setOnClickListener无法正常工作。
请让我知道一些可能的解决方案。
答案 0 :(得分:0)
我找到了答案。并将您的位置int arg0
更改为final int arg0
仅供检查。
arg2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("Position", "positon : " + arg0);
}
});
问候。