0
所以我得到了我的片段在這裏與一個ListView裏面,這是工作的相當不錯。列表視圖onItemClickListener
但我不知道如何使用片段內的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;
}
});
你想點擊列表中的項目?? – 2014-12-05 13:24:31
是的,這就是我想要的聽衆。 – Ovi 2014-12-05 13:39:52