所以我在里面有一个Listview里面的片段,这非常好用。
但我无法弄清楚如何在片段中使用onItemClickListener。
我可能需要一些帮助将它放在Fragmentcode中。
我已经找到了Thread,但我仍然无法让它工作
FragmentJanuar.java
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class FragmentJanuar extends Fragment {
private ArrayList<meetings> myMeetings = new ArrayList<meetings>();
private ContactsAdapter adapter;
private ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
View v = inflater.inflate(R.layout.fragment_januar, container, false);
myMeetings.add(new meetings("Event 1", "01.01.2001", "01:00", "1€", "Ort 1", "Strasse 1", 0, "www.event1url.de", "http://www.maplinkevent1.com"));
myMeetings.add(new meetings("Event 2", "01.01.2001", "01:00", "1€", "Ort 1", "Strasse 1", 0, "www.event1url.de", "http://www.maplinkevent1.com"));
lv =(ListView)v.findViewById(R.id.listview_januar);
adapter = new ContactsAdapter(getActivity(), myMeetings);
lv.setAdapter(adapter);
return v;
}
private class ContactsAdapter extends BaseAdapter {
private FragmentActivity activity;
private LayoutInflater inflater;
private ArrayList<meetings> data;
public ContactsAdapter (FragmentActivity a, ArrayList<meetings> d){
activity = a;
data = d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public meetings getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
v = inflater.inflate(R.layout.item_view, null);
meetings currentMeeting = myMeetings.get(position);
//Eventname
TextView Event = (TextView) v.findViewById(R.id.item_txtEvent);
Event.setText(currentMeeting.getEventname());
//date
TextView Date = (TextView) v.findViewById(R.id.item_txtDate);
Date.setText(currentMeeting.getDate());
//Time
TextView Time = (TextView) v.findViewById(R.id.item_txtTime);
Time.setText("Uhrzeit: " + currentMeeting.getTime() + "Uhr" );
//Price
TextView Price = (TextView) v.findViewById(R.id.item_txtPrice);
Price.setText("Eintritt: "+ currentMeeting.getPrice());
//LocationOrt
TextView LocationOrt = (TextView) v.findViewById(R.id.item_txtLocationOrt);
LocationOrt.setText("Location: " + currentMeeting.getLocationOrt());
//LocationStrasse
TextView LocationStrasse = (TextView) v.findViewById(R.id.item_txtLocationStrasse);
LocationStrasse.setText(currentMeeting.getLocationStrasse());
//EventUrl
TextView Eventurl = (TextView) v.findViewById(R.id.item_txtEventurl);
Eventurl.setText("Url: " + currentMeeting.getEventurl());
return v;
}
}
}
这是我要插入的onItemClickListener。
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String myurl = myMeetings.get(position).getMapslink().toString();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(myurl));
startActivity(i);
return;
}
});
答案 0 :(得分:0)
R.layout.item_view
给根视图提供id,即在顶层中查看上面布局的视图
public View getView
然后在上面的方法中注册一个点击监听器
例如。 rootView.setOnCLickListenere(this)
覆盖onClick()
activity.startActivity(&#34; XYZ&#34; ABC&#34)