大家好我正在为Android 4.3开发这个应用程序,而且它是一个短信阻止应用程序。我正在使用一个活动而休息是片段。现在我有两个列表片段,一个是正常出现的blacklistfragment,但其他blockedlistfragment是空白的。我设置了一些断点,数据正在加载适配器但没有片段的输出没有错误所以我在这里感到困扰。如果有人可以指出我做错了什么或者缺少什么,那将会很棒。)
这些是我的文件:
==== BlockedlistFragment.java ===
package com.studio.shahz.ultimatesmsblocker;
import android.app.ListFragment;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.studio.shahz.ultimatesmsblocker.adapters.BlockedListArrayAdapter;
import com.studio.shahz.ultimatesmsblocker.data.BlacklistData;
import com.studio.shahz.ultimatesmsblocker.data.Blockedlist;
import com.studio.shahz.ultimatesmsblocker.data.BlockedlistData;
import java.util.List;
/**
* Created by Shahz on 8/22/2016.
*/
public class BlockedListFragment extends android.support.v4.app.ListFragment{
List<Blockedlist> blockedlist;
public BlockedListFragment() throws SQLException {
blockedlist = new BlockedlistData().getBlockedlist();
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
container.removeAllViews();
View rootView = inflater.inflate(R.layout.fragment_blockedlist, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Log.d("SHAHZ","finally yarr");
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BlockedListArrayAdapter adapter = new BlockedListArrayAdapter(getActivity(),
R.layout.blockedlist_listitem,
blockedlist);
setListAdapter(adapter);
}
@Override
public void onStart(){
super.onStart();
Log.d("Blocked Status: ","It is started now");
}
@Override
public void onStop(){
super.onStop();
Log.d("Status: ","It is stopped now...");
// ((MainActivity) getActivity()).loadBlacklist();
}
}
=== BlockedlistArrayAdapter.java ===
package com.studio.shahz.ultimatesmsblocker.adapters;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.studio.shahz.ultimatesmsblocker.MainActivity;
import com.studio.shahz.ultimatesmsblocker.R;
import com.studio.shahz.ultimatesmsblocker.data.Blockedlist;
import com.studio.shahz.ultimatesmsblocker.data.DBHandler;
import java.util.List;
/**
* Created by Shahz on 8/24/2016.
*/
public class BlockedListArrayAdapter extends ArrayAdapter<Blockedlist>{
private Context context;
private List<Blockedlist> objects;
// DBHandler db;
public BlockedListArrayAdapter(Context context, int resource, List<Blockedlist> objects) {
super(context, resource);
this.context = context;
// db=DBHandler.getInstance(this.context);
this.objects = objects;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final Blockedlist blocklist = objects.get(position);
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.blockedlist_listitem, null);
TextView textBlockedId=(TextView) view.findViewById(R.id.blocked_id);
textBlockedId.setText(blocklist.getBlockedId());
TextView textBlacklistId=(TextView) view.findViewById(R.id.blacklist_id);
textBlacklistId.setText(blocklist.getBlacklistId());
TextView textMessageCount=(TextView) view.findViewById(R.id.message_count);
textMessageCount.setText(blocklist.getBlockedCount());
TextView textBlockedName=(TextView) view.findViewById(R.id.blocked_name);
textBlockedName.setText(blocklist.getContactName());
TextView textBlockedNumber=(TextView) view.findViewById(R.id.blocked_number);
textBlockedNumber.setText(blocklist.getBlacklistNumber());
Log.d("shahz",blocklist.getBlockedId());
Log.d("shahz",blocklist.getBlacklistId());
Log.d("shahz",blocklist.getBlacklistNumber());
Log.d("shahz",blocklist.getBlockedCount());
Log.d("shahz",blocklist.getContactName());
// view.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Log.d("Blocked message : ",String.valueOf(blocklist.getBlockedCount()));
//// ((MainActivity)context).loadEditContactFragment(blocklist.getId(),blacklist.getName(),blacklist.getNumber());
// }
// });
return view;
}
}
=== BlockedlistData.java ===
package com.studio.shahz.ultimatesmsblocker.data;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Shahz on 8/25/2016.
*/
public class BlockedlistData {
private Cursor result;
private List<Blockedlist> blockedlist= new ArrayList<Blockedlist>();
public List<Blockedlist> getBlockedlist() {
return blockedlist;
}
public BlockedlistData() throws SQLException {
DBHandler handler=DBHandler.getInstance(null);
handler.removeExpiredMessages();
result = handler.getBlockedList();
if(result!=null && result.moveToFirst()){
do {
// count=handler.getBlockedCount(Integer.valueOf(result.getString(1)));
blockedlist.add(new Blockedlist(result.getString(0),result.getString(1),result.getString(2),result.getString(3),result.getString(4)));
Log.d("Results are: ",result.getString(0)+" "+result.getString(1)+" "+result.getString(2)+" "+result.getString(3)+" "+result.getString(4));
}while (result.moveToNext());
}
result.close();
}
}
=== Blocklist.java ===
package com.studio.shahz.ultimatesmsblocker.data;
import android.os.Bundle;
/**
* Created by Shahz on 8/19/2016.
*/
public class Blockedlist{
// constants for field references
public static final String BLOCKED_ID="1";
public static final String BLACKLIST_ID = "count";
public static final String BLACKLIST_NUMBER="number";
public static final String CONTACT_NAME = "name";
public static final String BLOCKED_COUNT="50";
// private fields
private String blocked_id;
private String blacklist_id;
private String blacklist_number;
private String contact_name;
private String blocked_count;
// getters and setters
public String getBlockedId(){
return blocked_id;
}
public void setBlockedId(String id){
this.blocked_id=id;
}
public String getBlacklistId(){
return blacklist_id;
}
public void setBlacklistId(String id){
this.blacklist_id=id;
}
public String getBlacklistNumber(){
return blacklist_number;
}
public void setBlacklistNumber(String number){
this.blacklist_number=number;
}
public String getContactName(){
return contact_name;
}
public void setContactName(String name){
this.contact_name=name;
}
public String getBlockedCount(){
return blocked_count;
}
public void setBlockedCount(String count){
this.blocked_count=count;
}
// Used when creating the data object
public Blockedlist(String id,String blacklistId,String number,String name,String count) {
this.blocked_id=id;
this.blacklist_id=blacklistId;
this.blacklist_number=number;
this.contact_name=name;
this.blocked_count=count;
}
// Create from a bundle
public Blockedlist(Bundle b) {
if (b != null) {
this.blocked_id=b.getString(BLOCKED_ID);
this.blacklist_id=b.getString(BLACKLIST_ID);
this.blacklist_number=b.getString(BLACKLIST_NUMBER);
this.contact_name=b.getString(CONTACT_NAME);
this.blocked_count=b.getString(BLOCKED_COUNT);
}
}
// Package data for transfer between activities
public Bundle toBundle() {
Bundle b = new Bundle();
b.putString(BLOCKED_ID,this.blocked_id);
b.putString(BLACKLIST_ID,this.blacklist_id);
b.putString(BLACKLIST_NUMBER,this.blacklist_number);
b.putString(CONTACT_NAME,this.contact_name);
b.putString(BLOCKED_COUNT,this.blocked_count);
return b;
}
// Output flower data
@Override
public String toString() {
return contact_name;
}
}
另外,我用于获取数据的查询如下。它可以工作,并可以从这里记录适配器中的数据:
public Cursor getBlockedList(){
Cursor result;
SQLiteDatabase db=this.getReadableDatabase();
String query="SELECT b.id, b.blocked_id, l.phone_number,l.contact_name,count(b.id) " +
"FROM blockedmessages b, blacklist l WHERE b.blocked_id = l.id GROUP BY b.blocked_id";
result=db.rawQuery(query,null);
return result;
}
感谢您的时间:)