|
|
|
[Android Studio] ช่วยหน่อยครับ พอดีอยากเอา ListActivity ไปไว้ใน Fragment อะครับ ลองแก้หลายทางแล้วติด Error ตลอดเลยครับ |
|
|
|
|
|
|
|
สอบถามหน่อยครับ หากผมต้องการจะเปลี่ยนจาก ListActivity ของ Code นี้เป็นหน้า Fragment ต้องแก้ตรงไหนบ้างอะครับ พอทำแล้วหลังๆจะ
รบกวนพี่ๆช่วยดูให้หน่อยนะครับ อ่านมาหลายอันมาก ยังแก้ไม่ได้อยู่อะครับ ขอบคุณครับ
ติดตรงนี้
Code (Android-Java)
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
R.id.tvNameMain, conNames));
พอเปลี่ยนเป็นก็ไม่ได้เหมือนเดิม
Code (Android-Java)
mListView.setAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.tvNameMain, conNames));
โค๊ดทั้งหมดครับ
Code (Android-Java)
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class Recents extends ListActivity {
private ListView mListView;
private ArrayList<String> conNames;
private ArrayList<String> conNumbers;
private ArrayList<String> conTime;
private ArrayList<String> conDate;
private ArrayList<String> conType;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recents);
mListView = (ListView) findViewById(R.id.list);
conNames = new ArrayList<String>();
conNumbers = new ArrayList<String>();
conTime = new ArrayList<String>();
conDate = new ArrayList<String>();
conType = new ArrayList<String>();
Cursor curLog = getAllCallLogs(getContentResolver());
setCallLogs(curLog);
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
R.id.tvNameMain, conNames));
// Set onclicklistener to the list item.
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
ArrayList<String> conNames) {
super(context, resource, textViewResourceId, conNames);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = setList(position, parent);
return row;
}
private View setList(int position, ViewGroup parent) {
LayoutInflater inf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inf.inflate(R.layout.liststyle, parent, false);
TextView tvName = (TextView) row.findViewById(R.id.tvNameMain);
TextView tvNumber = (TextView) row.findViewById(R.id.tvNumberMain);
TextView tvTime = (TextView) row.findViewById(R.id.tvTime);
TextView tvDate = (TextView) row.findViewById(R.id.tvDate);
TextView tvType = (TextView) row.findViewById(R.id.tvType);
tvName.setText(conNames.get(position));
tvNumber.setText(conNumbers.get(position));
tvTime.setText("( " + conTime.get(position) + "sec )");
tvDate.setText(conDate.get(position));
tvType.setText("( " + conType.get(position) + " )");
return row;
}
}
private void setCallLogs(Cursor curLog) {
while (curLog.moveToNext()) {
String callNumber = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
conNumbers.add(callNumber);
String callName = curLog
.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
if (callName == null) {
conNames.add("Unknown");
} else
conNames.add(callName);
String callDate = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.DATE));
SimpleDateFormat formatter = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm");
String dateString = formatter.format(new Date(Long
.parseLong(callDate)));
conDate.add(dateString);
String callType = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.TYPE));
if (callType.equals("1")) {
conType.add("Incoming");
} else
conType.add("Outgoing");
String duration = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.DURATION));
conTime.add(duration);
}
}
public static Cursor getAllCallLogs(ContentResolver cr) {
// reading all data in descending order according to DATE
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Uri callUri = Uri.parse("content://call_log/calls");
Cursor curCallLogs = cr.query(callUri, null, null, null, strOrder);
return curCallLogs;
}
}
AndroidMainfest เพิ่ม 2 อันนี้เรียบร้อยแล้ว
Code (Android-Java)
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
Tag : Mobile, Android
|
|
|
|
|
|
Date :
2016-10-24 00:54:46 |
By :
nomzod |
View :
872 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอนนี้ติดปัญหาอะไรครับ
|
|
|
|
|
Date :
2016-10-26 12:37:40 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|