setOnItemClickListener事件在Fragment.my中没有工作setOnItemClickListener在onPostExecute ...请帮助我们...... thnx提前。这是我的代码
HomeFragment.java
public class HomeFragment extends Fragment {
private PagerAdapter mPagerAdapter;
ListView lvHome;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity().getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.build();
ImageLoader.getInstance().init(config);
lvHome = (ListView)view.findViewById(R.id.lvHome);
//Start download
new JSONTask().execute("http://www.darkabc.com/mobile_home.php");
ViewPager pager = (ViewPager) view.findViewById(R.id.viewPager2);
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(getActivity(), ImageOneWelcome.class.getName()));
fragments.add(Fragment.instantiate(getActivity(), ImageTwoWelcome.class.getName()));
fragments.add(Fragment.instantiate(getActivity(), ImageThreeWelcome.class.getName()));
mPagerAdapter = new ImagePagerAdapter(getChildFragmentManager(), fragments);
pager.setAdapter(mPagerAdapter);
return view;
}
private class JSONTask extends AsyncTask<String,String, List<Bardata>> {
@Override
protected List<Bardata> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader= null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer =new StringBuffer();
String line="";
while ((line =reader.readLine()) != null){
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("home");
List<Bardata> bardataList =new ArrayList<>();
Gson gson = new Gson();
for (int i= 0;i<parentArray.length();i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
Bardata bardata = gson.fromJson(finalObject.toString(), Bardata.class);
bardataList.add(bardata);
}
return bardataList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(final List<Bardata> result) {
super.onPostExecute(result);
if(result != null) {
HomeAdapter adapter =new HomeAdapter(getActivity().getApplicationContext(), R.layout.homefragmentrow, result);
lvHome.setAdapter(adapter);
lvHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bardata bardata = result.get(position);
Intent intent = new Intent(getActivity(), Individualbar.class);
intent.putExtra("bardata", new Gson().toJson(bardata));
startActivity(intent);
}
});
} else {
Toast.makeText(getActivity().getApplicationContext(), "Not able to fetch data from server, please check your internet.", Toast.LENGTH_SHORT).show();
}
}
}
public class HomeAdapter extends ArrayAdapter {
private List<Bardata> barDataList;
private int resource;
private LayoutInflater inflater ;
public HomeAdapter(Context context, int resource, List<Bardata> objects) {
super(context, resource, objects);
barDataList = objects;
this.resource =resource;
inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.date1 = (ImageView) convertView.findViewById(R.id.date);
holder.image11 = (ImageView) convertView.findViewById(R.id.image1);
holder.image21 = (ImageView) convertView.findViewById(R.id.image2);
holder.image31 = (ImageView) convertView.findViewById(R.id.image3);
holder.image41 = (ImageView) convertView.findViewById(R.id.image4);
holder.image51 = (ImageView) convertView.findViewById(R.id.image5);
holder.zen1 = (TextView) convertView.findViewById(R.id.zen);
holder.cityname1 = (TextView) convertView.findViewById(R.id.cityname);
holder.rating1 = (TextView) convertView.findViewById(R.id.rating);
holder.text1 = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ImageLoader.getInstance().displayImage(barDataList.get(position).getBar_image(), holder.date1, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
// progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
// progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
// progressBar.setVisibility(View.GONE);
}
});
holder.zen1.setText(barDataList.get(position).getBarname());
holder.cityname1.setText(barDataList.get(position).getLocation());
holder.rating1.setText(barDataList.get(position).getRating());
holder.text1.setText(barDataList.get(position).getAboutbar());
return convertView;
}
class ViewHolder{
ImageView date1;
ImageView image11;
ImageView image21;
ImageView image31;
ImageView image41;
ImageView image51;
TextView zen1;
TextView cityname1;
TextView rating1;
TextView text1;
}
}
}
fragment_home.xml
<FrameLayout 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:clickable="true"
android:orientation="vertical"
tools:context="com.example.abhijeetsingh.drawer.HomeFragment">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="170dp"
android:elevation="1dp"
android:id="@+id/viewPager2">
</android.support.v4.view.ViewPager>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="170dp"
android:id="@+id/lvHome">
</ListView>
homefragmentrow.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@id/relative1"
android:layout_marginTop="7dp"
android:background="@drawable/customborder">
<ImageView
android:layout_width="40dp"
android:layout_height="30dp"
android:id="@+id/date"
android:paddingLeft="5dp"
android:layout_marginTop="10dp"
android:src="@drawable/tgif"
android:scaleType="fitXY"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="10dp"
android:id="@+id/line"
android:layout_below="@+id/date"
android:paddingLeft="5dp"
android:layout_marginTop="10dp"
android:src="@drawable/line"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TGIF"
android:textSize="15dp"
android:id="@+id/zen"
android:textColor="#EF9C1A"
android:paddingTop="10dp"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/date"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cityname"
android:layout_toRightOf="@+id/zen"
android:text="| B-25, Safdarjung, New Delhi"
android:layout_marginLeft="10dp"
android:paddingTop="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="@+id/rating"
android:layout_below="@+id/line"
android:paddingLeft="5dp"
android:textColor="#000"
android:layout_marginTop="10dp"
android:text="Rated"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/ratingimage"
android:layout_below="@+id/line"
android:layout_marginTop="10dp"
android:layout_marginLeft="2dp"
android:src="@drawable/rated"
android:layout_toRightOf="@+id/rating"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ratingimage"
android:layout_below="@+id/line"
android:id="@+id/text"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:textSize="15dp"
android:text="It is a long established fact../>
<ImageView
android:id="@+id/image1"
android:layout_width="66dp"
android:layout_height="70dp"
android:scaleType="fitXY"
android:padding="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:layout_below="@+id/text"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>
</ScrollView>
答案 0 :(得分:1)
你已经使得FrameLayout Clickable窃取了clickEvent并且Kistview无法调用OnItemClick方法。
机器人:可点击= “真”
从FrameLayout中删除上面的行。 它应该工作。
答案 1 :(得分:1)
在你定义AsyncTask之外的onItemClick。
答案 2 :(得分:1)
试试这个。
public class HomeAdapter extends ArrayAdapter {
private List<Bardata> barDataList;
private int resource;
private LayoutInflater inflater ;
public HomeAdapter(Context context, int resource, List<Bardata> objects) {
super(context, resource, objects);
barDataList = objects;
this.resource =resource;
inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.date1 = (ImageView) convertView.findViewById(R.id.date);
holder.image11 = (ImageView) convertView.findViewById(R.id.image1);
holder.image21 = (ImageView) convertView.findViewById(R.id.image2);
holder.image31 = (ImageView) convertView.findViewById(R.id.image3);
holder.image41 = (ImageView) convertView.findViewById(R.id.image4);
holder.image51 = (ImageView) convertView.findViewById(R.id.image5);
holder.zen1 = (TextView) convertView.findViewById(R.id.zen);
holder.cityname1 = (TextView) convertView.findViewById(R.id.cityname);
holder.rating1 = (TextView) convertView.findViewById(R.id.rating);
holder.text1 = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Bardata data = barDataList.get(position);
convertView.setOnClickListener(new Listener(data))
ImageLoader.getInstance().displayImage(data.getBar_image(), holder.date1, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
// progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
// progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
// progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
// progressBar.setVisibility(View.GONE);
}
});
holder.zen1.setText(barDataList.get(position).getBarname());
holder.cityname1.setText(barDataList.get(position).getLocation());
holder.rating1.setText(barDataList.get(position).getRating());
holder.text1.setText(barDataList.get(position).getAboutbar());
return convertView;
}
class Listener implements View.OnClickListener {
private Bardata data;
Listener(Bardata data) {
this.data = data;
}
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Individualbar.class);
intent.putExtra("bardata", new Gson().toJson(data));
startActivity(intent);
}
}
class ViewHolder{
ImageView date1;
ImageView image11;
ImageView image21;
ImageView image31;
ImageView image41;
ImageView image51;
TextView zen1;
TextView cityname1;
TextView rating1;
TextView text1;
}
}
答案 3 :(得分:0)
在AsyncTask之外执行此操作。
lvHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bardata bardata = result.get(position);
Intent intent = new Intent(getActivity(), Individualbar.class);
intent.putExtra("bardata", new Gson().toJson(bardata));
startActivity(intent);
}
});
} else {
Toast.makeText(getActivity().getApplicationContext(), "Not able to fetch data from server, please check your internet.", Toast.LENGTH_SHORT).show();
}
答案 4 :(得分:0)
这个答案是针对评论中提出的其他问题:
如果您正在进行GET
来电,请将参数添加到网址/get_call?parameter_1=value_1¶meter_2=value_2
。如果它是POST
或PUT
,您可以OutputStreamWriter
使用BufferedWriter
将参数设置为网址。
示例:
OutputStreamWriter out = null;
try {
out = new OutputStreamWriter(con.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
BufferedWriter bw = null;
if (out != null) {
bw = new BufferedWriter(out);
} else {
// Connection Error
}
try {
bw.write(parametersAsString);
bw.flush();
bw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
您可以使用JSONObject
放置参数和值,并使用parametersAsString
函数将其替换为toString()
替换