2016-08-05 42 views
0

我的应用程序中有一项服务和一项活动。服务通过用户的操作获取字符串,并将该字符串传递给活动。在这里,我将成功获取字符串值并显示在活动屏幕上。在列表视图中显示字符串数据作为字符串值每隔yime更改

我有一个活动的列表视图和字符串值应显示在该列表中有序。我的问题是,我只能在最近输入的列表视图中显示一个项目。之前输入的字符串不显示。

所以每次我必须从服务获取字符串,该字符串应该显示在列表视图以及以前的列表项目。 请帮助我..谢谢。

我的活动是...

public class MainActivity extends AppCompatActivity { 


String pichi; 
SharedPreferences pref=null; 
ListView list; 
private ArrayList<String> strArray; 
int i; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    list=(ListView)findViewById(R.id.offlineList); 

    pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
    pichi=pref.getString("KEY","default"); 

    strArray=new ArrayList<String>(); 
    if (strArray.contains(pichi)){ 
     Toast.makeText(getApplicationContext(),"Link already exists",Toast.LENGTH_LONG).show(); 
    }else { 
     int count = list.getCount(); 
     for (i = 0; i < count; i++) { 
      strArray.add("Row" + i); 
     } 
     final ArrayAdapter<String> adapter = new ArrayAdapter<String> 
       (MainActivity.this, android.R.layout.simple_list_item_1, strArray); 
     if (!list.equals(i)) { 
      strArray.add(pichi); 
      adapter.notifyDataSetChanged(); 
     } 
     list.setAdapter(adapter); 
    } 

} 

public void startService(View view) { 
    startService(new Intent(MainActivity.this, CheckService.class)); 

} 

}

其中startService(查看视图)是按下按钮的动作。

我的服务是....

public class CheckService extends Service { 


public CheckService() { 
} 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO: Return the communication channel to the service. 
    throw new UnsupportedOperationException("Not yet implemented"); 
} 

String a; 
Intent dialogIntent; 

public void onCreate() { 
    super.onCreate(); 

} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.i("LocalService", "Received Start id" + startId + ":" + intent); 

    final ClipboardManager clipboard = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE); 
    clipboard.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() { 
     public void onPrimaryClipChanged() { 
      a = clipboard.getText().toString(); 
      if (Patterns.WEB_URL.matcher(a).matches()){ 

       dialogIntent = new Intent(CheckService.this, DialogActivity.class); 
       dialogIntent.putExtra("text",a); 
       dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(dialogIntent); 
     } 
      else if (!URLUtil.isValidUrl(a)){ 
      // Toast.makeText(CheckService.this, "text is not a url link", Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 
    return super.onStartCommand(intent, flags, startId); 

} 


@Override 
public void onStart(Intent intent, int startId) { 
    // Perform your long running operations here. 
// Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onDestroy() { 
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 
    stopService(dialogIntent); 
} 

}

在服务我从那里我可以得到的字符串值警告对话框...

我的对话活动是.....

public class DialogActivity extends Activity{ 

AlertDialog alert; 
String receivedText; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Bundle bundle=getIntent().getExtras(); 
    receivedText=bundle.getString("text","default"); 

    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder 
      .setTitle("yoLobee") 
      .setMessage("save to yoLobee offline..?") 
      .setIcon(R.drawable.logo) 
      .setCancelable(true) 
      .setNeutralButton("SHARE", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        Intent sendIntent = new Intent(); 
        sendIntent.setAction(Intent.ACTION_SEND); 
        sendIntent.putExtra(Intent.EXTRA_TEXT, receivedText); 
        sendIntent.setType("text/plain"); 
        startActivity(Intent.createChooser(sendIntent,receivedText)); 
       } 
      }) 
      .setNegativeButton("NO", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        alert.dismiss(); 
        alert.dismiss(); 
        builder.setCancelable(true); 
       } 
      }) 
      .setPositiveButton("YES", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Toast.makeText(DialogActivity.this, "Link saved to yoLobee..", Toast.LENGTH_LONG).show(); 
        SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
        SharedPreferences.Editor edit = pref.edit(); 
        edit.putString("KEY",receivedText); 
        edit.commit(); 
       //  edit.apply(); 

       } 
      }); 

    alert = builder.create(); 
    alert.setCanceledOnTouchOutside(true); 
    setFinishOnTouchOutside(true); 
    alert.setCancelable(true); 
    alert.show(); 
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
    lp.copyFrom(alert.getWindow().getAttributes()); 
    lp.width = WindowManager.LayoutParams.MATCH_PARENT; 
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
    Window window = alert.getWindow(); 
    WindowManager.LayoutParams wlp = window.getAttributes(); 
    wlp.gravity = Gravity.TOP; 
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND; 
    window.setAttributes(wlp); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (event.getAction() == KeyEvent.ACTION_DOWN) { 
     alert.cancel(); 
     alert.dismiss(); 
    } 
    return super.onKeyDown(keyCode, event); 
} 

@Override 
public void onBackPressed() { 
    super.onBackPressed(); 
    alert.cancel(); 
    alert.dismiss(); 
} 

}

所以从对话框活动中我得到的字符串值并显示在列表中。 该列表仅显示来自服务的最近字符串值,因为它一次只获取一个值。我希望列表显示从服务中获取的字符串列表。

+0

请张贴您的适配器代码... – Kushan

+0

我没有任何适配器代码...我使用阵列适配器作为适配器来显示项目在列表视图中。因为我想适配器是在列表中显示数组数据,我需要阵列数据工作正常。 –

+0

我没有太多的知识,如果有任何变化,我必须让PLZ建议我。 –

回答

0

我觉得这条线int count = list.getCount();onCreate返回0因为你没有添加任何项目list到目前为止。

+0

是的,我没有添加项目列表,这是我的问题。每次如果我将字符串传递给strArray,它将只显示该项目。所以最上面的列表将只包含一个项目。如何从我传递的值中创建字符串数组列表。 –

+0

检查http://stacktips.com/tutorials/android/creating-a-background-service-in-android。如果你想使用服务和更新列表,所以你也应该使用接收器。 – faranjit

0

如果您提供一段代码来查看它将会很有帮助。 读你的问题似乎你已经在写你正在用来填充列表的数组了。

只需从活动中获取字符串并将其添加到您正在用来显示listview并调用notifyDataSetChanged()的数组中。它会工作

+0

我也试着调用notifyDataSetChanged()。它没有工作。如何添加数组中的字符串以显示listview,并且每次输入新字符串时,该数组应包含所有值。 –

相关问题