我的应用程序中有一项服务和一项活动。 service通过用户的操作获取字符串,并将该字符串传递给活动。在这里,我成功获取活动的字符串值并显示在活动屏幕上。
我在活动中有一个列表视图,字符串值应该按顺序显示在该列表中。我的问题是我只能在列表视图中显示最近输入的一个项目。之前输入的字符串未显示。
因此,每次我必须从服务中获取字符串,该字符串应与listview一起显示在列表视图中。 请帮帮我..谢谢。
我的活动是......
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 :(得分:0)
如果您提供一段代码来查看它将会很有帮助。 阅读你的问题似乎是你正在编写用于填充列表的数组。
只需从活动中获取字符串并将其添加到您用于显示listview的数组中,并调用notifyDataSetChanged()。它会起作用
答案 1 :(得分:0)
我认为int count = list.getCount();
中的这一行onCreate
会返回0,因为到目前为止您没有向list
添加任何项目。